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

InstructionsCollection   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 42
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A addInstruction() 0 4 1
B execute() 0 18 5
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
}