Completed
Pull Request — master (#148)
by Kevin
02:16
created

InstructionsCollection::execute()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 12
nc 5
nop 0
1
<?php
2
3
namespace Magium\TestCase\Configurable;
4
5
use Zend\Di\Di;
6
7
class InstructionsCollection
8
{
9
10
    protected $instructions = [];
11
    protected $container;
12
    protected $interpolator;
13
14
    public function __construct(
15
        Di $container,
16
        Interpolator $interpolator
17
    )
18
    {
19
        $this->container = $container;
20
        $this->interpolator = $interpolator;
21
    }
22
23
    public function addInstruction(InstructionInterface $instruction)
24
    {
25
        $this->instructions[] = $instruction;
26
    }
27
28
    public function execute()
29
    {
30
        foreach ($this->instructions as $instruction) {
31
            if ($instruction instanceof InstructionInterface) {
32
                $instance = $this->container->get($instruction->getClassName());
33
                $callback = [$instance, $instruction->getMethod()];
34
                if (!is_callable($callback)) {
35
                    throw new InvalidInstructionException('Unable to execute instruction');
36
                }
37
                $params = [];
38
                $callParams = $instruction->getParams();
39
                if ($callParams) {
40
                    $params = $callParams;
41
                }
42
                call_user_func_array($callback, $params);
43
            }
44
        }
45
    }
46
47
48
}