Lamp   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

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

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
     * 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