Completed
Pull Request — master (#13)
by Korotkov
02:36
created

Registry   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 4 2
A getCommandsRegistry() 0 11 3
A __construct() 0 3 1
A getDevice() 0 3 1
A setCommand() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author    : Korotkov Danila <[email protected]>
7
 * @license   https://mit-license.org/ MIT
8
 */
9
10
namespace Behavioral\Command;
11
12
/**
13
 * Class Registry
14
 * @package Behavioral\Command
15
 */
16
class Registry implements RegistryInterface
17
{
18
    /**
19
     * @var DeviceInterface
20
     */
21
    private $device;
22
    /**
23
     * @var array
24
     */
25
    private $commandsRegistry = [];
26
27
    /**
28
     * CommandRegistry constructor.
29
     * @param DeviceInterface $device
30
     */
31
    public function __construct(DeviceInterface $device)
32
    {
33
        $this->device = $device;
34
    }
35
36
    /**
37
     * @param CommandInterface $command
38
     * @param TypeInterface    $type
39
     */
40
    public function setCommand(CommandInterface $command, TypeInterface $type): void
41
    {
42
        $this->commandsRegistry[$type->getName()] = $command;
43
    }
44
45
    /**
46
     * @param TypeInterface $type
47
     */
48
    public function execute(TypeInterface $type): void
49
    {
50
        if (isset($this->commandsRegistry[$type->getName()])) {
51
            $this->getCommandsRegistry($type->getName())->execute($this->getDevice(), $type);
52
        }
53
    }
54
55
    /**
56
     * @return DeviceInterface
57
     */
58
    public function getDevice(): DeviceInterface
59
    {
60
        return $this->device;
61
    }
62
63
    /**
64
     * @param string|null $key
65
     * @return array|mixed
66
     */
67
    public function getCommandsRegistry(string $key = null)
68
    {
69
        if (isset($key)) {
70
            if (array_key_exists($key, $this->commandsRegistry)) {
71
                return $this->commandsRegistry[$key];
72
            }
73
74
            throw new \InvalidArgumentException('Wrong argument');
75
        }
76
77
        return $this->commandsRegistry;
78
    }
79
}
80