Completed
Push — 1.0 ( 794bad...d76faf )
by Vladimir
08:37
created

InstallDriver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 13.89%

Importance

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