FootballSubject   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 9
Bugs 0 Features 1
Metric Value
eloc 16
c 9
b 0
f 1
dl 0
loc 60
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A notifyObservers() 0 12 2
A attachObserver() 0 7 2
A detachObserver() 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\Observer;
11
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
61
    {
62
        foreach ($this->observers as $observer) {
63
            printf(
64
                "%s has get information about: %s %s \n",
65
                $observer->getName(),
66
                $this->name,
67
                $event->getName()
68
            );
69
        }
70
71
        print("\n");
72
    }
73
}
74