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 $identifier => $testConfig) { |
51
|
|
|
$this->loadTest($serviceLocator, $engine, $identifier, $testConfig); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function loadTest(ServiceLocatorInterface $serviceLocator, EngineInterface $engine, $identifier, array $config) |
56
|
|
|
{ |
57
|
|
|
$filter = null; |
58
|
|
|
if (array_key_exists('filter', $config)) { |
59
|
|
|
$filter = $serviceLocator->get($config['filter']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$variantChooser = null; |
63
|
|
|
if (array_key_exists('variant_chooser', $config)) { |
64
|
|
|
$variantChooser = $serviceLocator->get($config['variant_chooser']); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$variants = $this->loadTestVariants($serviceLocator, $config); |
68
|
|
|
$options = array_key_exists('options', $config) ? $config['options'] : []; |
69
|
|
|
|
70
|
|
|
$test = new Test($identifier, $variants); |
71
|
|
|
|
72
|
|
|
$engine->addTest($test, $options, $filter, $variantChooser); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function loadTestVariants(ServiceLocatorInterface $serviceLocator, $config) |
76
|
|
|
{ |
77
|
|
|
$variants = []; |
78
|
|
|
|
79
|
|
|
foreach ($config['variants'] as $identifier => $variant) { |
80
|
|
|
|
81
|
|
|
// We also support shortcuts so that the user can specify a service name straight away. |
82
|
|
|
if (is_string($variant)) { |
83
|
|
|
$variant = [ |
84
|
|
|
'type' => $variant, |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (!array_key_exists('type', $variant)) { |
89
|
|
|
throw new RuntimeException('The type of the variant is missing.'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$options = empty($variant['options']) ? [] : $variant['options']; |
93
|
|
|
|
94
|
|
|
$variants[] = $this->loadTestVariant($serviceLocator, $variant['type'], $identifier, $options); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $variants; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function loadTestVariant(ServiceLocatorInterface $serviceLocator, $type, $identifier, array $options) |
101
|
|
|
{ |
102
|
|
|
switch ($type) { |
103
|
|
|
case 'callback': |
104
|
|
|
$variant = $this->loadTestVariantCallback($serviceLocator, $identifier, $options); |
105
|
|
|
break; |
106
|
|
|
|
107
|
|
|
case 'simple': |
108
|
|
|
$variant = $this->loadTestVariantSimple($serviceLocator, $identifier, $options); |
109
|
|
|
break; |
110
|
|
|
|
111
|
|
|
case 'service_manager': |
112
|
|
|
default: |
113
|
|
|
$variant = $this->loadTestVariantFromServiceManager($serviceLocator, $type); |
114
|
|
|
break; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $variant; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private function loadTestVariantCallback(ServiceLocatorInterface $serviceLocator, $identifier, array $options) |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
if (!array_key_exists('callback', $options)) { |
123
|
|
|
throw new RuntimeException('Missing "callback" for callback variant.'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if (!is_callable($options['callback'])) { |
127
|
|
|
throw new RuntimeException('The "callback" for callback variant cannot be called.'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return new CallbackVariant($identifier, $options['callback']); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
private function loadTestVariantSimple(ServiceLocatorInterface $serviceLocator, $identifier, array $options) |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
return new SimpleVariant($identifier); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
private function loadTestVariantFromServiceManager(ServiceLocatorInterface $serviceLocator, $type) |
139
|
|
|
{ |
140
|
|
|
if (!$serviceLocator->has($type)) { |
141
|
|
|
throw new RuntimeException(sprintf('The variant "%s" is not a valid service manager name.', $type)); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $serviceLocator->get($type); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
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.