GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — 3.0 ( 06e746...c52ac9 )
by Vermeulen
04:55
created

Subject::getNotifyHeap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace BFW;
4
5
use \Exception;
6
use \SplSubject;
7
use \SplObserver;
8
9
/**
10
 * Class to manage subject in observers systems
11
 */
12
class Subject implements SplSubject
13
{
14
    /**
15
     * @const ERR_OBSERVER_NOT_FOUND Exception code if the observer to detach
16
     * has not been found.
17
     */
18
    const ERR_OBSERVER_NOT_FOUND = 1314001;
19
    
20
    /**
21
     * @var \SplObserver[] $observers List of all observers
22
     */
23
    protected $observers = [];
24
    
25
    /**
26
     * @var \stdClass[] $notifyHeap List of notify to send
27
     */
28
    protected $notifyHeap = [];
29
    
30
    /**
31
     * @var string $action The current action to send to observers
32
     */
33
    protected $action = '';
34
    
35
    /**
36
     * @var mixed $context The current context to send to observers
37
     */
38
    protected $context = null;
39
    
40
    /**
41
     * Return list of all observers
42
     * 
43
     * @return \SplObserver[]
44
     */
45
    public function getObservers()
46
    {
47
        return $this->observers;
48
    }
49
    
50
    /**
51
     * Return list of all notify to send
52
     * 
53
     * @return \stdClass[]
54
     */
55
    public function getNotifyHeap()
56
    {
57
        return $this->notifyHeap;
58
    }
59
    
60
    /**
61
     * Return the action
62
     * 
63
     * @return string
64
     */
65
    public function getAction()
66
    {
67
        return $this->action;
68
    }
69
    
70
    /**
71
     * Return the context
72
     * 
73
     * @return mixed
74
     */
75
    public function getContext()
76
    {
77
        return $this->context;
78
    }
79
80
    /**
81
     * Attach a new observer to the list
82
     * 
83
     * @param \SplObserver $observer The new observer
84
     * 
85
     * @return \BFW\Subject The current instance of this class
86
     */
87
    public function attach(SplObserver $observer)
88
    {
89
        $this->observers[] = $observer;
90
91
        return $this;
92
    }
93
94
    /**
95
     * Detach a observer to the list
96
     * 
97
     * @param \SplObserver $observer The observer instance to detach
98
     * 
99
     * @return \BFW\Subject The current instance of this class
100
     */
101
    public function detach(SplObserver $observer)
102
    {
103
        $key = array_search($observer, $this->observers, true);
104
        
105
        if ($key === false) {
106
            throw new Exception(
107
                'The observer has not been found.',
108
                self::ERR_OBSERVER_NOT_FOUND
109
            );
110
        }
111
        
112
        unset($this->observers[$key]);
113
114
        return $this;
115
    }
116
117
    /**
118
     * Send a notification to all observers
119
     * 
120
     * @return \BFW\Subject The current instance of this class
121
     */
122
    public function notify()
123
    {
124
        foreach ($this->observers as $observer) {
125
            $observer->update($this);
126
        }
127
        
128
        return $this;
129
    }
130
    
131
    /**
132
     * Read the notify heap list and send each notify into the list.
133
     * 
134
     * @return $this
135
     */
136
    public function readNotifyHeap()
137
    {
138
        foreach ($this->notifyHeap as $notifyIndex => $notifyDatas) {
139
            $this->action  = $notifyDatas->action;
140
            $this->context = $notifyDatas->context;
141
            
142
            $this->notify();
143
            
144
            //Remove the current notification from list
145
            unset($this->notifyHeap[$notifyIndex]);
146
        }
147
        
148
        //Some new notifications has been added during the loop
149
        if (count($this->notifyHeap) > 0) {
150
            $this->readNotifyHeap();
151
        }
152
153
        return $this;
154
    }
155
    
156
    /**
157
     * Add a new notification to the list of notification to send.
158
     * If there is only one notification into the list, it will be send now.
159
     * Else, a notification is currently sent, so we wait it finish and the
160
     * current notification will be sent.
161
     * 
162
     * @param string $action The action to send
163
     * @param notification $context (default null) The context to send
164
     * 
165
     * @return \BFW\Subject The current instance of this class
166
     */
167
    public function addNotification($action, $context = null)
168
    {
169
        $this->notifyHeap[] = (object) [
170
            'action'  => $action,
171
            'context' => $context
172
        ];
173
        
174
        if (count($this->notifyHeap) === 1) {
175
            $this->readNotifyHeap();
176
        }
177
        
178
        return $this;
179
    }
180
}
181