Completed
Branch master (ed410a)
by Mariano
03:14
created

ActionFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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