Completed
Pull Request — master (#164)
by Alejandro
17:04
created

ListKeysCommand::execute()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 2
dl 0
loc 29
rs 9.1448
c 0
b 0
f 0
ccs 20
cts 20
cp 1
crap 5
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\CLI\Command\Api;
5
6
use Shlinkio\Shlink\Rest\Entity\ApiKey;
7
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
use Zend\I18n\Translator\TranslatorInterface;
14
15
class ListKeysCommand extends Command
16
{
17
    private const ERROR_STRING_PATTERN = '<fg=red>%s</>';
18
    private const SUCCESS_STRING_PATTERN = '<info>%s</info>';
19
    private const WARNING_STRING_PATTERN = '<comment>%s</comment>';
20
21
    public const NAME = 'api-key:list';
22
23
    /**
24
     * @var ApiKeyServiceInterface
25
     */
26
    private $apiKeyService;
27
    /**
28
     * @var TranslatorInterface
29
     */
30
    private $translator;
31
32 2
    public function __construct(ApiKeyServiceInterface $apiKeyService, TranslatorInterface $translator)
33
    {
34 2
        $this->apiKeyService = $apiKeyService;
35 2
        $this->translator = $translator;
36 2
        parent::__construct();
37 2
    }
38
39 2
    public function configure()
40
    {
41 2
        $this->setName(self::NAME)
42 2
             ->setDescription($this->translator->translate('Lists all the available API keys.'))
43 2
             ->addOption(
44 2
                 'enabledOnly',
45 2
                 null,
46 2
                 InputOption::VALUE_NONE,
47 2
                 $this->translator->translate('Tells if only enabled API keys should be returned.')
48
             );
49 2
    }
50
51 2
    public function execute(InputInterface $input, OutputInterface $output)
52
    {
53 2
        $io = new SymfonyStyle($input, $output);
54 2
        $enabledOnly = $input->getOption('enabledOnly');
55 2
        $list = $this->apiKeyService->listKeys($enabledOnly);
56 2
        $rows = [];
57
58
        /** @var ApiKey $row */
59 2
        foreach ($list as $row) {
60 2
            $key = $row->getKey();
61 2
            $expiration = $row->getExpirationDate();
62 2
            $messagePattern = $this->determineMessagePattern($row);
63
64
            // Set columns for this row
65 2
            $rowData = [\sprintf($messagePattern, $key)];
66 2
            if (! $enabledOnly) {
67 1
                $rowData[] = \sprintf($messagePattern, $this->getEnabledSymbol($row));
68
            }
69 2
            $rowData[] = $expiration !== null ? $expiration->format(\DateTime::ATOM) : '-';
70
71 2
            $rows[] = $rowData;
72
        }
73
74 2
        $io->table(\array_filter([
75 2
            $this->translator->translate('Key'),
76 2
            ! $enabledOnly ? $this->translator->translate('Is enabled') : null,
77 2
            $this->translator->translate('Expiration date'),
78 2
        ]), $rows);
79 2
    }
80
81 2
    private function determineMessagePattern(ApiKey $apiKey): string
82
    {
83 2
        if (! $apiKey->isEnabled()) {
84
            return self::ERROR_STRING_PATTERN;
85
        }
86
87 2
        return $apiKey->isExpired() ? self::WARNING_STRING_PATTERN : self::SUCCESS_STRING_PATTERN;
88
    }
89
90
    /**
91
     * @param ApiKey $apiKey
92
     * @return string
93
     */
94 1
    private function getEnabledSymbol(ApiKey $apiKey): string
95
    {
96 1
        return ! $apiKey->isEnabled() || $apiKey->isExpired() ? '---' : '+++';
97
    }
98
}
99