Completed
Push — master ( 67e465...aa77c9 )
by Alejandro
13s queued 10s
created

ListKeysCommand::determineMessagePattern()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 3
rs 10
c 0
b 0
f 0
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
    }
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
    }
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
            $expiration = $apiKey->getExpirationDate();
61 2
            $messagePattern = $this->determineMessagePattern($apiKey);
62
63
            // Set columns for this row
64 2
            $rowData = [sprintf($messagePattern, $apiKey)];
65 2
            if (! $enabledOnly) {
66 1
                $rowData[] = sprintf($messagePattern, $this->getEnabledSymbol($apiKey));
67
            }
68 2
            $rowData[] = $expiration !== null ? $expiration->toAtomString() : '-';
69 2
            return $rowData;
70 2
        }, $this->apiKeyService->listKeys($enabledOnly));
71
72 2
        $io->table(array_filter([
73 2
            $this->translator->translate('Key'),
74 2
            ! $enabledOnly ? $this->translator->translate('Is enabled') : null,
75 2
            $this->translator->translate('Expiration date'),
76 2
        ]), $rows);
77
    }
78
79 2
    private function determineMessagePattern(ApiKey $apiKey): string
80
    {
81 2
        if (! $apiKey->isEnabled()) {
82 1
            return self::ERROR_STRING_PATTERN;
83
        }
84
85 2
        return $apiKey->isExpired() ? self::WARNING_STRING_PATTERN : self::SUCCESS_STRING_PATTERN;
86
    }
87
88
    /**
89
     * @param ApiKey $apiKey
90
     * @return string
91
     */
92 1
    private function getEnabledSymbol(ApiKey $apiKey): string
93
    {
94 1
        return ! $apiKey->isEnabled() || $apiKey->isExpired() ? '---' : '+++';
95
    }
96
}
97