Completed
Push — master ( f7424d...b53e51 )
by Alejandro
07:43
created

ListKeysCommand::getEnabledSymbol()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 2
nc 4
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Shlinkio\Shlink\CLI\Command\Api;
3
4
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
5
use Shlinkio\Shlink\Rest\Entity\ApiKey;
6
use Shlinkio\Shlink\Rest\Service\ApiKeyService;
7
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Helper\Table;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Zend\I18n\Translator\TranslatorInterface;
14
15
class ListKeysCommand extends Command
16
{
17
    /**
18
     * @var ApiKeyServiceInterface
19
     */
20
    private $apiKeyService;
21
    /**
22
     * @var TranslatorInterface
23
     */
24
    private $translator;
25
26
    /**
27
     * ListKeysCommand constructor.
28
     * @param ApiKeyServiceInterface|ApiKeyService $apiKeyService
29
     * @param TranslatorInterface $translator
30
     *
31
     * @Inject({ApiKeyService::class, "translator"})
32
     */
33 2
    public function __construct(ApiKeyServiceInterface $apiKeyService, TranslatorInterface $translator)
34
    {
35 2
        $this->apiKeyService = $apiKeyService;
36 2
        $this->translator = $translator;
37 2
        parent::__construct(null);
38 2
    }
39
40 2
    public function configure()
41
    {
42 2
        $this->setName('api-key:list')
43 2
             ->setDescription($this->translator->translate('Lists all the available API keys.'))
44 2
             ->addOption(
45 2
                 'enabledOnly',
46 2
                 null,
47 2
                 InputOption::VALUE_NONE,
48 2
                 $this->translator->translate('Tells if only enabled API keys should be returned.')
49 2
             );
50 2
    }
51
52 2
    public function execute(InputInterface $input, OutputInterface $output)
53
    {
54 2
        $enabledOnly = $input->getOption('enabledOnly');
55 2
        $list = $this->apiKeyService->listKeys($enabledOnly);
56
57 2
        $table = new Table($output);
58 2
        if ($enabledOnly) {
59 1
            $table->setHeaders([
60 1
                $this->translator->translate('Key'),
61 1
                $this->translator->translate('Expiration date'),
62 1
            ]);
63 1
        } else {
64 1
            $table->setHeaders([
65 1
                $this->translator->translate('Key'),
66 1
                $this->translator->translate('Is enabled'),
67 1
                $this->translator->translate('Expiration date'),
68 1
            ]);
69
        }
70
71
        /** @var ApiKey $row */
72 2
        foreach ($list as $row) {
73 2
            $key = $row->getKey();
74 2
            $expiration = $row->getExpirationDate();
75 2
            $rowData = [];
76 2
            $formatMethod = ! $row->isEnabled()
77 2
                ? 'getErrorString'
78 2
                : ($row->isExpired() ? 'getWarningString' : 'getSuccessString');
79
80 2
            if ($enabledOnly) {
81 1
                $rowData[] = $this->{$formatMethod}($key);
82 1
            } else {
83 1
                $rowData[] = $this->{$formatMethod}($key);
84 1
                $rowData[] = $this->{$formatMethod}($this->getEnabledSymbol($row));
85
            }
86
87 2
            $rowData[] = isset($expiration) ? $expiration->format(\DateTime::ISO8601) : '-';
88 2
            $table->addRow($rowData);
89 2
        }
90
91 2
        $table->render();
92 2
    }
93
94
    /**
95
     * @param string $string
96
     * @return string
97
     */
98
    protected function getErrorString($string)
99
    {
100
        return sprintf('<fg=red>%s</>', $string);
101
    }
102
103
    /**
104
     * @param string $string
105
     * @return string
106
     */
107 2
    protected function getSuccessString($string)
108
    {
109 2
        return sprintf('<info>%s</info>', $string);
110
    }
111
112
    /**
113
     * @param $string
114
     * @return string
115
     */
116
    protected function getWarningString($string)
117
    {
118
        return sprintf('<comment>%s</comment>', $string);
119
    }
120
121
    /**
122
     * @param ApiKey $apiKey
123
     * @return string
124
     */
125 1
    protected function getEnabledSymbol(ApiKey $apiKey)
126
    {
127 1
        return ! $apiKey->isEnabled() || $apiKey->isExpired() ? '---' : '+++';
128
    }
129
}
130