Passed
Push — master ( f60209...9abc45 )
by Korotkov
13:21 queued 12s
created

FootballSubject::getSubjectName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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\Observer;
11
12
class FootballSubject implements SubjectInterface
13
{
14
    use NameTrait;
15
16
    private array $observers = [];
17
18
    public function attachObserver(ObserverInterface $observer): void
19
    {
20
        if (array_key_exists($observer->getName(), $this->observers)) {
21
            throw new \InvalidArgumentException("Observer is already exist");
22
        }
23
24
        $this->observers[$observer->getName()] = $observer;
25
    }
26
27
    public function detachObserver(string $name): void
28
    {
29
        if (!array_key_exists($name, $this->observers)) {
30
            throw new \InvalidArgumentException("Observer is not exist");
31
        }
32
33
        unset($this->observers[$name]);
34
    }
35
36
    public function notifyObservers(EventInterface $event): void
37
    {
38
        foreach ($this->observers as $observer) {
39
            printf(
40
                "%s has get information about: %s %s \n",
41
                $observer->getName(),
42
                $this->name,
43
                $event->getName()
44
            );
45
        }
46
    }
47
}
48