Total Complexity | 6 |
Total Lines | 60 |
Duplicated Lines | 0 % |
Changes | 9 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
12 | class FootballSubject implements SubjectInterface |
||
13 | { |
||
14 | use NameTrait; |
||
15 | |||
16 | private array $observers = []; |
||
17 | |||
18 | /** |
||
19 | * Adds an observer |
||
20 | * --------------------- |
||
21 | * Добавляет наблюдателя |
||
22 | * |
||
23 | * @param ObserverInterface $observer |
||
24 | * @return void |
||
25 | */ |
||
26 | public function attachObserver(ObserverInterface $observer): void |
||
27 | { |
||
28 | if (array_key_exists($observer->getName(), $this->observers)) { |
||
29 | throw new \InvalidArgumentException("Observer is already exist"); |
||
30 | } |
||
31 | |||
32 | $this->observers[$observer->getName()] = $observer; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Removes an observer |
||
37 | * ------------------- |
||
38 | * Убирает наблюдателя |
||
39 | * |
||
40 | * @param string $name |
||
41 | * @return void |
||
42 | */ |
||
43 | public function detachObserver(string $name): void |
||
44 | { |
||
45 | if (!array_key_exists($name, $this->observers)) { |
||
46 | throw new \InvalidArgumentException("Observer is not exist"); |
||
47 | } |
||
48 | |||
49 | unset($this->observers[$name]); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Notifies all observers of an event |
||
54 | * -------------------------------------- |
||
55 | * Уведомляет всех наблюдателей о событии |
||
56 | * |
||
57 | * @param EventInterface $event |
||
58 | * @return void |
||
59 | */ |
||
60 | public function notifyObservers(EventInterface $event): void |
||
72 | } |
||
73 | } |
||
74 |