1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sebdesign\SM\Factory; |
4
|
|
|
|
5
|
|
|
use SM\SMException; |
6
|
|
|
use SM\Factory\Factory as BaseFactory; |
7
|
|
|
use SM\Callback\CallbackFactoryInterface; |
8
|
|
|
use Sebdesign\SM\StateMachine\StateMachine; |
9
|
|
|
use Sebdesign\SM\Metadata\MetadataStoreInterface; |
10
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
11
|
|
|
|
12
|
|
|
class Factory extends BaseFactory |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var \Sebdesign\SM\Metadata\MetadataStoreInterface|null |
16
|
|
|
*/ |
17
|
|
|
protected $metadataStore; |
18
|
|
|
|
19
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
20
|
|
|
array $configs, |
21
|
|
|
EventDispatcherInterface $dispatcher = null, |
22
|
|
|
CallbackFactoryInterface $callbackFactory = null, |
23
|
|
|
MetadataStoreInterface $metadataStore = null |
24
|
|
|
) { |
25
|
|
|
parent::__construct($configs, $dispatcher, $callbackFactory); |
26
|
|
|
|
27
|
|
|
$this->metadataStore = $metadataStore; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritcoc}. |
32
|
|
|
*/ |
33
|
|
|
protected function createStateMachine($object, array $config) |
34
|
|
|
{ |
35
|
|
|
if (! isset($config['state_machine_class'])) { |
36
|
|
|
$class = StateMachine::class; |
37
|
|
|
} elseif (class_exists($config['state_machine_class'])) { |
38
|
|
|
$class = $config['state_machine_class']; |
39
|
|
|
} else { |
40
|
|
|
throw new SMException(sprintf( |
41
|
|
|
'Class "%s" for creating a new state machine does not exist.', |
42
|
|
|
$config['state_machine_class'] |
43
|
|
|
)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return new $class($object, $config, $this->dispatcher, $this->callbackFactory, $this->metadataStore); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function addConfig(array $config, $graph = 'default') |
53
|
|
|
{ |
54
|
|
|
$config['states'] = $this->normalizeStates($config); |
55
|
|
|
|
56
|
|
|
parent::addConfig($config, $graph); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Normalize the states as associative arrays. |
61
|
|
|
* |
62
|
|
|
* - The state is defined as a string. |
63
|
|
|
* E.g. ['states' => ['stateA']] |
64
|
|
|
* |
65
|
|
|
* - The state is defined as an associative array. |
66
|
|
|
* E.g. ['states' => [['name' => 'stateA']]] |
67
|
|
|
* |
68
|
|
|
* @param array $config |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
protected function normalizeStates(array $config) |
72
|
|
|
{ |
73
|
|
|
$states = []; |
74
|
|
|
|
75
|
|
|
foreach ($config['states'] as $state) { |
76
|
|
|
if (is_string($state)) { |
77
|
|
|
$state = ['name' => $state]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$states[] = $state; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $states; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.