RosfinLookupCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\Services\Client\Bridge\Symfony\Console\Command;
6
7
use Damax\Services\Client\Client;
8
use Damax\Services\Client\InvalidRequestException;
9
use Damax\Services\Client\RosfinItem;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Style\SymfonyStyle;
15
16
final class RosfinLookupCommand extends Command
17
{
18
    protected static $defaultName = 'damax:rosfin:lookup';
19
20
    private $client;
21
22
    public function __construct(Client $client)
23
    {
24
        parent::__construct();
25
26
        $this->client = $client;
27
    }
28
29
    protected function configure()
30
    {
31
        $this
32
            ->setDescription('Lookup rosfin catalogue.')
33
            ->addArgument('fullName', InputArgument::REQUIRED, 'Full name.')
34
            ->addArgument('birthDate', InputArgument::OPTIONAL, 'Birth date.')
35
        ;
36
    }
37
38
    protected function execute(InputInterface $input, OutputInterface $output)
39
    {
40
        $io = new SymfonyStyle($input, $output);
41
42
        try {
43
            $check = $this->client->checkRosfin(
44
                (string) $input->getArgument('fullName'),
45
                (string) $input->getArgument('birthDate')
46
            );
47
        } catch (InvalidRequestException $e) {
48
            $io->error($e->getMessage());
49
50
            return 1;
51
        }
52
53
        if (!count($check)) {
54
            $io->success('Not found.');
55
56
            return 0;
57
        }
58
59
        foreach ($check as $item) {
60
            $io->newLine();
61
            $io->table(['Field', 'Value'], $this->formatItem($item));
62
        }
63
    }
64
65
    private function formatItem(RosfinItem $terrorist): array
66
    {
67
        return [
68
            ['ID', $terrorist->id()],
69
            ['Type', $terrorist->type()],
70
            ['Full name', implode("\n", $terrorist->fullName())],
71
            ['Birth date', $terrorist->birthDate() ?: '-'],
72
            ['Birth place', $terrorist->birthPlace() ?: '-'],
73
            ['Description', $terrorist->description() ? mb_substr($terrorist->description(), 0, 120) : '-'],
74
            ['Address', $terrorist->address() ?: '-'],
75
            ['Resolution', $terrorist->resolution() ?: '-'],
76
            ['Passport', $terrorist->passport() ?: '-'],
77
        ];
78
    }
79
}
80