Completed
Push — master ( fc1c11...f02f16 )
by recca
25:01 queued 19:39
created

Artisan::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 13
ccs 9
cts 9
cp 1
crap 2
rs 9.8333
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 4
    public function __construct(ArtisanContract $artisan)
50
    {
51 4
        parent::__construct();
52
53 4
        $this->artisan = $artisan;
54 4
    }
55
56
    /**
57
     * Handle the command.
58
     *
59
     * @throws \InvalidArgumentException
60
     */
61 4
    public function handle()
62
    {
63 4
        $command = $this->forceCommand(
64 4
            trim($this->option('command'))
65
        );
66
67 4
        $input = new StringInput($command);
68 4
        $input->setInteractive(false);
69 4
        if (isset($this->notSupport[$input->getFirstArgument()]) === true) {
70 1
            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 4
    protected function forceCommand($command)
82
    {
83
        if ((
84 4
            Str::startsWith($command, 'migrate') === true && Str::startsWith($command, 'migrate:status') === false ||
85 4
            Str::startsWith($command, 'db:seed') === true
86 4
        ) && strpos($command, '--force') === false) {
87 1
            $command .= ' --force';
88
        }
89
90
        if ((
91 4
            is_null($this->laravel) === false &&
92 4
            version_compare($this->laravel->version(), 5.5, '>=') &&
93 4
            Str::startsWith($command, 'vendor:publish') === true
94 4
        ) && strpos($command, '--all') === false) {
95 1
            $command .= ' --all';
96
        }
97
98 4
        return $command;
99
    }
100
101
    /**
102
     * Get the console command options.
103
     *
104
     * @return array
105
     */
106 4
    protected function getOptions()
107
    {
108
        return [
109 4
            ['command', null, InputOption::VALUE_REQUIRED],
110
        ];
111
    }
112
}
113