Passed
Pull Request — master (#18)
by
unknown
02:29
created

AbstractWorkflow::create()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 42
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 28
nc 12
nop 0
1
<?php
2
3
namespace LineMob\Core\Workflow;
4
5
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
6
use Symfony\Component\Workflow\DefinitionBuilder;
7
use Symfony\Component\Workflow\MarkingStore\MultipleStateMarkingStore;
8
use Symfony\Component\Workflow\Transition;
9
use Symfony\Component\Workflow\Validator\DefinitionValidatorInterface;
10
use Symfony\Component\Workflow\Validator\StateMachineValidator;
11
use Symfony\Component\Workflow\Validator\WorkflowValidator;
12
use Symfony\Component\Workflow\Workflow;
13
use Symfony\Component\Workflow\MarkingStore\SingleStateMarkingStore;
14
15
abstract class AbstractWorkflow
16
{
17
    /**
18
     * @var EventDispatcherInterface
19
     */
20
    protected $dispatcher;
21
22
    /**
23
     * @param EventDispatcherInterface|null $dispatcher
24
     */
25
    public function __construct(EventDispatcherInterface $dispatcher = null)
26
    {
27
        $this->dispatcher = $dispatcher;
28
    }
29
30
    /**
31
     * @param array $config
32
     *
33
     * @return DefinitionValidatorInterface
34
     */
35
    protected function getDefinitionValidator(array $config)
36
    {
37
        if ('state_machine' === $config['type']) {
38
            return new StateMachineValidator();
39
        }
40
41
        if ('single_state' === $config['marking_store']) {
42
            return new WorkflowValidator(true);
43
        }
44
45
        return new WorkflowValidator();
46
    }
47
48
    /**
49
     * @return Workflow
50
     */
51
    public function create()
52
    {
53
        $config = array_merge([
54
            'name' => get_class($this),
55
            'places' => [],
56
            'transitions' => [],
57
            'type' => 'workflow',
58
            'marking_store' => [
59
                'type' => 'single_state',
60
                'arguments' => ['state']
61
            ]
62
        ], $this->getConfig());
63
64
        $definitionBuilder = new DefinitionBuilder();
65
        $definitionBuilder->addPlaces((array) $config['places']);
66
67
        $transitions = [];
68
        foreach ((array) $config['transitions'] as $name => $transition) {
69
            if ('workflow' === $config['type']) {
70
                $transitions[] = new Transition($name, $transition['from'], $transition['to']);
71
            } elseif ('state_machine' === $config['type']) {
72
                foreach ((array)$transition['from'] as $from) {
73
                    foreach ((array)$transition['to'] as $to) {
74
                        $transitions[] = new Transition($name, $from, $to);
75
                    }
76
                }
77
            }
78
        }
79
80
        $definitionBuilder->addTransitions($transitions);
81
        $definition = $definitionBuilder->build();
82
83
        $markingStoreArguments = $config['marking_store']['arguments'];
84
        if ('multiple_state' === $config['marking_store']['type']) {
85
            $marking = new MultipleStateMarkingStore(...$markingStoreArguments);
0 ignored issues
show
Bug introduced by
$markingStoreArguments is expanded, but the parameter $property of Symfony\Component\Workfl...ingStore::__construct() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
            $marking = new MultipleStateMarkingStore(/** @scrutinizer ignore-type */ ...$markingStoreArguments);
Loading history...
86
        } else {
87
            $marking = new SingleStateMarkingStore(...$markingStoreArguments);
0 ignored issues
show
Bug introduced by
$markingStoreArguments is expanded, but the parameter $property of Symfony\Component\Workfl...ingStore::__construct() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
            $marking = new SingleStateMarkingStore(/** @scrutinizer ignore-type */ ...$markingStoreArguments);
Loading history...
88
        }
89
90
        $this->getDefinitionValidator($config)->validate($definition, $config['name']);
91
92
        return new Workflow($definition, $marking, $this->dispatcher, $config['name']);
93
    }
94
95
    /**
96
     * @return array
97
     */
98
    abstract protected function getConfig();
99
}
100