Executer   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 11
c 4
b 1
f 0
lcom 1
cbo 3
dl 0
loc 121
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 4 1
A executeFrom() 0 4 1
A executeFromArray() 0 4 1
A marshal() 0 13 2
A marshalFromArray() 0 4 1
A getParameterValueForCommand() 0 20 4
1
<?php
2
3
namespace TillKruss\LaravelTactician;
4
5
use ArrayAccess;
6
use ReflectionClass;
7
use ReflectionParameter;
8
use League\Tactician\CommandBus;
9
use Illuminate\Support\Collection;
10
use TillKruss\LaravelTactician\Exceptions\CanNotMapParameterValue;
11
use TillKruss\LaravelTactician\Contracts\Executer as ExecuterContract;
12
13
class Executer implements ExecuterContract
14
{
15
    /**
16
     * The command bus instance.
17
     *
18
     * @var \League\Tactician\CommandBus
19
     */
20
    protected $bus;
21
22
    /**
23
     * Create a new command bus executer.
24
     *
25
     * @param \League\Tactician\CommandBus  $bus
26
     */
27
    public function __construct(CommandBus $bus)
28
    {
29
        $this->bus = $bus;
30
    }
31
32
    /**
33
     * Executes a command in the command bus.
34
     *
35
     * @param  object  $command
36
     * @return mixed
37
     */
38
    public function execute($command)
39
    {
40
        return $this->bus->handle($command);
41
    }
42
43
    /**
44
     * Marshal a command from the given class name and execute it in the command bus.
45
     *
46
     * @param  string       $command
47
     * @param  ArrayAccess  $source
48
     * @param  array        $extras
49
     * @return mixed
50
     */
51
    public function executeFrom($command, ArrayAccess $source, array $extras = [])
52
    {
53
        return $this->execute($this->marshal($command, $source, $extras));
54
    }
55
56
    /**
57
     * Marshal a command from the given class name and execute it in the command bus.
58
     *
59
     * @param  string  $command
60
     * @param  array   $array
61
     * @return mixed
62
     */
63
    public function executeFromArray($command, array $array)
64
    {
65
        return $this->execute($this->marshalFromArray($command, $array));
66
    }
67
68
    /**
69
     * Marshal a command from the given array accessible object.
70
     *
71
     * @param  string       $command
72
     * @param  ArrayAccess  $source
73
     * @param  array        $extras
74
     * @return object
75
     */
76
    protected function marshal($command, ArrayAccess $source, array $extras = [])
77
    {
78
        $injected = [];
79
        $reflection = new ReflectionClass($command);
80
81
        if ($constructor = $reflection->getConstructor()) {
82
            $injected = array_map(function ($parameter) use ($command, $source, $extras) {
83
                return $this->getParameterValueForCommand($command, $source, $parameter, $extras);
84
            }, $constructor->getParameters());
85
        }
86
87
        return $reflection->newInstanceArgs($injected);
88
    }
89
90
    /**
91
     * Marshal a command from the given array.
92
     *
93
     * @param  string  $command
94
     * @param  array   $array
95
     * @return object
96
     */
97
    protected function marshalFromArray($command, array $array)
98
    {
99
        return $this->marshal($command, new Collection, $array);
100
    }
101
102
    /**
103
     * Get a parameter value for a marshalled command.
104
     *
105
     * @param  string              $command
106
     * @param  ArrayAccess         $source
107
     * @param  ReflectionParameter $parameter
108
     * @param  array               $extras
109
     * @return mixed
110
     *
111
     * @throws Exceptions\CanNotMapParameterValue
112
     */
113
    protected function getParameterValueForCommand(
114
        $command,
115
        ArrayAccess $source,
116
        ReflectionParameter $parameter,
117
        array $extras = []
118
    ) {
119
        if (array_key_exists($parameter->name, $extras)) {
120
            return $extras[$parameter->name];
121
        }
122
123
        if (isset($source[$parameter->name])) {
124
            return $source[$parameter->name];
125
        }
126
127
        if ($parameter->isDefaultValueAvailable()) {
128
            return $parameter->getDefaultValue();
129
        }
130
131
        throw CanNotMapParameterValue::forCommand($command, $parameter->name);
132
    }
133
}
134