Completed
Push — master ( 0df1f8...958d5f )
by Sébastien
07:01
created

StateMachine::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 5
1
<?php
2
3
namespace Sebdesign\SM\StateMachine;
4
5
use SM\SMException;
6
use Sebdesign\SM\Metadata\MetadataStore;
7
use SM\Callback\CallbackFactoryInterface;
8
use Sebdesign\SM\Metadata\MetadataStoreInterface;
9
use SM\StateMachine\StateMachine as BaseStateMachine;
10
use Symfony\Component\PropertyAccess\PropertyAccessor;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
13
class StateMachine extends BaseStateMachine
14
{
15
    /**
16
     * @var \Sebdesign\SM\Metadata\MetadataStoreInterface
17
     */
18
    protected $metadataStore;
19
20
    /**
21
     * {@inheritdoc}
22
     *
23
     * @param \Sebdesign\SM\Metadata\MetadataStoreInterface|null $metadataStore
24
     */
25 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
26
        $object,
27
        array $config,
28
        EventDispatcherInterface $dispatcher = null,
29
        CallbackFactoryInterface $callbackFactory = null,
30
        MetadataStoreInterface   $metadataStore = null
31
    ) {
32
        parent::__construct($object, $config, $dispatcher, $callbackFactory);
33
34
        $this->metadataStore = $metadataStore ?: new MetadataStore($config);
35
    }
36
37
    /**
38
     * Set a new state to the underlying object.
39
     *
40
     * @param string $state
41
     *
42
     * @throws \SM\SMException
43
     */
44
    protected function setState($state)
45
    {
46
        if (! $this->hasState($state)) {
47
            throw new SMException(sprintf(
48
                'Cannot set the state to "%s" to object "%s" with graph %s because it is not pre-defined.',
49
                $state,
50
                get_class($this->object),
51
                $this->config['graph']
52
            ));
53
        }
54
55
        $accessor = new PropertyAccessor();
56
        $accessor->setValue($this->object, $this->config['property_path'], $state);
57
    }
58
59
    /**
60
     * Check if the graph has the given state.
61
     *
62
     * @param  string $state
63
     * @return bool
64
     */
65
    protected function hasState($state)
66
    {
67
        foreach ($this->config['states'] as $value) {
68
            if ($value['name'] === $state) {
69
                return true;
70
            }
71
        }
72
73
        return false;
74
    }
75
76
    /**
77
     * Get the metadata.
78
     *
79
     * @return \Sebdesign\SM\Metadata\MetadataStoreInterface
80
     */
81
    public function metadata($type = null, $subject = null, $key = null, $default = null)
82
    {
83
        if (is_null($type)) {
84
            return $this->metadataStore;
85
        }
86
87
        switch ($type) {
88
            case 'graph':
89
                return $this->getGraphMetadata($subject, $key);
90
            case 'state':
91
                return $this->getStateMetadata($subject, $key, $default);
92
            case 'transition':
93
                return $this->getTransitionMetadata($subject, $key, $default);
94
            default:
95
                return $this->getGraphMetadata($type, $subject);
96
        }
97
    }
98
99
    protected function getGraphMetadata($key, $default)
100
    {
101
        return $this->metadataStore->graph($key, $default);
102
    }
103
104
    protected function getStateMetadata($subject, $key, $default)
105
    {
106
        if ($this->hasState($subject)) {
107
            return $this->metadataStore->state($subject, $key, $default);
108
        }
109
110
        if (is_null($subject)) {
111
            return $this->metadataStore->state($this->getState(), $key, $default);
112
        }
113
114
        return $this->metadataStore->state($this->getState(), $subject, $key);
115
    }
116
117
    protected function getTransitionMetadata($subject, $key, $default)
118
    {
119
        return $this->metadataStore->transition($subject, $key, $default);
120
    }
121
}
122