Completed
Push — master ( f9e7cd...0f9065 )
by Basenko
09:06
created

Run::publish()   C

Complexity

Conditions 7
Paths 3

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 12
cts 12
cp 1
rs 6.9811
c 0
b 0
f 0
cc 7
eloc 13
nc 3
nop 1
crap 7
1
<?php
2
3
namespace MadWeb\Initializer;
4
5
use InvalidArgumentException;
6
use MadWeb\Initializer\Contracts\Runner;
7
8
class Run implements Runner
9
{
10
    protected $commands = [];
11
12 24
    public function artisan(string $command, array $arguments = []): Runner
13
    {
14 24
        $this->pushCommand(__FUNCTION__, $command, $arguments);
15
16 24
        return $this;
17
    }
18
19 6
    public function external(string $command, ...$arguments): Runner
20
    {
21 6
        $this->pushCommand(__FUNCTION__, $command, $arguments);
22
23 6
        return $this;
24
    }
25
26 3
    public function callable(callable $function, ...$arguments): Runner
27
    {
28 3
        $this->pushCommand(__FUNCTION__, $function, $arguments);
29
30 3
        return $this;
31
    }
32
33 24
    public function dispatch($job): Runner
34
    {
35 24
        $this->pushCommand(__FUNCTION__, $job);
36
37 24
        return $this;
38
    }
39
40 6
    public function dispatchNow($job): Runner
41
    {
42 6
        $this->pushCommand(__FUNCTION__, $job);
43
44 6
        return $this;
45
    }
46
47 24
    public function publish($providers): Runner
48
    {
49 24
        if (is_string($providers)) {
50 3
            $this->artisan('vendor:publish', ['--provider' => $providers]);
51 21
        } elseif (is_array($providers)) {
52 18
            foreach ($providers as $provider => $tag) {
53 18
                $arguments = [];
54
55 18
                $arguments['--provider'] = is_numeric($provider) ? $tag : $provider;
56
57 18
                if (! is_numeric($provider) and is_string($tag)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
58 12
                    $arguments['--tag'] = $tag;
59
                }
60
61 18
                $this->artisan('vendor:publish', $arguments);
62
            }
63
        } else {
64 3
            throw new InvalidArgumentException('Invalid publishable argument.');
65
        }
66
67 21
        return $this;
68
    }
69
70 60
    protected function pushCommand(string $type, $command, array $arguments = [])
71
    {
72 60
        $this->commands[] = compact('type', 'command', 'arguments');
73 60
    }
74
75 60
    public function getCommands(): array
76
    {
77 60
        return $this->commands;
78
    }
79
}
80