ChangeTokenCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 11 3
A __construct() 0 5 1
A changeToken() 0 9 1
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Command/ApiKey/ChangeTokenCommand.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 ChangeTokenCommand
22
 *
23
 * @package App\Command\ApiKey
24
 * @author TLe, Tarmo Leppänen <[email protected]>
25
 */
26
#[AsCommand(
27
    name: self::NAME,
28
    description: 'Command to change token for existing API key',
29
)]
30
class ChangeTokenCommand extends Command
31
{
32
    use SymfonyStyleTrait;
33
34
    final public const NAME = 'api-key:change-token';
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 token you want to change?');
52
        $message = $apiKey instanceof ApiKey ? $this->changeToken($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
     * Method to change API key token.
63
     *
64
     * @return array<int, string>
65
     *
66
     * @throws Throwable
67
     */
68
    private function changeToken(ApiKey $apiKey): array
69
    {
70
        // Generate new token for API key
71
        $apiKey->generateToken();
72
73
        // Update API key
74
        $this->apiKeyResource->save($apiKey);
75
76
        return $this->apiKeyHelper->getApiKeyMessage('API key token updated - have a nice day', $apiKey);
77
    }
78
}
79