InstructionsCollection   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 56
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A addInstruction() 0 4 1
B execute() 0 29 6
1
<?php
2
3
namespace Magium\TestCase\Configurable;
4
5
use Magium\Util\Log\LoggerInterface;
6
use Zend\Di\Di;
7
8
class InstructionsCollection
9
{
10
11
    protected $instructions = [];
12
    protected $container;
13
    protected $interpolator;
14
    protected $logger;
15
16
    public function __construct(
17
        Di $container,
18
        Interpolator $interpolator,
19
        LoggerInterface $logger
20
    )
21
    {
22
        $this->container = $container;
23
        $this->interpolator = $interpolator;
24
        $this->logger = $logger;
25
    }
26
27
    public function addInstruction(InstructionInterface $instruction)
28
    {
29
        $this->instructions[] = $instruction;
30
    }
31
32
    public function execute()
33
    {
34
        foreach ($this->instructions as $instruction) {
35
            if ($instruction instanceof InstructionInterface) {
36
                $instance = $this->container->get($instruction->getClassName());
37
                $callback = [$instance, $instruction->getMethod()];
38
                if (!is_callable($callback)) {
39
                    throw new InvalidInstructionException('Unable to execute instruction');
40
                }
41
                $params = [];
42
                $callParams = $instruction->getParams();
43
                if ($callParams) {
44
                    $params = $callParams;
45
                }
46
                $this->logger->info(sprintf('Executing %s', $instruction->getClassName()), [
47
                    'class' => get_class($instance),
48
                    'params'    => json_encode($callParams)
49
                ]);
50
                try {
51
                    call_user_func_array($callback, $params);
52
                } catch (\Exception $e) {
53
54
                    $this->logger->err($e->getMessage());
55
                    throw $e;
56
                }
57
                $this->logger->info(sprintf('%s completed', $instruction->getClassName()));
58
            }
59
        }
60
    }
61
62
63
}
64