Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
87.50% |
14 / 16 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
DecoratingTaskSuggesterFactory | |
87.50% |
14 / 16 |
|
66.67% |
2 / 3 |
5.05 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
setLogger | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
create | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace GrowthExperiments\NewcomerTasks\TaskSuggester; |
4 | |
5 | use GrowthExperiments\NewcomerTasks\ConfigurationLoader\ConfigurationLoader; |
6 | use Psr\Log\LoggerAwareInterface; |
7 | use Psr\Log\LoggerInterface; |
8 | use Psr\Log\NullLogger; |
9 | use Wikimedia\ObjectFactory\ObjectFactory; |
10 | |
11 | /** |
12 | * A TaskSuggesterFactory that wraps another factory and decorates the suggester created by it. |
13 | */ |
14 | class DecoratingTaskSuggesterFactory extends TaskSuggesterFactory { |
15 | |
16 | /** @var TaskSuggesterFactory */ |
17 | private $taskSuggesterFactory; |
18 | |
19 | /** @var ObjectFactory */ |
20 | private $objectFactory; |
21 | |
22 | /** |
23 | * A list of ObjectFactory specifications for the decorators. The decorated suggester is |
24 | * passed via the 'extraArgs' option. |
25 | * @var array[] |
26 | */ |
27 | private $decorators; |
28 | |
29 | /** |
30 | * @param TaskSuggesterFactory $taskSuggesterFactory |
31 | * @param ObjectFactory $objectFactory |
32 | * @param array $decorators A list of ObjectFactory specifications for the decorators. |
33 | * The decorated suggester is passed via the 'extraArgs' option. |
34 | */ |
35 | public function __construct( |
36 | TaskSuggesterFactory $taskSuggesterFactory, |
37 | ObjectFactory $objectFactory, |
38 | array $decorators |
39 | ) { |
40 | $this->taskSuggesterFactory = $taskSuggesterFactory; |
41 | $this->objectFactory = $objectFactory; |
42 | $this->decorators = $decorators; |
43 | $this->logger = new NullLogger(); |
44 | } |
45 | |
46 | /** @inheritDoc */ |
47 | public function setLogger( LoggerInterface $logger ): void { |
48 | $this->logger = $logger; |
49 | $this->taskSuggesterFactory->setLogger( $logger ); |
50 | } |
51 | |
52 | /** @inheritDoc */ |
53 | public function create( ?ConfigurationLoader $customConfigurationLoader = null ) { |
54 | $suggester = $this->taskSuggesterFactory->create( $customConfigurationLoader ); |
55 | foreach ( $this->decorators as $spec ) { |
56 | $suggester = $this->objectFactory->createObject( $spec, [ |
57 | 'allowCallable' => true, |
58 | 'extraArgs' => [ $suggester ], |
59 | 'assertClass' => TaskSuggester::class, |
60 | ] ); |
61 | if ( $suggester instanceof LoggerAwareInterface ) { |
62 | $suggester->setLogger( $this->logger ); |
63 | } |
64 | } |
65 | return $suggester; |
66 | } |
67 | |
68 | } |