|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/** |
|
4
|
|
|
* /src/Command/ApiKey/EditApiKeyCommand.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\DTO\ApiKey\ApiKey as ApiKeyDto; |
|
13
|
|
|
use App\Entity\ApiKey as ApiKeyEntity; |
|
14
|
|
|
use App\Form\Type\Console\ApiKeyType; |
|
15
|
|
|
use App\Resource\ApiKeyResource; |
|
16
|
|
|
use Matthias\SymfonyConsoleForm\Console\Helper\FormHelper; |
|
17
|
|
|
use Symfony\Component\Console\Attribute\AsCommand; |
|
18
|
|
|
use Symfony\Component\Console\Command\Command; |
|
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
21
|
|
|
use Throwable; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class EditApiKeyCommand |
|
25
|
|
|
* |
|
26
|
|
|
* @package App\Command\ApiKey |
|
27
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
#[AsCommand( |
|
30
|
|
|
name: self::NAME, |
|
31
|
|
|
description: 'Command to edit existing API key', |
|
32
|
|
|
)] |
|
33
|
|
|
class EditApiKeyCommand extends Command |
|
34
|
|
|
{ |
|
35
|
|
|
use SymfonyStyleTrait; |
|
36
|
|
|
|
|
37
|
|
|
final public const NAME = 'api-key:edit'; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct( |
|
40
|
|
|
private readonly ApiKeyResource $apiKeyResource, |
|
41
|
|
|
private readonly ApiKeyHelper $apiKeyHelper, |
|
42
|
|
|
) { |
|
43
|
|
|
parent::__construct(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @noinspection PhpMissingParentCallCommonInspection |
|
48
|
|
|
* |
|
49
|
|
|
* @throws Throwable |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
52
|
|
|
{ |
|
53
|
|
|
$io = $this->getSymfonyStyle($input, $output); |
|
54
|
|
|
$apiKey = $this->apiKeyHelper->getApiKey($io, 'Which API key you want to edit?'); |
|
55
|
|
|
$message = $apiKey instanceof ApiKeyEntity ? $this->updateApiKey($input, $output, $apiKey) : null; |
|
56
|
|
|
|
|
57
|
|
|
if ($input->isInteractive()) { |
|
58
|
|
|
$io->success($message ?? ['Nothing changed - have a nice day']); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return 0; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Method to update specified API key via specified form. |
|
66
|
|
|
* |
|
67
|
|
|
* @return array<int, string> |
|
68
|
|
|
* |
|
69
|
|
|
* @throws Throwable |
|
70
|
|
|
*/ |
|
71
|
|
|
private function updateApiKey(InputInterface $input, OutputInterface $output, ApiKeyEntity $apiKey): array |
|
72
|
|
|
{ |
|
73
|
|
|
// Load entity to DTO |
|
74
|
|
|
$dtoLoaded = new ApiKeyDto(); |
|
75
|
|
|
$dtoLoaded->load($apiKey); |
|
76
|
|
|
|
|
77
|
|
|
/** @var FormHelper $helper */ |
|
78
|
|
|
$helper = $this->getHelper('form'); |
|
79
|
|
|
|
|
80
|
|
|
/** @var ApiKeyDto $dtoEdit */ |
|
81
|
|
|
$dtoEdit = $helper->interactUsingForm( |
|
82
|
|
|
ApiKeyType::class, |
|
83
|
|
|
$input, |
|
84
|
|
|
$output, |
|
85
|
|
|
[ |
|
86
|
|
|
'data' => $dtoLoaded, |
|
87
|
|
|
] |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
|
|
// Patch API key |
|
91
|
|
|
$this->apiKeyResource->patch($apiKey->getId(), $dtoEdit); |
|
92
|
|
|
|
|
93
|
|
|
return $this->apiKeyHelper->getApiKeyMessage('API key updated - have a nice day', $apiKey); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|