Completed
Push — master ( 63d3c0...3a8a58 )
by Korotkov
02:41 queued 12s
created

Lamp   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 8
c 3
b 0
f 0
dl 0
loc 30
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 7 2
A setCommand() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author  : Jagepard <[email protected]>
7
 * @license https://mit-license.org/ MIT
8
 */
9
10
namespace Behavioral\Command;
11
12
class Lamp implements DeviceInterface
13
{
14
    /**
15
     * @var array
16
     */
17
    private $commandsRegistry = [];
18
19
    /**
20
     * @param string $type
21
     */
22
    public function execute(string $type): void
23
    {
24
        if (!array_key_exists($type, $this->commandsRegistry)) {
25
            throw new \InvalidArgumentException('Type does not exist in the Registry');
26
        }
27
28
        $this->commandsRegistry[$type]->execute();
29
    }
30
31
    /**
32
     * @param string           $type
33
     * @param CommandInterface $command
34
     */
35
    public function setCommand(string $type, CommandInterface $command): void
36
    {
37
        if (array_key_exists($type, $this->commandsRegistry)) {
38
            throw new \InvalidArgumentException('Command already exists');
39
        }
40
41
        $this->commandsRegistry[$type] = $command;
42
    }
43
}
44