Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.91% covered (success)
90.91%
10 / 11
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
LinkRecommendationFilter
90.91% covered (success)
90.91%
10 / 11
50.00% covered (danger)
50.00%
1 / 2
6.03
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
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
5.03
1<?php
2
3namespace GrowthExperiments\NewcomerTasks;
4
5use GrowthExperiments\NewcomerTasks\AddLink\LinkRecommendationStore;
6use GrowthExperiments\NewcomerTasks\Task\TaskSet;
7use GrowthExperiments\NewcomerTasks\TaskType\LinkRecommendationTaskType;
8
9/**
10 * Filter out link recommendation tasks that don't have a DB entry for recommendation data.
11 */
12class LinkRecommendationFilter extends AbstractTaskSetFilter implements TaskSetFilter {
13
14    /** @var LinkRecommendationStore */
15    private $linkRecommendationStore;
16
17    /**
18     * @param LinkRecommendationStore $linkRecommendationStore
19     */
20    public function __construct( LinkRecommendationStore $linkRecommendationStore ) {
21        $this->linkRecommendationStore = $linkRecommendationStore;
22    }
23
24    /** @inheritDoc */
25    public function filter( TaskSet $taskSet, int $maxLength = PHP_INT_MAX ): TaskSet {
26        $invalidTasks = [];
27        $validTasks = [];
28        foreach ( $taskSet as $task ) {
29            if ( count( $validTasks ) >= $maxLength ) {
30                break;
31            }
32            if ( $task->getTaskType() instanceof LinkRecommendationTaskType &&
33                !$this->linkRecommendationStore->getByLinkTarget( $task->getTitle() ) ) {
34                $invalidTasks[] = $task;
35            } else {
36                $validTasks[] = $task;
37            }
38        }
39        return $this->copyValidAndInvalidTasksToNewTaskSet( $taskSet, $validTasks, $invalidTasks );
40    }
41}