Completed
Branch develop (4c3d66)
by Mariano
11:18
created

ActionFactory::createActionFromConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2.0023

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 19
ccs 11
cts 12
cp 0.9167
rs 9.4285
cc 2
eloc 11
nc 2
nop 2
crap 2.0023
1
<?php
2
namespace Mcustiel\PowerRoute\Common\Factories;
3
4
use Mcustiel\PowerRoute\Actions\GoToAction;
5
use Mcustiel\PowerRoute\PowerRoute;
6
use Mcustiel\PowerRoute\Actions\ActionInterface;
7
8
class ActionFactory extends Mapping
9
{
10 3
    public function __construct(array $mapping)
11
    {
12 3
        parent::__construct(array_merge(['goto' => [GoToAction::class]], $mapping));
13 3
    }
14
15
    /**
16
     * @param array $config
17
     *
18
     * @return \Mcustiel\PowerRoute\Actions\ActionInterface[]
19
     */
20 3
    public function createFromConfig(array $config, PowerRoute $executor)
21
    {
22 3
        $actions = [];
23
24 3
        foreach ($config as $actionData) {
25 3
            $actions[] = $this->createActionFromConfig($actionData, $executor);
26 3
        }
27
28 3
        return $actions;
29
    }
30
31 3
    private function createActionFromConfig($actionData, $executor)
32
    {
33 3
        $class = key($actionData);
34 3
        $this->checkMappingIsValid($class);
35
36 3
        if (is_object($this->mapping[$class])) {
37
            return $this->mapping[$class];
38
        }
39
40 3
        $className = $this->getClassName($class);
41 3
        $arguments = $this->getConstructorArguments($class);
42
43 3
        $object = new $className($arguments);
44 3
        $object->setArgument(
45 3
            $this->getConstructorArgument($executor, $actionData[$class], $className)
46 3
        );
47
48 3
        return $object;
49
    }
50
51 3
    private function getConstructorArgument($executor, $argument, $class)
52
    {
53 3
        if ($class == GoToAction::class) {
54 2
            $classArgument = new \stdClass;
55 2
            $classArgument->route = $argument;
56 2
            $classArgument->executor = $executor;
57 2
        } else {
58 3
            $classArgument = $argument;
59
        }
60
61 3
        return $classArgument;
62
    }
63
}
64