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

InstallDriver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 12.2%

Importance

Changes 0
Metric Value
dl 0
loc 75
ccs 5
cts 41
cp 0.122
rs 10
c 0
b 0
f 0
wmc 7
lcom 2
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
B handle() 0 64 6
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