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

ActionFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 47
wmc 6
lcom 1
cbo 3
ccs 24
cts 24
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createFromConfig() 0 10 2
A createActionFromConfig() 0 10 1
A getConstructorArgument() 0 12 2
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