/
ApiKeyHelper.php
159 lines (141 loc) · 4.07 KB
/
ApiKeyHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
declare(strict_types = 1);
/**
* /src/Command/ApiKey/ApiKeyHelper.php
*
* @author TLe, Tarmo Leppänen <tarmo.leppanen@protacon.com>
*/
namespace App\Command\ApiKey;
use App\Entity\ApiKey as ApiKeyEntity;
use App\Resource\ApiKeyResource;
use App\Security\RolesService;
use Closure;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;
use function array_map;
use function implode;
use function sprintf;
/**
* Class ApiKeyHelper
*
* @package App\Command\ApiKey
* @author TLe, Tarmo Leppänen <tarmo.leppanen@protacon.com>
*/
class ApiKeyHelper
{
/**
* @var ApiKeyResource
*/
private $apiKeyResource;
/**
* @var RolesService
*/
private $rolesService;
/**
* ApiKeyHelper constructor.
*
* @param ApiKeyResource $apiKeyResource
* @param RolesService $rolesService
*/
public function __construct(ApiKeyResource $apiKeyResource, RolesService $rolesService)
{
$this->apiKeyResource = $apiKeyResource;
$this->rolesService = $rolesService;
}
/**
* Method to get API key entity. Also note that this may return a null in cases that user do not want to make any
* changes to API keys.
*
* @param SymfonyStyle $io
* @param string $question
*
* @return ApiKeyEntity|null
*
* @throws Throwable
*/
public function getApiKey(SymfonyStyle $io, string $question): ?ApiKeyEntity
{
$apiKeyFound = false;
$apiKeyEntity = null;
while ($apiKeyFound !== true) {
/** @var ApiKeyEntity|null $apiKeyEntity */
$apiKeyEntity = $this->getApiKeyEntity($io, $question);
if ($apiKeyEntity === null) {
break;
}
$message = sprintf(
'Is this the correct API key \'[%s] [%s] %s\'?',
$apiKeyEntity->getId(),
$apiKeyEntity->getToken(),
$apiKeyEntity->getDescription()
);
$apiKeyFound = (bool)$io->confirm($message, false);
}
return $apiKeyEntity ?? null;
}
/**
* Helper method to get "normalized" message for API key. This is used on following cases:
* - User changes API key token
* - User creates new API key
* - User modifies API key
* - User removes API key
*
* @param string $message
* @param ApiKeyEntity $apiKey
*
* @return array<int, string>
*/
public function getApiKeyMessage(string $message, ApiKeyEntity $apiKey): array
{
return [
$message,
sprintf(
"GUID: %s\nToken: %s",
$apiKey->getId(),
$apiKey->getToken()
),
];
}
/**
* Method to list ApiKeys where user can select desired one.
*
* @param SymfonyStyle $io
* @param string $question
*
* @return ApiKeyEntity|null
*
* @throws Throwable
*/
private function getApiKeyEntity(SymfonyStyle $io, string $question): ?ApiKeyEntity
{
$choices = [];
$iterator = $this->getApiKeyIterator($choices);
array_map($iterator, $this->apiKeyResource->find([], ['token' => 'ASC']));
$choices['Exit'] = 'Exit command';
return $this->apiKeyResource->findOne((string)$io->choice($question, $choices));
}
/**
* Method to return ApiKeyIterator closure. This will format ApiKey entities for choice list.
*
* @param string[] $choices
*
* @return Closure
*/
private function getApiKeyIterator(array &$choices): Closure
{
/**
* Lambda function create api key choices
*
* @param ApiKeyEntity $apiKey
*/
return function (ApiKeyEntity $apiKey) use (&$choices): void {
$value = sprintf(
'[%s] %s - Roles: %s',
$apiKey->getToken(),
$apiKey->getDescription(),
implode(', ', $this->rolesService->getInheritedRoles($apiKey->getRoles()))
);
$choices[$apiKey->getId()] = $value;
};
}
}