Completed
Push — master ( 7df85e...fc1c11 )
by recca
21:52 queued 20:42
created

Artisan::forceCommand()   B

Complexity

Conditions 9
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
nc 4
nop 1
dl 0
loc 19
ccs 11
cts 11
cp 1
crap 9
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
namespace Recca0120\Terminal\Console\Commands;
4
5
use Illuminate\Contracts\Console\Kernel as ArtisanContract;
6
use Illuminate\Support\Str;
7
use InvalidArgumentException;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Input\StringInput;
10
11
class Artisan extends Command
12
{
13
    /**
14
     * The console command name.
15
     *
16
     * @var string
17
     */
18
    protected $name = 'artisan';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'laravel artisan';
26
27
    /**
28
     * no support array.
29
     *
30
     * @var array
31
     */
32
    protected $notSupport = [
33
        'down' => '',
34
        'tinker' => '',
35
    ];
36
37
    /**
38
     * $artisan.
39
     *
40
     * @var \Illuminate\Contracts\Console\Kernel
41
     */
42
    protected $artisan;
43
44
    /**
45
     * __construct.
46
     *
47
     * @param \Illuminate\Contracts\Console\Kernel $artisan
48
     */
49 3
    public function __construct(ArtisanContract $artisan)
50
    {
51 3
        parent::__construct();
52
53 3
        $this->artisan = $artisan;
54 3
    }
55
56
    /**
57
     * Handle the command.
58
     *
59
     * @throws \InvalidArgumentException
60
     */
61 3
    public function handle()
62
    {
63 3
        $command = $this->forceCommand(
64 3
            trim($this->option('command'))
65
        );
66
67 3
        $input = new StringInput($command);
68 3
        $input->setInteractive(false);
69 3
        if (isset($this->notSupport[$input->getFirstArgument()]) === true) {
70
            throw new InvalidArgumentException('Command "'.$command.'" is not supported');
71
        }
72 3
        $this->artisan->handle($input, $this->getOutput());
73 3
    }
74
75
    /**
76
     * need focre option.
77
     *
78
     * @param string $command
79
     * @return string
80
     */
81 3
    protected function forceCommand($command)
82
    {
83
        if ((
84 3
            Str::startsWith($command, 'migrate') === true && Str::startsWith($command, 'migrate:status') === false ||
85 3
            Str::startsWith($command, 'db:seed') === true
86 3
        ) && strpos($command, '--force') === false) {
87 1
            $command .= ' --force';
88
        }
89
90
        if ((
91 3
            is_null($this->laravel) === false &&
92 3
            version_compare($this->laravel->version(), 5.5, '>=') &&
93 3
            Str::startsWith($command, 'vendor:publish') === true
94 3
        ) && strpos($command, '--all') === false) {
95 1
            $command .= ' --all';
96
        }
97
98 3
        return $command;
99
    }
100
101
    /**
102
     * Get the console command options.
103
     *
104
     * @return array
105
     */
106 3
    protected function getOptions()
107
    {
108
        return [
109 3
            ['command', null, InputOption::VALUE_REQUIRED],
110
        ];
111
    }
112
}
113