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.
Passed
Push — master ( 02f758...f8ecde )
by Anton
02:06
created

Task::setHasRun()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Task;
9
10
use Deployer\Host\Host;
11
use function Deployer\Support\array_flatten;
12
13
class Task
14
{
15
    /**
16
     * @var string
17
     */
18
    private $name;
19
20
    /**
21
     * @var callable
22
     */
23
    private $callback;
24
25
    /**
26
     * @var string
27
     */
28
    private $description;
29
30
    /**
31
     * Should we run this task locally?
32
     *
33
     * @var bool
34
     */
35
    private $local = false;
36
37
    /**
38
     * Lists of hosts, roles, stages there task should be executed.
39
     *
40
     * @var array
41
     */
42
    private $on = ['hosts' => [], 'roles' => [], 'stages' => []];
43
44
    /**
45
     * List of task names to run before.
46
     *
47
     * @var array
48
     */
49
    private $before = [];
50
51
    /**
52
     * List of task names to run after.
53
     *
54
     * @var array
55
     */
56
    private $after = [];
57
58
    /**
59
     * Make task internal and not visible in CLI.
60
     *
61
     * @var bool
62
     */
63
    private $private = false;
64
65
    /**
66
     * Mark task to run only once, of the first node from the pool
67
     *
68
     * @var bool
69
     */
70
    private $once = false;
71
72
    /**
73
     * Mark if the task has run at least once
74
     *
75
     * @var bool
76
     */
77
    private $hasRun = false;
78
79
    /**
80
     * Shallow task will not print execution message/finish messages.
81
     * Useful for success messages and info printing.
82
     *
83
     * @var bool
84
     */
85
    private $shallow = false;
86
87
    /**
88
     * @param string $name Tasks name
89
     * @param callable $callback Task code
90
     */
91 28
    public function __construct($name, callable $callback = null)
92
    {
93 28
        $this->name = $name;
94 28
        $this->callback = $callback;
95 28
    }
96
97
    /**
98
     * @param Context $context
99
     */
100 16
    public function run(Context $context)
101
    {
102 16
        Context::push($context);
103
104
        // Call task
105 16
        call_user_func($this->callback);
106
107 16
        if ($this->once) {
108 1
            $this->hasRun = true;
109
        }
110
111
        // Clear working_path
112 16
        if ($context->getConfig() !== null) {
113 13
            $context->getConfig()->set('working_path', false);
114
        }
115
116 16
        Context::pop();
117 16
    }
118
119
    /**
120
     * @return string
121
     */
122 21
    public function getName()
123
    {
124 21
        return $this->name;
125
    }
126
127
    /**
128
     * @return string
129
     */
130 17
    public function getDescription()
131
    {
132 17
        return $this->description;
133
    }
134
135
    /**
136
     * @param string $description
137
     * @return $this
138
     */
139 12
    public function desc($description)
140
    {
141 12
        $this->description = $description;
142 12
        return $this;
143
    }
144
145
    /**
146
     * Mark this task local
147
     *
148
     * @return $this
149
     */
150 17
    public function local()
151
    {
152 17
        $this->local = true;
153 17
        return $this;
154
    }
155
156
    /**
157
     * @return bool
158
     */
159 17
    public function isLocal()
160
    {
161 17
        return $this->local;
162
    }
163
164 13
    public function once()
165
    {
166 13
        $this->once = true;
167 13
        return $this;
168
    }
169
170 6
    public function isOnce()
171
    {
172 6
        return $this->once;
173
    }
174
175
    /**
176
     * @param array $hosts
177
     * @return $this
178
     */
179 2
    public function onHosts(...$hosts)
180
    {
181 2
        $this->on['hosts'] = array_flatten($hosts);
182 2
        return $this;
183
    }
184
185
    /**
186
     * @param array $roles
187
     * @return $this
188
     */
189 2
    public function onRoles(...$roles)
190
    {
191 2
        $this->on['roles'] = array_flatten($roles);
192 2
        return $this;
193
    }
194
195
    /**
196
     * @param array $stages
197
     * @return $this
198
     */
199 2
    public function onStage(...$stages)
200
    {
201 2
        $this->on['stages'] = array_flatten($stages);
202 2
        return $this;
203
    }
204
205
    /**
206
     * Checks what task should be performed on one of hosts.
207
     *
208
     * @param Host[] $hosts
209
     * @return bool
210
     */
211 21
    public function shouldBePerformed(...$hosts)
212
    {
213
        // don't allow to run again it the task has been marked to run only once
214 21
        if ($this->once && $this->hasRun) {
215 3
            return false;
216
        }
217
218 21
        foreach ($hosts as $host) {
219 18
            $onHost = empty($this->on['hosts']) || in_array($host->getHostname(), $this->on['hosts'], true);
220
221 18
            $onRole = empty($this->on['roles']);
222 18
            foreach ((array) $host->get('roles', []) as $role) {
223 6
                if (in_array($role, $this->on['roles'], true)) {
224 6
                    $onRole = true;
225
                }
226
            }
227
228 18
            $onStage = empty($this->on['stages']);
229 18
            if ($host->has('stage')) {
230 2
                if (in_array($host->get('stage'), $this->on['stages'], true)) {
231 1
                    $onStage = true;
232
                }
233
            }
234
235 18
            if ($onHost && $onRole && $onStage) {
236 18
                return true;
237
            }
238
        }
239
240 4
        return empty($hosts);
241
    }
242
243
    /**
244
     * @return boolean
245
     */
246 17
    public function isPrivate()
247
    {
248 17
        return $this->private;
249
    }
250
251
    /**
252
     * Mark task as private
253
     *
254
     * @return $this
255
     */
256 12
    public function setPrivate()
257
    {
258 12
        $this->private = true;
259 12
        return $this;
260
    }
261
262
    /**
263
     * @param string $task
264
     *
265
     * @return $this
266
     */
267 1
    public function addBefore(string $task)
268
    {
269 1
        array_unshift($this->before, $task);
270 1
        return $this;
271
    }
272
273
    /**
274
     * @param string $task
275
     *
276
     * @return $this
277
     */
278 1
    public function addAfter(string $task)
279
    {
280 1
        array_push($this->after, $task);
281 1
        return $this;
282
    }
283
284
    /**
285
     * Get before tasks names.
286
     * @return string[]
287
     */
288 19
    public function getBefore()
289
    {
290 19
        return $this->before;
291
    }
292
293
    /**
294
     * Get after tasks names.
295
     * @return string[]
296
     */
297 19
    public function getAfter()
298
    {
299 19
        return $this->after;
300
    }
301
302
    /**
303
     * Sets task shallow.
304
     *
305
     * Shallow task will not print execution message/finish messages.
306
     *
307
     * @return $this
308
     */
309 11
    public function shallow()
310
    {
311 11
        $this->shallow = true;
312 11
        return $this;
313
    }
314
315
    /**
316
     * @return bool
317
     */
318 18
    public function isShallow()
319
    {
320 18
        return $this->shallow;
321
    }
322
323
    /**
324
     * @internal this is used by ParallelExecutor and prevent multiple run
325
     */
326 2
    public function setHasRun()
327
    {
328 2
        if ($this->isOnce()) {
329 2
            $this->hasRun = true;
330
        }
331 2
    }
332
}
333