Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
TaskSetListener
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace GrowthExperiments\NewcomerTasks;
4
5use GrowthExperiments\GrowthExperimentsServices;
6use GrowthExperiments\NewcomerTasks\AddImage\CacheBackedImageRecommendationProvider;
7use GrowthExperiments\NewcomerTasks\Task\TaskSet;
8use GrowthExperiments\NewcomerTasks\TaskType\ImageRecommendationBaseTaskType;
9use MediaWiki\Deferred\DeferredUpdates;
10use MediaWiki\MediaWikiServices;
11use Wikimedia\ObjectCache\WANObjectCache;
12use Wikimedia\Stats\IBufferingStatsdDataFactory;
13
14/**
15 * Called from CacheDecorator after a TaskSet is returned (either from cache or by calling the
16 * decorated suggester).
17 *
18 * Used currently for fetching and caching recommendation data for image recommendation tasks.
19 */
20class TaskSetListener {
21
22    /** @var WANObjectCache */
23    private $cache;
24
25    /** @var IBufferingStatsdDataFactory */
26    private $statsdDataFactory;
27
28    /**
29     * @param WANObjectCache $cache
30     * @param IBufferingStatsdDataFactory $statsdDataFactory
31     */
32    public function __construct( WANObjectCache $cache, IBufferingStatsdDataFactory $statsdDataFactory ) {
33        $this->cache = $cache;
34        $this->statsdDataFactory = $statsdDataFactory;
35    }
36
37    /**
38     * Execute code after task set is returned from CacheDecorator (either from cache or by
39     * calling the decorated suggester).
40     *
41     * FIXME Find a better way to structure the actions that different task types can require when a TaskSet is
42     * constructed.
43     *
44     * @param TaskSet $taskSet
45     */
46    public function run( TaskSet $taskSet ): void {
47        $fname = __METHOD__;
48        DeferredUpdates::addCallableUpdate( function () use ( $taskSet, $fname ) {
49            foreach ( $taskSet as $task ) {
50                if ( $task->getTaskType() instanceof ImageRecommendationBaseTaskType ) {
51                    $growthServices = GrowthExperimentsServices::wrap( MediaWikiServices::getInstance() );
52                    CacheBackedImageRecommendationProvider::getWithSetCallback(
53                        $this->cache,
54                        $growthServices->getImageRecommendationProviderUncached(),
55                        $task->getTaskType(),
56                        $task->getTitle(),
57                        $fname,
58                        $this->statsdDataFactory
59                    );
60                }
61            }
62        } );
63    }
64}