1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpAbModule\Service; |
4
|
|
|
|
5
|
|
|
use PhpAb\Engine\Engine; |
6
|
|
|
use PhpAb\Engine\EngineInterface; |
7
|
|
|
use PhpAb\Event\DispatcherInterface; |
8
|
|
|
use PhpAb\Participation\ParticipationManagerInterface; |
9
|
|
|
use PhpAb\Test\Test; |
10
|
|
|
use PhpAb\Variant\CallbackVariant; |
11
|
|
|
use PhpAb\Variant\SimpleVariant; |
12
|
|
|
use RuntimeException; |
13
|
|
|
use Zend\ServiceManager\FactoryInterface; |
14
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
15
|
|
|
|
16
|
|
|
class EngineFactory implements FactoryInterface |
17
|
|
|
{ |
18
|
|
|
public function createService(ServiceLocatorInterface $serviceLocator) |
19
|
|
|
{ |
20
|
|
|
/** @var array $config */ |
21
|
|
|
$config = $serviceLocator->get('Config'); |
22
|
|
|
|
23
|
|
|
/** @var ParticipationManagerInterface $participationManager */ |
24
|
|
|
$participationManager = $serviceLocator->get('phpab.participation_manager'); |
25
|
|
|
|
26
|
|
|
/** @var DispatcherInterface $dispatcher */ |
27
|
|
|
$dispatcher = $serviceLocator->get('phpab.dispatcher'); |
28
|
|
|
|
29
|
|
|
$filter = $this->loadService($serviceLocator, $config['phpab']['default_filter']); |
30
|
|
|
$chooser = $this->loadService($serviceLocator, $config['phpab']['default_variant_chooser']); |
31
|
|
|
|
32
|
|
|
$engine = new Engine($participationManager, $dispatcher, $filter, $chooser); |
|
|
|
|
33
|
|
|
|
34
|
|
|
$this->loadTests($engine, $serviceLocator, $config['phpab']); |
35
|
|
|
|
36
|
|
|
return $engine; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private function loadService(ServiceLocatorInterface $serviceLocator, $serviceName) |
40
|
|
|
{ |
41
|
|
|
if (!$serviceLocator->has($serviceName)) { |
42
|
|
|
return null; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $serviceLocator->get($serviceName); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function loadTests(EngineInterface $engine, ServiceLocatorInterface $serviceLocator, $config) |
49
|
|
|
{ |
50
|
|
|
foreach ($config['tests'] as $key => $testConfig) { |
51
|
|
|
$this->loadTest($serviceLocator, $engine, $testConfig); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function loadTest(ServiceLocatorInterface $serviceLocator, EngineInterface $engine, array $config) |
56
|
|
|
{ |
57
|
|
|
if (!array_key_exists('identifier', $config)) { |
58
|
|
|
throw new RuntimeException('Missing the "identifier" for the test.'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$filter = null; |
62
|
|
|
if (array_key_exists('filter', $config)) { |
63
|
|
|
$filter = $serviceLocator->get($config['filter']); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$variantChooser = null; |
67
|
|
|
if (array_key_exists('variant_chooser', $config)) { |
68
|
|
|
$variantChooser = $serviceLocator->get($config['variant_chooser']); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$variants = $this->loadTestVariants($serviceLocator, $config); |
72
|
|
|
$options = array_key_exists('options', $config) ? $config['options'] : []; |
73
|
|
|
|
74
|
|
|
$test = new Test($config['identifier'], $variants); |
75
|
|
|
|
76
|
|
|
$engine->addTest($test, $options, $filter, $variantChooser); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function loadTestVariants(ServiceLocatorInterface $serviceLocator, $config) |
80
|
|
|
{ |
81
|
|
|
$variants = []; |
82
|
|
|
|
83
|
|
|
foreach ($config['variants'] as $variant) { |
84
|
|
|
if (!array_key_exists('type', $variant)) { |
85
|
|
|
throw new RuntimeException('The type of the variant is missing.'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$options = empty($variant['options']) ? [] : $variant['options']; |
89
|
|
|
|
90
|
|
|
$variants[] = $this->loadTestVariant($serviceLocator, $variant['type'], $options); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $variants; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function loadTestVariant(ServiceLocatorInterface $serviceLocator, $type, array $options) |
97
|
|
|
{ |
98
|
|
|
switch ($type) { |
99
|
|
|
case 'simple': |
100
|
|
|
$variant = $this->loadTestVariantSimple($serviceLocator, $options); |
101
|
|
|
break; |
102
|
|
|
|
103
|
|
|
case 'callback': |
104
|
|
|
$variant = $this->loadTestVariantCallback($serviceLocator, $options); |
105
|
|
|
break; |
106
|
|
|
|
107
|
|
|
default: |
108
|
|
|
$variant = $this->loadTestVariantFromServiceManager($serviceLocator, $type); |
109
|
|
|
break; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $variant; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function loadTestVariantCallback(ServiceLocatorInterface $serviceLocator, array $options) |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
if (!array_key_exists('identifier', $options)) { |
118
|
|
|
throw new RuntimeException('Missing "identifier" for callback variant.'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (!array_key_exists('callback', $options)) { |
122
|
|
|
throw new RuntimeException('Missing "callback" for callback variant.'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (!is_callable($options['callback'])) { |
126
|
|
|
throw new RuntimeException('The "callback" for callback variant cannot be called.'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return new CallbackVariant($options['identifier'], $options['callback']); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
private function loadTestVariantSimple(ServiceLocatorInterface $serviceLocator, array $options) |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
if (!array_key_exists('identifier', $options)) { |
135
|
|
|
throw new RuntimeException('Missing "identifier" for simple variant.'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return new SimpleVariant($options['identifier']); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
private function loadTestVariantFromServiceManager(ServiceLocatorInterface $serviceLocator, $type) |
142
|
|
|
{ |
143
|
|
|
if (!$serviceLocator->has($type)) { |
144
|
|
|
throw new RuntimeException(sprintf('The variant "%s" could not be loaded.', $type)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $serviceLocator->get($type); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.