Completed
Push — master ( f7ceef...6986d0 )
by Alejandro
16s
created

ListKeysCommand::determineMessagePattern()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

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