Completed
Push — master ( aca90e...8ef0e7 )
by Alejandro
10s
created

ListKeysCommand   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 89.13%

Importance

Changes 0
Metric Value
dl 0
loc 107
rs 10
c 0
b 0
f 0
ccs 41
cts 46
cp 0.8913
wmc 16
lcom 1
cbo 6

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 11 1
B execute() 0 29 5
A determineFormatMethod() 0 8 3
A getErrorString() 0 4 1
A getSuccessString() 0 4 1
A getWarningString() 0 4 1
A getEnabledSymbol() 0 4 3
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
    const NAME = 'api-key:list';
18
19
    /**
20
     * @var ApiKeyServiceInterface
21
     */
22
    private $apiKeyService;
23
    /**
24
     * @var TranslatorInterface
25
     */
26
    private $translator;
27
28 2
    public function __construct(ApiKeyServiceInterface $apiKeyService, TranslatorInterface $translator)
29
    {
30 2
        $this->apiKeyService = $apiKeyService;
31 2
        $this->translator = $translator;
32 2
        parent::__construct();
33 2
    }
34
35 2
    public function configure()
36
    {
37 2
        $this->setName(self::NAME)
38 2
             ->setDescription($this->translator->translate('Lists all the available API keys.'))
39 2
             ->addOption(
40 2
                 'enabledOnly',
41 2
                 null,
42 2
                 InputOption::VALUE_NONE,
43 2
                 $this->translator->translate('Tells if only enabled API keys should be returned.')
44
             );
45 2
    }
46
47 2
    public function execute(InputInterface $input, OutputInterface $output)
48
    {
49 2
        $io = new SymfonyStyle($input, $output);
50 2
        $enabledOnly = $input->getOption('enabledOnly');
51 2
        $list = $this->apiKeyService->listKeys($enabledOnly);
52 2
        $rows = [];
53
54
        /** @var ApiKey $row */
55 2
        foreach ($list as $row) {
56 2
            $key = $row->getKey();
57 2
            $expiration = $row->getExpirationDate();
58 2
            $formatMethod = $this->determineFormatMethod($row);
59
60
            // Set columns for this row
61 2
            $rowData = [$formatMethod($key)];
62 2
            if (! $enabledOnly) {
63 1
                $rowData[] = $formatMethod($this->getEnabledSymbol($row));
64
            }
65 2
            $rowData[] = $expiration !== null ? $expiration->format(\DateTime::ATOM) : '-';
66
67 2
            $rows[] = $rowData;
68
        }
69
70 2
        $io->table(array_filter([
71 2
            $this->translator->translate('Key'),
72 2
            ! $enabledOnly ? $this->translator->translate('Is enabled') : null,
73 2
            $this->translator->translate('Expiration date'),
74 2
        ]), $rows);
75 2
    }
76
77 2
    private function determineFormatMethod(ApiKey $apiKey): callable
78
    {
79 2
        if (! $apiKey->isEnabled()) {
80
            return [$this, 'getErrorString'];
81
        }
82
83 2
        return $apiKey->isExpired() ? [$this, 'getWarningString'] : [$this, 'getSuccessString'];
84
    }
85
86
    /**
87
     * @param string $value
88
     * @return string
89
     */
90
    private function getErrorString(string $value): string
91
    {
92
        return sprintf('<fg=red>%s</>', $value);
93
    }
94
95
    /**
96
     * @param string $value
97
     * @return string
98
     */
99 2
    private function getSuccessString(string $value): string
100
    {
101 2
        return sprintf('<info>%s</info>', $value);
102
    }
103
104
    /**
105
     * @param string $value
106
     * @return string
107
     */
108
    private function getWarningString(string $value): string
109
    {
110
        return sprintf('<comment>%s</comment>', $value);
111
    }
112
113
    /**
114
     * @param ApiKey $apiKey
115
     * @return string
116
     */
117 1
    private function getEnabledSymbol(ApiKey $apiKey): string
118
    {
119 1
        return ! $apiKey->isEnabled() || $apiKey->isExpired() ? '---' : '+++';
120
    }
121
}
122