Passed
Push — master ( f57d83...b11985 )
by Vladimir
02:42
created

InstallDriver::handle()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 64
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 35
nc 10
nop 0
dl 0
loc 64
ccs 0
cts 36
cp 0
crap 42
rs 8.6346
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\Toolbelt\Commands;
6
7
use GuzzleHttp\Client;
8
use FondBot\Toolbelt\Command;
9
use FondBot\Foundation\Kernel;
10
use Symfony\Component\Process\Process;
11
use Symfony\Component\Console\Input\InputArgument;
12
13
class InstallDriver extends Command
14
{
15 1
    protected function configure(): void
16
    {
17
        $this
18 1
            ->setName('driver:install')
19 1
            ->setDescription('Install driver')
20 1
            ->addArgument('name', InputArgument::REQUIRED, 'Driver name');
21 1
    }
22
23
    public function handle(): void
24
    {
25
        /** @var Client $http */
26
        $http = resolve(Client::class);
27
28
        $response = $http->request('GET', 'https://fondbot.com/api/drivers', [
29
            'form_params' => ['version' => Kernel::VERSION],
30
        ]);
31
32
        $items = json_decode((string) $response->getBody(), true);
33
34
        // Check if package is listed in store
35
        $name = $this->getArgument('name');
36
        $drivers = collect($items);
37
38
        $driver = $drivers->first(function ($item) use ($name) {
39
            return $item['name'] === $name;
40
        });
41
42
        if ($driver === null) {
43
            $this->error('"'.$name.'" is not found in the official drivers list.');
44
            $package = $this->input('Type composer package name if you know which one you want to install');
45
        } else {
46
            // If driver is not official we should ask user to confirm installation
47
            if ($driver['official'] !== true) {
48
                if (!$this->confirm('"'.$name.'" is not official. Still want to install?')) {
49
                    exit;
50
                }
51
            }
52
53
            $package = $driver['package'];
54
        }
55
56
        // Determine if package is already installed
57
        $composer = json_decode($this->filesystem()->read('composer.json'), true);
58
        $installed = collect($composer['require'])
59
            ->merge($composer['require-dev'])
60
            ->search(function ($_, $item) use ($package) {
61
                return hash_equals($item, $package);
62
            });
63
64
        if ($installed !== false) {
65
            $this->error('Driver is already installed.');
66
67
            return;
68
        }
69
70
        // Install driver
71
        $this->info('Installing driver...');
72
73
        $process = new Process('composer require '.$package, path());
74
        $output = '';
75
        $result = $process->run(function ($_, $line) use (&$output) {
76
            $output .= $line;
77
        });
78
79
        if ($result !== 0) {
80
            $this->error($output);
81
            $this->error('Installation failed.');
82
            exit($result);
83
        }
84
85
        $this->success('Driver installed.');
86
    }
87
}
88