Passed
Push — master ( a85aeb...922d24 )
by Vladimir
08:38
created

ListDrivers::handle()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 25
c 0
b 0
f 0
cc 2
eloc 15
nc 1
nop 0
ccs 16
cts 16
cp 1
crap 2
rs 8.8571
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Toolbelt\Commands;
6
7
use Http\Client\HttpClient;
8
use FondBot\Toolbelt\Command;
9
use Http\Message\RequestFactory;
10
use Symfony\Component\Console\Helper\Table;
11
12
class ListDrivers extends Command
13
{
14 2
    protected function configure(): void
15
    {
16
        $this
17 2
            ->setName('driver:list')
18 2
            ->setDescription('Get list of all available drivers');
19 2
    }
20
21 1
    public function handle(): void
22
    {
23
        /** @var HttpClient $http */
24 1
        $http = resolve(HttpClient::class);
25
        /** @var RequestFactory $requestFactory */
26 1
        $requestFactory = resolve(RequestFactory::class);
27
28 1
        $request = $requestFactory->createRequest('GET', 'https://fondbot.com/api/drivers');
29
30 1
        $response = $http->sendRequest($request);
31
32 1
        $items = json_decode((string) $response->getBody(), true);
33
34 1
        $drivers = collect($items)
35 1
            ->map(function ($item) {
36 1
                return [$item['name'], $item['package'], $item['official'] ? '✅' : '❌'];
37 1
            })
38 1
            ->toArray();
39
40 1
        $table = new Table($this->output);
41
        $table
42 1
            ->setHeaders(['Name', 'Package', 'Official'])
43 1
            ->setRows($drivers)
44 1
            ->render();
45 1
    }
46
}
47