Lamp::setCommand()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 7
rs 10
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
     * Command registry
16
     * ----------------
17
     * Реестр команд
18
     *
19
     * @var array
20
     */
21
    private array $commands = [];
22
23
    /**
24
     * Executes a specific command from the registry
25
     * ---------------------------------------------
26
     * Исполняет определенную команду из реестра
27
     *
28
     * @param  string $commandName
29
     * @return void
30
     */
31
    public function execute(string $commandName): void
32
    {
33
        if (!array_key_exists($commandName, $this->commands)) {
34
            throw new \InvalidArgumentException("Command $commandName does not exist in the registry");
35
        }
36
37
        $this->commands[$commandName]->execute();
38
    }
39
40
    /**
41
     * Adds a command to the registry
42
     * ------------------------------
43
     * Добавляет команду в реестр
44
     * 
45
     * @param string $commandName
46
     * @param CommandInterface $command
47
     */
48
    public function setCommand(string $commandName, CommandInterface $command): void
49
    {
50
        if (array_key_exists($commandName, $this->commands)) {
51
            throw new \InvalidArgumentException("Command $commandName already exists");
52
        }
53
54
        $this->commands[$commandName] = $command;
55
    }
56
}
57