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

Factory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 13.51 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 10
loc 74
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 1
A createStateMachine() 0 15 3
A addConfig() 0 6 1
A normalizeStates() 0 14 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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(
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...
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