Completed
Push — master ( 2a88b7...1fad07 )
by Vladimir
06:33
created

InstallDriver::handle()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 55
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

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