Completed
Push — master ( 106ec7...e6f585 )
by Korotkov
05:44 queued 02:40
created

FootballSubject::notify()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author    : Korotkov Danila <[email protected]>
7
 * @copyright Copyright (c) 2017, Korotkov Danila
8
 * @license   http://www.gnu.org/licenses/gpl.html GNU GPLv3.0
9
 */
10
11
namespace Behavioral\Observer;
12
13
/**
14
 * Class FootballSubject
15
 *
16
 * @package Behavioral\Observer
17
 */
18
class FootballSubject implements SubjectInterface
19
{
20
21
    /**
22
     * @var array
23
     */
24
    protected $observers = [];
25
26
    /**
27
     * @var string
28
     */
29
    protected $subjectName;
30
31
    /**
32
     * FootballSubject constructor.
33
     *
34
     * @param string $subjectName
35
     */
36
    public function __construct(string $subjectName)
37
    {
38
        $this->subjectName = $subjectName;
39
    }
40
41
    /**
42
     * @param ObserverInterface $observer
43
     */
44
    public function attachObserver(ObserverInterface $observer): void
45
    {
46
        $this->observers[$observer->getObserverName()] = $observer;
47
    }
48
49
    /**
50
     * @param string $observerName
51
     */
52
    public function detachObserver(string $observerName): void
53
    {
54
        if (array_key_exists($observerName, $this->observers)) {
55
            unset($this->observers[$observerName]);
56
        }
57
    }
58
59
    /**
60
     * @param EventInterface $event
61
     */
62
    public function notify(EventInterface $event): void
63
    {
64
        foreach ($this->observers as $observer) {
65
            printf("%s has get information about: %s %s \n",
66
                $observer->getObserverName(), $this->getSubjectName(), $event->getEventName());
67
        }
68
    }
69
70
    /**
71
     * @return mixed
72
     */
73
    public function getSubjectName(): string
74
    {
75
        return $this->subjectName;
76
    }
77
}
78