Completed
Push — master ( da89f6...8759f3 )
by recca
01:28
created

Artisan::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Recca0120\Terminal\Console\Commands;
4
5
use InvalidArgumentException;
6
use Illuminate\Console\Command;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Input\StringInput;
9
use Illuminate\Contracts\Console\Kernel as ArtisanContract;
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 3
53 3
        $this->artisan = $artisan;
54
    }
55
56
    /**
57
     * Handle the command.
58
     *
59
     * @throws \InvalidArgumentException
60 3
     */
61
    public function handle()
62 3
    {
63
        $command = $this->forceCommand($this->option('command'));
64 3
65 3
        $input = new StringInput($command);
66 3
        $input->setInteractive(false);
67 1
        if (isset($this->notSupport[$input->getFirstArgument()]) === true) {
68
            throw new InvalidArgumentException('Command "'.$command.'" is not supported');
69 2
        }
70 2
        $this->artisan->handle($input, $this->getOutput());
71
    }
72
73
    /**
74
     * need focre option.
75
     *
76
     * @param string $command
77
     * @return string
78 3
     */
79
    protected function forceCommand($command)
80
    {
81 3
        return (
82 2
            starts_with($command, 'migrate') === true && starts_with($command, 'migrate:status') === false ||
83 3
            starts_with($command, 'db:seed') === true
84 3
        ) && strpos('command', '--force') === false ?
85
            $command .= ' --force' : $command;
86
    }
87
88
    /**
89
     * Get the console command options.
90
     *
91
     * @return array
92 3
     */
93
    protected function getOptions()
94
    {
95 3
        return [
96 3
            ['command', null, InputOption::VALUE_OPTIONAL],
97
        ];
98
    }
99
}
100