Completed
Push — master ( afce1d...f8221d )
by Vladimir
09:37
created

InstallDriver::handle()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 61
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 34
nc 10
nop 2
dl 0
loc 61
ccs 0
cts 35
cp 0
crap 42
rs 8.6806
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Console;
6
7
use GuzzleHttp\Client;
8
use FondBot\Foundation\Kernel;
9
use Illuminate\Console\Command;
10
use Symfony\Component\Process\Process;
11
use Illuminate\Contracts\Filesystem\Filesystem;
12
13
class InstallDriver extends Command
14
{
15
    protected $signature = 'driver:install 
16
                            {name : Driver name to be installed}';
17
18
    public function handle(Client $http, Filesystem $filesystem): void
19
    {
20
        $response = $http->request('GET', 'https://fondbot.com/api/drivers', [
21
            'form_params' => ['version' => Kernel::VERSION],
22
        ]);
23
24
        $items = json_decode((string) $response->getBody(), true);
25
26
        // Check if package is listed in store
27
        $name = $this->argument('name');
28
        $drivers = collect($items);
29
30
        $driver = $drivers->first(function ($item) use ($name) {
31
            return $item['name'] === $name;
32
        });
33
34
        if ($driver === null) {
35
            $this->error('"'.$name.'" is not found in the official drivers list.');
36
            $package = $this->ask('Type composer package name if you know which one you want to install');
37
        } else {
38
            // If driver is not official we should ask user to confirm installation
39
            if ($driver['official'] !== true) {
40
                if (!$this->confirm('"'.$name.'" is not official. Still want to install?')) {
41
                    return;
42
                }
43
            }
44
45
            $package = $driver['package'];
46
        }
47
48
        // Determine if package is already installed
49
        $composer = json_decode($filesystem->get('composer.json'), true);
50
        $installed = collect($composer['require'])
51
            ->merge($composer['require-dev'])
52
            ->search(function ($_, $item) use ($package) {
53
                return hash_equals($item, $package);
54
            });
55
56
        if ($installed !== false) {
57
            $this->error('Driver is already installed.');
58
59
            return;
60
        }
61
62
        // Install driver
63
        $this->info('Installing driver...');
64
65
        $process = new Process('composer require '.$package, path());
66
        $output = '';
67
        $result = $process->run(function ($_, $line) use (&$output) {
68
            $output .= $line;
69
        });
70
71
        if ($result !== 0) {
72
            $this->error($output);
73
            $this->error('Installation failed.');
74
            exit($result);
75
        }
76
77
        $this->info('Driver installed.');
78
    }
79
}
80