RemoveApiKeyCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 15
dl 0
loc 45
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 5 1
A execute() 0 11 3
A __construct() 0 5 1
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Command/ApiKey/RemoveApiKeyCommand.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Command\ApiKey;
10
11
use App\Command\Traits\SymfonyStyleTrait;
12
use App\Entity\ApiKey;
13
use App\Resource\ApiKeyResource;
14
use Symfony\Component\Console\Attribute\AsCommand;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Throwable;
19
20
/**
21
 * Class RemoveApiKeyCommand
22
 *
23
 * @package App\Command\ApiKey
24
 * @author TLe, Tarmo Leppänen <[email protected]>
25
 */
26
#[AsCommand(
27
    name: self::NAME,
28
    description: 'Console command to remove existing API key',
29
)]
30
class RemoveApiKeyCommand extends Command
31
{
32
    use SymfonyStyleTrait;
33
34
    final public const NAME = 'api-key:remove';
35
36
    public function __construct(
37
        private readonly ApiKeyResource $apiKeyResource,
38
        private readonly ApiKeyHelper $apiKeyHelper,
39
    ) {
40
        parent::__construct();
41
    }
42
43
    /**
44
     * @noinspection PhpMissingParentCallCommonInspection
45
     *
46
     * @throws Throwable
47
     */
48
    protected function execute(InputInterface $input, OutputInterface $output): int
49
    {
50
        $io = $this->getSymfonyStyle($input, $output);
51
        $apiKey = $this->apiKeyHelper->getApiKey($io, 'Which API key you want to remove?');
52
        $message = $apiKey instanceof ApiKey ? $this->delete($apiKey) : null;
53
54
        if ($input->isInteractive()) {
55
            $io->success($message ?? ['Nothing changed - have a nice day']);
56
        }
57
58
        return 0;
59
    }
60
61
    /**
62
     * @return array<int, string>
63
     *
64
     * @throws Throwable
65
     */
66
    private function delete(ApiKey $apiKey): array
67
    {
68
        $this->apiKeyResource->delete($apiKey->getId());
69
70
        return $this->apiKeyHelper->getApiKeyMessage('API key deleted - have a nice day', $apiKey);
71
    }
72
}
73