Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
90.00% |
18 / 20 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
NewcomerTasksValidator | |
90.00% |
18 / 20 |
|
50.00% |
2 / 4 |
7.05 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
validate | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
4 | |||
validateVariable | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDefaultContent | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace GrowthExperiments\Config\Validation; |
4 | |
5 | use GrowthExperiments\NewcomerTasks\TaskType\ImageRecommendationTaskTypeHandler; |
6 | use GrowthExperiments\NewcomerTasks\TaskType\LinkRecommendationTaskTypeHandler; |
7 | use GrowthExperiments\NewcomerTasks\TaskType\SectionImageRecommendationTaskTypeHandler; |
8 | use GrowthExperiments\NewcomerTasks\TaskType\TaskTypeHandlerRegistry; |
9 | use GrowthExperiments\NewcomerTasks\TaskType\TemplateBasedTaskTypeHandler; |
10 | use StatusValue; |
11 | |
12 | class NewcomerTasksValidator implements IConfigValidator { |
13 | /** |
14 | * Default task type data. Should usually be accessed via |
15 | * SpecialEditGrowthConfig::getDefaultDataForEnabledTaskTypes(). |
16 | */ |
17 | public const SUGGESTED_EDITS_TASK_TYPES = [ |
18 | 'copyedit' => [ |
19 | 'difficulty' => 'easy', |
20 | 'handler-id' => TemplateBasedTaskTypeHandler::ID, |
21 | 'icon' => 'article', |
22 | ], |
23 | 'links' => [ |
24 | 'difficulty' => 'easy', |
25 | 'handler-id' => TemplateBasedTaskTypeHandler::ID, |
26 | 'icon' => 'article', |
27 | ], |
28 | 'link-recommendation' => [ |
29 | 'difficulty' => 'easy', |
30 | 'handler-id' => LinkRecommendationTaskTypeHandler::ID, |
31 | 'icon' => 'robot', |
32 | ], |
33 | 'image-recommendation' => [ |
34 | 'difficulty' => 'medium', |
35 | 'handler-id' => ImageRecommendationTaskTypeHandler::ID, |
36 | 'icon' => 'robot', |
37 | ], |
38 | 'section-image-recommendation' => [ |
39 | 'difficulty' => 'medium', |
40 | 'handler-id' => SectionImageRecommendationTaskTypeHandler::ID, |
41 | 'icon' => 'robot', |
42 | ], |
43 | 'references' => [ |
44 | 'difficulty' => 'medium', |
45 | 'handler-id' => TemplateBasedTaskTypeHandler::ID, |
46 | 'icon' => 'article', |
47 | ], |
48 | 'update' => [ |
49 | 'difficulty' => 'medium', |
50 | 'handler-id' => TemplateBasedTaskTypeHandler::ID, |
51 | 'icon' => 'article', |
52 | ], |
53 | 'expand' => [ |
54 | 'difficulty' => 'hard', |
55 | 'handler-id' => TemplateBasedTaskTypeHandler::ID, |
56 | 'icon' => 'article', |
57 | ] |
58 | ]; |
59 | |
60 | public const SUGGESTED_EDITS_MACHINE_SUGGESTIONS_TASK_TYPES = [ |
61 | 'link-recommendation', |
62 | 'image-recommendation', |
63 | 'section-image-recommendation' |
64 | ]; |
65 | |
66 | /** @var TaskTypeHandlerRegistry */ |
67 | private $taskTypeHandlerRegistry; |
68 | |
69 | /** |
70 | * @param TaskTypeHandlerRegistry $taskTypeHandlerRegistry |
71 | */ |
72 | public function __construct( |
73 | TaskTypeHandlerRegistry $taskTypeHandlerRegistry |
74 | ) { |
75 | $this->taskTypeHandlerRegistry = $taskTypeHandlerRegistry; |
76 | } |
77 | |
78 | /** |
79 | * @inheritDoc |
80 | */ |
81 | public function validate( array $config ): StatusValue { |
82 | $status = StatusValue::newGood(); |
83 | |
84 | // Code inspired by PageConfigurationLoader::parseTaskTypesFromConfig |
85 | foreach ( $config as $taskId => $taskConfig ) { |
86 | // Fall back to legacy task type handler if none specified |
87 | $handlerId = $taskConfig['type'] ?? TemplateBasedTaskTypeHandler::ID; |
88 | |
89 | if ( !$this->taskTypeHandlerRegistry->has( $handlerId ) ) { |
90 | $status->fatal( |
91 | 'growthexperiments-config-validator-newcomertasks-invalid-task-type-handler-id', |
92 | $handlerId |
93 | ); |
94 | continue; |
95 | } |
96 | $taskTypeHandler = $this->taskTypeHandlerRegistry->get( $handlerId ); |
97 | |
98 | $status->merge( |
99 | $taskTypeHandler->validateTaskTypeConfiguration( $taskId, $taskConfig ) |
100 | ); |
101 | if ( $status->isGood() ) { |
102 | $taskType = $taskTypeHandler->createTaskType( $taskId, $taskConfig ); |
103 | $status->merge( $taskTypeHandler->validateTaskTypeObject( $taskType ) ); |
104 | } |
105 | } |
106 | return $status; |
107 | } |
108 | |
109 | /** |
110 | * @inheritDoc |
111 | */ |
112 | public function validateVariable( string $variable, $value ): void { |
113 | // Implemented as no-op, because this method throws an exception |
114 | // which is not user friendly. |
115 | } |
116 | |
117 | /** |
118 | * @inheritDoc |
119 | */ |
120 | public function getDefaultContent(): array { |
121 | return []; |
122 | } |
123 | } |