ActionFactory::createFromConfig()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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