Passed
Push — master ( 5521dc...e99b67 )
by Vladimir
01:54
created

ListDriversCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Console;
6
7
use FondBot\Foundation\Api;
8
use Illuminate\Console\Command;
9
use FondBot\Channels\ChannelManager;
10
use GuzzleHttp\Exception\ClientException;
11
12
class ListDriversCommand extends Command
13
{
14
    protected $signature = 'fondbot:driver:list';
15
    protected $description = 'List add installed drivers';
16
17
    private $api;
18
    private $channelManager;
19
20
    public function __construct(Api $api, ChannelManager $channelManager)
21
    {
22
        parent::__construct();
23
24
        $this->api = $api;
25
        $this->channelManager = $channelManager;
26
    }
27
28
    public function handle(): void
29
    {
30
        try {
31
            $installedDrivers = collect($this->channelManager->getDrivers())->keys()->toArray();
32
            $availableDrivers = $this->api->getDrivers();
33
34
            $rows = collect($availableDrivers)
35
                ->transform(function ($item) use ($installedDrivers) {
36
                    return [
37
                        $item['name'],
38
                        $item['package'],
39
                        $item['official'] ? '✅' : '❌',
40
                        in_array($item['name'], $installedDrivers, true) ? '✅' : '❌',
41
                    ];
42
                })
43
                ->toArray();
44
45
            $this->table(['Name', 'Package', 'Official', 'Installed'], $rows);
46
        } catch (ClientException $exception) {
47
            $this->error('Connection to FondBot API failed. Please check your internet connection and try again.');
48
        }
49
    }
50
}
51