Passed
Push — master ( 84911a...1f9a15 )
by Vladimir
02:21
created

InstallDriverCommand::handle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 31
rs 9.7666
c 0
b 0
f 0
cc 4
nc 4
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Console;
6
7
use FondBot\FondBot;
8
use FondBot\Foundation\Api;
9
use Illuminate\Console\Command;
10
use FondBot\Foundation\Composer;
11
12
class InstallDriverCommand extends Command
13
{
14
    protected $signature = 'fondbot:driver:install 
15
                            {name : Driver name to be installed}';
16
17
    protected $description = 'Install driver';
18
19
    private $api;
20
    private $composer;
21
22
    public function __construct(Api $api, Composer $composer)
23
    {
24
        parent::__construct();
25
26
        $this->api = $api;
27
        $this->composer = $composer;
28
    }
29
30
    public function handle(): void
31
    {
32
        // Check if package is listed in store
33
        $name = $this->argument('name');
34
        $driver = $this->api->findDriver($this->argument('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') can also be of type null and array; however, parameter $name of FondBot\Foundation\Api::findDriver() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        $driver = $this->api->findDriver(/** @scrutinizer ignore-type */ $this->argument('name'));
Loading history...
35
36
        if ($driver === null) {
37
            $this->error('"'.$name.'" is not found in the available drivers list or is not yet supported by current FondBot version ('.FondBot::VERSION.').');
0 ignored issues
show
Bug introduced by
Are you sure $name of type null|string|array can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
            $this->error('"'./** @scrutinizer ignore-type */ $name.'" is not found in the available drivers list or is not yet supported by current FondBot version ('.FondBot::VERSION.').');
Loading history...
38
39
            exit(0);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
40
        }
41
42
        if ($this->composer->installed($driver['package'])) {
43
            $this->error('Driver is already installed.');
44
45
            return;
46
        }
47
48
        // Install driver
49
        $this->info('Installing driver...');
50
51
        $result = $this->composer->install($driver['package'], function ($_, $line) use (&$output) {
52
            $output .= $line;
53
        });
54
55
        if ($result !== 0) {
56
            $this->error($output);
57
            exit($result);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
58
        }
59
60
        $this->info('Driver installed. ✔');
61
    }
62
}
63