Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
70.83% covered (warning)
70.83%
17 / 24
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ImageRecommendationFilter
70.83% covered (warning)
70.83%
17 / 24
33.33% covered (danger)
33.33%
1 / 3
8.22
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 filter
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
5.01
 makeKey
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace GrowthExperiments\NewcomerTasks;
4
5use GrowthExperiments\NewcomerTasks\AddImage\AddImageSubmissionHandler;
6use GrowthExperiments\NewcomerTasks\Task\TaskSet;
7use GrowthExperiments\NewcomerTasks\TaskType\ImageRecommendationBaseTaskType;
8use Wikimedia\ObjectCache\WANObjectCache;
9
10/**
11 * Filter out image recommendation tasks that have been marked as invalid in a temporary cache
12 * in AddImageSubmissionHandler. This is needed because search index updates don't happen in real time.
13 */
14class ImageRecommendationFilter extends AbstractTaskSetFilter implements TaskSetFilter {
15
16    /** @var WANObjectCache */
17    private $cache;
18
19    /**
20     * @param WANObjectCache $cache
21     */
22    public function __construct( WANObjectCache $cache ) {
23        $this->cache = $cache;
24    }
25
26    /** @inheritDoc */
27    public function filter( TaskSet $taskSet, int $maxLength = PHP_INT_MAX ): TaskSet {
28        $invalidTasks = [];
29        $validTasks = [];
30        foreach ( $taskSet as $task ) {
31            if ( count( $validTasks ) >= $maxLength ) {
32                break;
33            }
34            if ( !$task->getTaskType() instanceof ImageRecommendationBaseTaskType ) {
35                $validTasks[] = $task;
36                continue;
37            }
38            $result = $this->cache->get( self::makeKey(
39                $this->cache,
40                $task->getTaskType()->getId(),
41                $task->getTitle()->getDBkey()
42            ) );
43            if ( $result ) {
44                $invalidTasks[] = $task;
45            } else {
46                $validTasks[] = $task;
47            }
48        }
49        return $this->copyValidAndInvalidTasksToNewTaskSet( $taskSet, $validTasks, $invalidTasks );
50    }
51
52    /**
53     * Use a dedicated cache key for keeping track of image recommendations that have been invalidated.
54     *
55     * @param WANObjectCache $cache
56     * @param string $taskTypeId
57     * @param string $dbKey
58     * @return string
59     *
60     * @see AddImageSubmissionHandler::handle()
61     */
62    public static function makeKey( WANObjectCache $cache, string $taskTypeId, string $dbKey ): string {
63        return $cache->makeKey(
64            'GrowthExperiments',
65            'InvalidatedImageRecommendations',
66            $taskTypeId,
67            $dbKey
68        );
69    }
70}