Factory   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 72
ccs 25
cts 25
cp 1
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeStates() 0 13 4
A __construct() 0 9 1
A addConfig() 0 5 1
A createStateMachine() 0 14 3
1
<?php
2
3
namespace Sebdesign\SM\Factory;
4
5
use Sebdesign\SM\Metadata\MetadataStoreInterface;
6
use Sebdesign\SM\StateMachine\StateMachine;
7
use SM\Callback\CallbackFactoryInterface;
8
use SM\Factory\Factory as BaseFactory;
9
use SM\SMException;
10
use SM\StateMachine\StateMachineInterface;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
13
class Factory extends BaseFactory
14
{
15
    /**
16
     * @var \Sebdesign\SM\Metadata\MetadataStoreInterface|null
17
     */
18
    protected $metadataStore;
19
20 39
    public function __construct(
21
        array $configs,
22
        ?EventDispatcherInterface $dispatcher = null,
23
        ?CallbackFactoryInterface $callbackFactory = null,
24
        ?MetadataStoreInterface $metadataStore = null
25
    ) {
26 39
        parent::__construct($configs, $dispatcher, $callbackFactory);
27
28 39
        $this->metadataStore = $metadataStore;
29 13
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 30
    protected function createStateMachine($object, array $config): StateMachineInterface
35
    {
36 30
        if (! isset($config['state_machine_class'])) {
37 24
            $class = StateMachine::class;
38 6
        } elseif (class_exists($config['state_machine_class'])) {
39 3
            $class = $config['state_machine_class'];
40
        } else {
41 3
            throw new SMException(sprintf(
42 3
                'Class "%s" for creating a new state machine does not exist.',
43 3
                $config['state_machine_class']
44 2
            ));
45
        }
46
47 27
        return new $class($object, $config, $this->dispatcher, $this->callbackFactory, $this->metadataStore);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 39
    public function addConfig(array $config, string $graph = 'default'): void
54
    {
55 39
        $config['states'] = $this->normalizeStates($config);
56
57 39
        parent::addConfig($config, $graph);
58 13
    }
59
60
    /**
61
     * Normalize the states as associative arrays.
62
     *
63
     * - The state is null.
64
     *   E.g. ['states' => [null]]
65
     *
66
     * - The state is defined as a string.
67
     *   E.g. ['states' => ['stateA']]
68
     *
69
     * - The state is defined as an associative array.
70
     *   E.g. ['states' => [['name' => 'stateA']]]
71
     */
72 39
    protected function normalizeStates(array $config): array
73
    {
74 39
        $states = [];
75
76 39
        foreach ($config['states'] as $state) {
77 30
            if (is_null($state) || is_scalar($state)) {
78 30
                $state = ['name' => $state];
79
            }
80
81 30
            $states[] = $state;
82
        }
83
84 39
        return $states;
85
    }
86
}
87