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 ( 8b5091...c51706 )
by Anton
01:50
created

Task::run()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

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