Skip to content
This repository was archived by the owner on Jun 25, 2025. It is now read-only.

Commit b4898b2

Browse files
committed
Improved some commands
- Add token - Delete token/s - Delete index
1 parent 75c5a4a commit b4898b2

File tree

8 files changed

+101
-33
lines changed

8 files changed

+101
-33
lines changed

.php_cs.cache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"php":"7.1.12-1+ubuntu16.04.1+deb.sury.org+1","version":"2.11.1:v2.11.1#ad94441c17b8ef096e517acccdbf3238af8a2da8","rules":{"blank_line_after_namespace":true,"braces":{"allow_single_line_closure":true},"class_definition":{"singleLine":true},"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_constants":true,"lowercase_keywords":true,"method_argument_space":true,"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":true,"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true,"binary_operator_spaces":true,"blank_line_after_opening_tag":true,"blank_line_before_statement":{"statements":["return"]},"cast_spaces":true,"class_attributes_separation":{"elements":["method"]},"concat_space":{"spacing":"none"},"declare_equal_normalize":true,"function_typehint_space":true,"include":true,"increment_style":true,"lowercase_cast":true,"magic_constant_casing":true,"native_function_casing":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_blank_lines_after_phpdoc":true,"no_empty_comment":true,"no_empty_phpdoc":true,"no_empty_statement":true,"no_extra_blank_lines":{"tokens":["curly_brace_block","extra","parenthesis_brace_block","square_brace_block","throw","use"]},"no_leading_import_slash":true,"no_leading_namespace_whitespace":true,"no_mixed_echo_print":{"use":"echo"},"no_multiline_whitespace_around_double_arrow":true,"no_short_bool_cast":true,"no_singleline_whitespace_before_semicolons":true,"no_spaces_around_offset":true,"no_trailing_comma_in_list_call":true,"no_trailing_comma_in_singleline_array":true,"no_unneeded_control_parentheses":true,"no_unneeded_curly_braces":true,"no_unneeded_final_method":true,"no_unused_imports":true,"no_whitespace_before_comma_in_array":true,"no_whitespace_in_blank_line":true,"normalize_index_brace":true,"object_operator_without_whitespace":true,"php_unit_fqcn_annotation":true,"phpdoc_align":{"tags":["method","param","property","return","throws","type","var"]},"phpdoc_annotation_without_dot":true,"phpdoc_indent":true,"phpdoc_inline_tag":true,"phpdoc_no_access":true,"phpdoc_no_alias_tag":true,"phpdoc_no_empty_return":true,"phpdoc_no_package":true,"phpdoc_no_useless_inheritdoc":true,"phpdoc_return_self_reference":true,"phpdoc_scalar":true,"phpdoc_separation":true,"phpdoc_single_line_var_spacing":true,"phpdoc_summary":true,"phpdoc_to_comment":true,"phpdoc_trim":true,"phpdoc_types":true,"phpdoc_var_without_name":true,"protected_to_private":true,"return_type_declaration":true,"semicolon_after_instruction":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_line_comment_style":{"comment_types":["hash"]},"single_quote":true,"space_after_semicolon":{"remove_in_empty_for_expressions":true},"standardize_increment":true,"standardize_not_equals":true,"ternary_operator_spaces":true,"trailing_comma_in_multiline_array":true,"trim_array_spaces":true,"unary_operator_spaces":true,"whitespace_after_comma_in_array":true,"yoda_style":true},"hashes":{"Tests\/Functional\/DependencyInjection\/ApisearchConfigurationTest.php":379272062,"Translator\/AggregationTranslator.php":2950948888,"ApisearchBundle.php":452340142,"Twig\/MethodAccessorExtension.php":1937766330,"Twig\/AggregationTranslationExtension.php":1168064907,"Twig\/UrlBuilderExtension.php":1238295593,"Command\/ImportIndexCommand.php":1722481554,"Command\/CreateIndexCommand.php":364387858,"Command\/ApisearchFormattedCommand.php":2617090809,"Command\/ResetIndexCommand.php":4289827141,"Command\/DeleteIndexCommand.php":2797453099,"Command\/ApisearchCommand.php":1883682797,"Command\/WithAppRepositoryBucketCommand.php":3329637861,"Command\/AddTokenCommand.php":3164274201,"Command\/PrintTokensCommand.php":2265717038,"Command\/ExportIndexCommand.php":3751299339,"Command\/WithRepositoryBucketCommand.php":835091977,"Command\/DeleteTokenCommand.php":1541156174,"DependencyInjection\/CompilerPass\/TagCompilerPass.php":1857409043,"DependencyInjection\/CompilerPass\/WriteTransformerCompilerPass.php":3787221228,"DependencyInjection\/CompilerPass\/ExporterCompilerPass.php":3859400892,"DependencyInjection\/CompilerPass\/RepositoryCompilerPass.php":3492158054,"DependencyInjection\/CompilerPass\/ReadTransformerCompilerPass.php":407060565,"DependencyInjection\/ApisearchConfiguration.php":2562035961,"DependencyInjection\/ApisearchExtension.php":2861843414,"Command\/DeleteTokensCommand.php":1536207707}}
1+
{"php":"7.1.12-1+ubuntu16.04.1+deb.sury.org+1","version":"2.11.1:v2.11.1#ad94441c17b8ef096e517acccdbf3238af8a2da8","rules":{"blank_line_after_namespace":true,"braces":{"allow_single_line_closure":true},"class_definition":{"singleLine":true},"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_constants":true,"lowercase_keywords":true,"method_argument_space":true,"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":true,"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true,"binary_operator_spaces":true,"blank_line_after_opening_tag":true,"blank_line_before_statement":{"statements":["return"]},"cast_spaces":true,"class_attributes_separation":{"elements":["method"]},"concat_space":{"spacing":"none"},"declare_equal_normalize":true,"function_typehint_space":true,"include":true,"increment_style":true,"lowercase_cast":true,"magic_constant_casing":true,"native_function_casing":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_blank_lines_after_phpdoc":true,"no_empty_comment":true,"no_empty_phpdoc":true,"no_empty_statement":true,"no_extra_blank_lines":{"tokens":["curly_brace_block","extra","parenthesis_brace_block","square_brace_block","throw","use"]},"no_leading_import_slash":true,"no_leading_namespace_whitespace":true,"no_mixed_echo_print":{"use":"echo"},"no_multiline_whitespace_around_double_arrow":true,"no_short_bool_cast":true,"no_singleline_whitespace_before_semicolons":true,"no_spaces_around_offset":true,"no_trailing_comma_in_list_call":true,"no_trailing_comma_in_singleline_array":true,"no_unneeded_control_parentheses":true,"no_unneeded_curly_braces":true,"no_unneeded_final_method":true,"no_unused_imports":true,"no_whitespace_before_comma_in_array":true,"no_whitespace_in_blank_line":true,"normalize_index_brace":true,"object_operator_without_whitespace":true,"php_unit_fqcn_annotation":true,"phpdoc_align":{"tags":["method","param","property","return","throws","type","var"]},"phpdoc_annotation_without_dot":true,"phpdoc_indent":true,"phpdoc_inline_tag":true,"phpdoc_no_access":true,"phpdoc_no_alias_tag":true,"phpdoc_no_empty_return":true,"phpdoc_no_package":true,"phpdoc_no_useless_inheritdoc":true,"phpdoc_return_self_reference":true,"phpdoc_scalar":true,"phpdoc_separation":true,"phpdoc_single_line_var_spacing":true,"phpdoc_summary":true,"phpdoc_to_comment":true,"phpdoc_trim":true,"phpdoc_types":true,"phpdoc_var_without_name":true,"protected_to_private":true,"return_type_declaration":true,"semicolon_after_instruction":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_line_comment_style":{"comment_types":["hash"]},"single_quote":true,"space_after_semicolon":{"remove_in_empty_for_expressions":true},"standardize_increment":true,"standardize_not_equals":true,"ternary_operator_spaces":true,"trailing_comma_in_multiline_array":true,"trim_array_spaces":true,"unary_operator_spaces":true,"whitespace_after_comma_in_array":true,"yoda_style":true},"hashes":{"Tests\/Functional\/DependencyInjection\/ApisearchConfigurationTest.php":379272062,"Translator\/AggregationTranslator.php":2950948888,"ApisearchBundle.php":452340142,"Twig\/MethodAccessorExtension.php":1937766330,"Twig\/AggregationTranslationExtension.php":1168064907,"Twig\/UrlBuilderExtension.php":1238295593,"Command\/ImportIndexCommand.php":1722481554,"Command\/CreateIndexCommand.php":364387858,"Command\/ApisearchFormattedCommand.php":3372612923,"Command\/ResetIndexCommand.php":4289827141,"Command\/DeleteIndexCommand.php":872001448,"Command\/ApisearchCommand.php":1200461271,"Command\/WithAppRepositoryBucketCommand.php":3721529541,"Command\/AddTokenCommand.php":2707377316,"Command\/PrintTokensCommand.php":2265717038,"Command\/ExportIndexCommand.php":3751299339,"Command\/WithRepositoryBucketCommand.php":835091977,"Command\/DeleteTokenCommand.php":2469160876,"DependencyInjection\/CompilerPass\/TagCompilerPass.php":1857409043,"DependencyInjection\/CompilerPass\/WriteTransformerCompilerPass.php":3787221228,"DependencyInjection\/CompilerPass\/ExporterCompilerPass.php":3859400892,"DependencyInjection\/CompilerPass\/RepositoryCompilerPass.php":3492158054,"DependencyInjection\/CompilerPass\/ReadTransformerCompilerPass.php":407060565,"DependencyInjection\/ApisearchConfiguration.php":2562035961,"DependencyInjection\/ApisearchExtension.php":2861843414,"Command\/DeleteTokensCommand.php":2095195833}}

Command/AddTokenCommand.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,25 +137,18 @@ protected function getSuccessMessage(
137137
*/
138138
protected function runCommand(InputInterface $input, OutputInterface $output)
139139
{
140-
$repository = $input->getArgument('repository');
141-
$indexArray = $this
142-
->repositoryBucket
143-
->getConfiguration()[$repository]['indexes'] ?? [];
144-
145-
$indices = array_map(function (string $index) use ($indexArray) {
146-
return $indexArray[$index] ?? null;
147-
}, $input->getOption('index'));
148-
$indices = array_filter($indices);
140+
list($app_id, $indices) = $this->getRepositoryAndIndices($input, $output);
141+
$endpoints = $this->getEndpoints($input, $output);
149142

150143
$this
151-
->repositoryBucket->findRepository($repository)
144+
->repositoryBucket->findRepository($input->getArgument('repository'))
152145
->addToken(
153146
new Token(
154147
TokenUUID::createById($input->getArgument('uuid')),
155-
(string) $input->getArgument('repository'),
148+
(string) $app_id,
156149
$indices,
157150
$input->getOption('http-referrer'),
158-
$input->getOption('endpoint'),
151+
$endpoints,
159152
$input->getOption('plugin'),
160153
(int) $input->getOption('seconds-valid'),
161154
(int) $input->getOption('max-hits-per-query'),

Command/ApisearchCommand.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616

1717
namespace Apisearch\Command;
1818

19+
use Apisearch\Http\Endpoints;
20+
use Exception;
1921
use Symfony\Component\Console\Command\Command;
2022
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
23+
use Symfony\Component\Console\Input\InputInterface;
2124
use Symfony\Component\Console\Output\OutputInterface;
2225
use Symfony\Component\Stopwatch\Stopwatch;
2326
use Symfony\Component\Stopwatch\StopwatchEvent;
@@ -261,6 +264,33 @@ protected function stopStopWatch($eventName)
261264
->stop($eventName);
262265
}
263266

267+
/**
268+
* Get endpoint compositions given their names.
269+
*
270+
* @param InputInterface $input
271+
* @param OutputInterface $output
272+
*
273+
* @return array
274+
*
275+
* @throws Exception
276+
*/
277+
protected function getEndpoints(
278+
InputInterface $input,
279+
OutputInterface $output
280+
): array {
281+
$endpointsName = $input->getOption('endpoint');
282+
$endpoints = Endpoints::compose($endpointsName);
283+
284+
if (count($endpointsName) > count($endpoints)) {
285+
throw new Exception(sprintf(
286+
'Endpoint not found. Endpoints available: %s',
287+
implode(', ', array_keys(Endpoints::all()))
288+
));
289+
}
290+
291+
return $endpoints;
292+
}
293+
264294
/**
265295
* Get project header.
266296
*

Command/ApisearchFormattedCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
4343
$input,
4444
$output
4545
);
46+
4647
$successfulMessage = $this->getSuccessMessage($input, $result);
4748
if (!empty($successfulMessage)) {
4849
$this->printMessage(

Command/DeleteIndexCommand.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ protected function configure()
101101
*/
102102
protected function getHeader(): string
103103
{
104-
return 'Create index';
104+
return 'Delete index';
105105
}
106106

107107
/**
@@ -131,15 +131,15 @@ protected function runCommand(InputInterface $input, OutputInterface $output)
131131
}
132132

133133
if ($input->getOption('with-events')) {
134-
$this->createEvents(
134+
$this->deleteEvents(
135135
$repository,
136136
$index,
137137
$output
138138
);
139139
}
140140

141141
if ($input->getOption('with-logs')) {
142-
$this->createLogs(
142+
$this->deleteLogs(
143143
$repository,
144144
$index,
145145
$output
@@ -159,25 +159,25 @@ protected function getSuccessMessage(
159159
InputInterface $input,
160160
$result
161161
): string {
162-
return 'Indices created properly';
162+
return 'Indices deleted properly';
163163
}
164164

165165
/**
166-
* Create events index.
166+
* Delete events index.
167167
*
168-
* @param string $repositoryName
168+
* @param string $repository
169169
* @param string $index
170170
* @param OutputInterface $output
171171
*/
172-
private function createEvents(
173-
string $repositoryName,
172+
protected function deleteEvents(
173+
string $repository,
174174
string $index,
175175
OutputInterface $output
176176
) {
177177
try {
178178
$this
179179
->eventRepositoryBucket
180-
->findRepository($repositoryName, $index)
180+
->findRepository($repository, $index)
181181
->deleteIndex();
182182
} catch (ResourceNotAvailableException $exception) {
183183
$this->printInfoMessage(
@@ -189,21 +189,21 @@ private function createEvents(
189189
}
190190

191191
/**
192-
* Create logs index.
192+
* Delete logs index.
193193
*
194-
* @param string $repositoryName
194+
* @param string $repository
195195
* @param string $index
196196
* @param OutputInterface $output
197197
*/
198-
private function createLogs(
199-
string $repositoryName,
198+
protected function deleteLogs(
199+
string $repository,
200200
string $index,
201201
OutputInterface $output
202202
) {
203203
try {
204204
$this
205205
->logRepositoryBucket
206-
->findRepository($repositoryName, $index)
206+
->findRepository($repository, $index)
207207
->deleteIndex();
208208
} catch (ResourceNotAvailableException $exception) {
209209
$this->printInfoMessage(

Command/DeleteTokenCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@ protected function getSuccessMessage(
8484
*/
8585
protected function runCommand(InputInterface $input, OutputInterface $output)
8686
{
87-
$repository = $input->getArgument('repository');
87+
$this->getRepositoryAndIndices($input, $output);
88+
8889
$this
89-
->repositoryBucket->findRepository($repository)
90+
->repositoryBucket->findRepository($input->getArgument('repository'))
9091
->deleteToken(
9192
TokenUUID::createById($input->getArgument('uuid'))
9293
);

Command/DeleteTokensCommand.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,6 @@ protected function getSuccessMessage(
7878
InputInterface $input,
7979
$result
8080
): string {
81-
return sprintf(
82-
'All Tokens deleted properly',
83-
$input->getArgument('uuid')
84-
);
81+
return 'All Tokens deleted properly';
8582
}
8683
}

Command/WithAppRepositoryBucketCommand.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
namespace Apisearch\Command;
1818

1919
use Apisearch\App\AppRepositoryBucket;
20+
use Exception;
21+
use Symfony\Component\Console\Input\InputInterface;
22+
use Symfony\Component\Console\Output\OutputInterface;
2023

2124
/**
2225
* Class WithAppRepositoryBucketCommand.
@@ -41,4 +44,47 @@ public function __construct(AppRepositoryBucket $repositoryBucket)
4144

4245
$this->repositoryBucket = $repositoryBucket;
4346
}
47+
48+
/**
49+
* Check repository and indices existence.
50+
*
51+
* @param InputInterface $input
52+
* @param OutputInterface $output
53+
*
54+
* @return array
55+
*
56+
* @throws Exception
57+
*/
58+
protected function getRepositoryAndIndices(
59+
InputInterface $input,
60+
OutputInterface $output
61+
): array {
62+
$repository = $input->getArgument('repository');
63+
$configuration = $this->repositoryBucket->getConfiguration();
64+
65+
if (!isset($configuration[$repository])) {
66+
throw new Exception(sprintf('Repository %s not found under apisearch configuration', $repository));
67+
}
68+
69+
if (!$input->hasOption('index')) {
70+
return [$configuration[$repository]['app_id'], []];
71+
}
72+
73+
$indexArray = $configuration[$repository]['indexes'] ?? [];
74+
75+
$indices = array_map(function (string $index) use ($indexArray, $repository, $output) {
76+
if (!isset($indexArray[$index])) {
77+
throw new Exception(sprintf(
78+
'Index %s not found under %s repository. Indices availables: %s',
79+
$index,
80+
$repository,
81+
implode(', ', array_keys($indexArray))
82+
));
83+
}
84+
85+
return $indexArray[$index];
86+
}, $input->getOption('index'));
87+
88+
return [$configuration[$repository]['app_id'], $indices];
89+
}
4490
}

0 commit comments

Comments
 (0)