Observable   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 6
Bugs 4 Features 0
Metric Value
wmc 14
c 6
b 4
f 0
lcom 2
cbo 0
dl 0
loc 99
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setObserved() 0 8 2
A addObserver() 0 4 1
A deleteObserver() 0 10 3
A notifyObservers() 0 8 3
A countObservers() 0 4 1
A hasChanged() 0 4 1
A setChanged() 0 4 1
A clearChanged() 0 4 1
1
<?php
2
3
namespace PEIP\Event;
4
5
/*
6
 * This file is part of the PEIP package.
7
 * (c) 2009-2016 Timo Michna <timomichna/yahoo.de>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
/**
14
 * Observable.
15
 *
16
 * @author Timo Michna <timomichna/yahoo.de>
17
 * @implements \PEIP\INF\Event\Observable
18
 */
19
class Observable implements \PEIP\INF\Event\Observable
20
{
21
    protected $observedObject;
22
23
    protected $observers = [];
24
25
    protected $hasChanged = false;
26
27
    /**
28
     * @param $observedObject
29
     *
30
     * @return
31
     */
32
    public function __construct($observedObject)
33
    {
34
        $this->setObserved($observedObject);
35
    }
36
37
    protected function setObserved($observedObject)
38
    {
39
        if (!is_object($observedObject)) {
40
            throw new \InvalidArgumentException("$observedObject must be an object. ".gettype($var).' given.');
41
        }
42
43
        $this->observedObject = $observedObject;
44
    }
45
46
    /**
47
     * @param $observer
48
     *
49
     * @return
50
     */
51
    public function addObserver(\PEIP\INF\Event\Observer $observer)
52
    {
53
        $this->observers[] = $observer;
54
    }
55
56
    /**
57
     * @param $observer
58
     *
59
     * @return
60
     */
61
    public function deleteObserver(\PEIP\INF\Event\Observer $observer)
62
    {
63
        foreach ($this->observers as $key => $obs) {
64
            if ($obs == $observer) {
65
                unset($this->observers[$key]);
66
67
                return true;
68
            }
69
        }
70
    }
71
72
    /**
73
     * @param $arguments
74
     *
75
     * @return
76
     */
77
    public function notifyObservers(array $arguments = [])
78
    {
79
        if ($this->hasChanged()) {
80
            foreach ($this->observers as $observer) {
81
                $observer->update($this->observedObject);
82
            }
83
        }
84
    }
85
86
    /**
87
     * @return
88
     */
89
    public function countObservers()
90
    {
91
        return count($this->obeservers);
0 ignored issues
show
Bug introduced by
The property obeservers does not seem to exist. Did you mean observers?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
92
    }
93
94
    /**
95
     * @return
96
     */
97
    public function hasChanged()
98
    {
99
        return $this->hasChanged();
100
    }
101
102
    /**
103
     * @return
104
     */
105
    public function setChanged()
106
    {
107
        $this->hasChanged = true;
108
    }
109
110
    /**
111
     * @return
112
     */
113
    public function clearChanged()
114
    {
115
        $this->hasChanged = true;
116
    }
117
}
118