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.
Test Failed
Push — master ( 31f734...513d45 )
by Anton
06:23
created

Task::onHosts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
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 12
    public function __construct($name, callable $callback = null)
92
    {
93 12
        $this->name = $name;
94 12
        $this->callback = $callback;
95 12
    }
96
97
    /**
98
     * @param Context $context
99
     */
100 3
    public function run(Context $context)
101
    {
102 3
        Context::push($context);
103
104
        // Call task
105 3
        call_user_func($this->callback);
106
107 3
        if ($this->once) {
108 1
            $this->hasRun = true;
109
        }
110
111
        // Clear working_path
112 3
        if ($context->getConfig() !== null) {
113
            $context->getConfig()->set('working_path', false);
114
        }
115
116 3
        Context::pop();
117 3
    }
118
119
    /**
120
     * @return string
121
     */
122 5
    public function getName()
123
    {
124 5
        return $this->name;
125
    }
126
127
    /**
128
     * @return string
129
     */
130 1
    public function getDescription()
131
    {
132 1
        return $this->description;
133
    }
134
135
    /**
136
     * @param string $description
137
     * @return $this
138
     */
139 1
    public function desc($description)
140
    {
141 1
        $this->description = $description;
142 1
        return $this;
143
    }
144
145
    /**
146
     * Mark this task local
147
     *
148
     * @return $this
149
     */
150 1
    public function local()
151
    {
152 1
        $this->local = true;
153 1
        return $this;
154
    }
155
156
    /**
157
     * @return bool
158
     */
159 1
    public function isLocal()
160
    {
161 1
        return $this->local;
162
    }
163
164 2
    public function once()
165
    {
166 2
        $this->once = true;
167 2
        return $this;
168
    }
169
170 1
    public function isOnce()
171
    {
172 1
        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 5
    public function shouldBePerformed(...$hosts)
212
    {
213
        // don't allow to run again it the task has been marked to run only once
214 5
        if ($this->once && $this->hasRun) {
215 1
            return false;
216
        }
217
218 5
        foreach ($hosts as $host) {
219 2
            $onHost = empty($this->on['hosts']) || in_array($host->getHostname(), $this->on['hosts'], true);
220
221 2
            $onRole = empty($this->on['roles']);
222 2
            foreach ((array) $host->get('roles', []) as $role) {
223 2
                if (in_array($role, $this->on['roles'], true)) {
224 1
                    $onRole = true;
225
                }
226
            }
227
228 2
            $onStage = empty($this->on['stages']);
229 2
            if ($host->has('stage')) {
230 2
                if (in_array($host->get('stage'), $this->on['stages'], true)) {
231 1
                    $onStage = true;
232
                }
233
            }
234
235 2
            if ($onHost && $onRole && $onStage) {
236 2
                return true;
237
            }
238
        }
239
240 4
        return empty($hosts);
241
    }
242
243
    /**
244
     * @return boolean
245
     */
246 1
    public function isPrivate()
247
    {
248 1
        return $this->private;
249
    }
250
251
    /**
252
     * Mark task as private
253
     *
254
     * @return $this
255
     */
256 1
    public function setPrivate()
257
    {
258 1
        $this->private = true;
259 1
        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 3
    public function getBefore()
289
    {
290 3
        return $this->before;
291
    }
292
293
    /**
294
     * Get after tasks names.
295
     * @return string[]
296
     */
297 3
    public function getAfter()
298
    {
299 3
        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
    public function shallow()
310
    {
311
        $this->shallow = true;
312
        return $this;
313
    }
314
315
    /**
316
     * @return bool
317
     */
318 2
    public function isShallow()
319
    {
320 2
        return $this->shallow;
321
    }
322
323
    /**
324
     * @internal this is used by ParallelExecutor and prevent multiple run
325
     */
326
    public function setHasRun()
327
    {
328
        if ($this->isOnce()) {
329
            $this->hasRun = true;
330
        }
331
    }
332
}
333