Passed
Branch develop (905d13)
by Mariano
09:27
created

ActionFactory::getConstructorArgument()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

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