Completed
Push — master ( c4148d...4cccaa )
by Korotkov
03:01 queued 11s
created

FootballSubject::notifyObservers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
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
    /**
15
     * @var string
16
     */
17
    private $subjectName;
18
    /**
19
     * @var array
20
     */
21
    private $observers = [];
22
23
    /**
24
     * FootballSubject constructor.
25
     * @param  string  $subjectName
26
     */
27
    public function __construct(string $subjectName)
28
    {
29
        $this->subjectName = $subjectName;
30
    }
31
32
    /**
33
     * @param  ObserverInterface  $observer
34
     * @throws \Exception
35
     */
36
    public function attachObserver(ObserverInterface $observer): void
37
    {
38
        if (array_key_exists($observer->getObserverName(), $this->observers)) {
39
            throw new \InvalidArgumentException('Observer is already exist');
40
        }
41
42
        $this->observers[$observer->getObserverName()] = $observer;
43
    }
44
45
    /**
46
     * @param  string  $subjectName
47
     */
48
    public function detachObserver(string $subjectName): void
49
    {
50
        if (!array_key_exists($subjectName, $this->observers)) {
51
            throw new \InvalidArgumentException('Observer is not exist');
52
        }
53
54
        unset($this->observers[$subjectName]);
55
    }
56
57
    /**
58
     * @param  EventInterface  $event
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->getObserverName(),
66
                $this->subjectName,
67
                $event->getEventName()
68
            );
69
        }
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function getSubjectName(): string
76
    {
77
        return $this->subjectName;
78
    }
79
}
80