Subject::doSomething()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @author: jiangyi
4
 * @date: 下午5:25 2018/11/28
5
 */
6
7
namespace Hello;
8
9
class Subject
10
{
11
    protected $observers = [];
12
    protected $name;
13
14
    public function __construct($name)
15
    {
16
        $this->name = $name;
17
    }
18
19
    public function getName()
20
    {
21
        return $this->name;
22
    }
23
24
    public function attach(Observer $observer)
25
    {
26
        $this->observers[] = $observer;
27
    }
28
29
    public function doSomething()
30
    {
31
        // 做点什么
32
        // ...
33
34
        // 通知观察者发生了些什么
35
        $this->notify('something');
36
    }
37
38
    public function doSomethingBad()
39
    {
40
        foreach ($this->observers as $observer) {
41
            $observer->reportError(42, 'Something bad happened', $this);
42
        }
43
    }
44
45
    protected function notify($argument)
46
    {
47
        foreach ($this->observers as $observer) {
48
            $observer->update($argument);
49
        }
50
    }
51
}
52