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

Commit b893f22

Browse files
committed
Added extra token commands
1 parent cc0cd46 commit b893f22

File tree

6 files changed

+288
-5
lines changed

6 files changed

+288
-5
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.17-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":{"Twig\/MethodAccessorExtension.php":1937766330,"Twig\/AggregationTranslationExtension.php":1168064907,"Twig\/UrlBuilderExtension.php":1238295593,"DependencyInjection\/CompilerPass\/ExporterCompilerPass.php":3859400892,"DependencyInjection\/CompilerPass\/ReadTransformerCompilerPass.php":407060565,"DependencyInjection\/CompilerPass\/WriteTransformerCompilerPass.php":3787221228,"DependencyInjection\/CompilerPass\/RepositoryCompilerPass.php":3492158054,"DependencyInjection\/CompilerPass\/TagCompilerPass.php":1857409043,"DependencyInjection\/ApisearchExtension.php":2861843414,"DependencyInjection\/ApisearchConfiguration.php":2562035961,"ApisearchBundle.php":452340142,"Translator\/AggregationTranslator.php":2950948888,"Command\/ExportIndexCommand.php":3751299339,"Command\/ImportIndexCommand.php":1722481554,"Command\/PrintTokensCommand.php":3313771574,"Command\/ApisearchCommand.php":1883682797,"Command\/ApisearchFormattedCommand.php":894019063,"Command\/WithRepositoryBucketCommand.php":4111986054,"Command\/WithAppRepositoryBucketCommand.php":4212053559,"Command\/ResetIndexCommand.php":4289827141,"Tests\/Functional\/DependencyInjection\/ApisearchConfigurationTest.php":379272062}}
1+
{"php":"7.1.17-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":{"Twig\/MethodAccessorExtension.php":1937766330,"Twig\/AggregationTranslationExtension.php":1168064907,"Twig\/UrlBuilderExtension.php":1238295593,"DependencyInjection\/CompilerPass\/ExporterCompilerPass.php":3859400892,"DependencyInjection\/CompilerPass\/ReadTransformerCompilerPass.php":407060565,"DependencyInjection\/CompilerPass\/WriteTransformerCompilerPass.php":3787221228,"DependencyInjection\/CompilerPass\/RepositoryCompilerPass.php":3492158054,"DependencyInjection\/CompilerPass\/TagCompilerPass.php":1857409043,"DependencyInjection\/ApisearchExtension.php":2861843414,"DependencyInjection\/ApisearchConfiguration.php":2562035961,"ApisearchBundle.php":452340142,"Translator\/AggregationTranslator.php":2950948888,"Command\/ExportIndexCommand.php":3751299339,"Command\/ImportIndexCommand.php":1722481554,"Command\/PrintTokensCommand.php":3931081773,"Command\/ApisearchCommand.php":1883682797,"Command\/ApisearchFormattedCommand.php":2617090809,"Command\/WithRepositoryBucketCommand.php":835091977,"Command\/WithAppRepositoryBucketCommand.php":3329637861,"Command\/ResetIndexCommand.php":4289827141,"Tests\/Functional\/DependencyInjection\/ApisearchConfigurationTest.php":379272062,"Command\/AddTokenCommand.php":4146793222,"Command\/DeleteTokenCommand.php":462070402}}

Command/AddTokenCommand.php

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Search PHP Bundle.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Feel free to edit as you please, and have fun.
10+
*
11+
* @author Marc Morera <yuhu@mmoreram.com>
12+
* @author PuntMig Technologies
13+
*/
14+
15+
declare(strict_types=1);
16+
17+
namespace Apisearch\Command;
18+
19+
use Apisearch\Token\Token;
20+
use Apisearch\Token\TokenUUID;
21+
use Symfony\Component\Console\Input\InputArgument;
22+
use Symfony\Component\Console\Input\InputInterface;
23+
use Symfony\Component\Console\Input\InputOption;
24+
use Symfony\Component\Console\Output\OutputInterface;
25+
26+
/**
27+
* Class AddTokenCommand.
28+
*/
29+
class AddTokenCommand extends WithAppRepositoryBucketCommand
30+
{
31+
/**
32+
* Configures the current command.
33+
*/
34+
protected function configure()
35+
{
36+
$this
37+
->setName('apisearch:add-token')
38+
->setDescription('Add a token')
39+
->addArgument(
40+
'uuid',
41+
InputArgument::REQUIRED,
42+
'UUID'
43+
)
44+
->addArgument(
45+
'repository',
46+
InputArgument::REQUIRED,
47+
'Repository name'
48+
)
49+
->addOption(
50+
'index',
51+
null,
52+
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
53+
'Indices',
54+
[]
55+
)
56+
->addOption(
57+
'http-referrer',
58+
null,
59+
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
60+
'Http referrers',
61+
[]
62+
)
63+
->addOption(
64+
'endpoint',
65+
null,
66+
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
67+
'Endpoints',
68+
[]
69+
)
70+
->addOption(
71+
'plugin',
72+
null,
73+
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
74+
'Plugins',
75+
[]
76+
)
77+
->addOption(
78+
'seconds-valid',
79+
null,
80+
InputOption::VALUE_OPTIONAL,
81+
'Seconds valid',
82+
Token::INFINITE_DURATION
83+
)
84+
->addOption(
85+
'max-hits-per-query',
86+
null,
87+
InputOption::VALUE_OPTIONAL,
88+
'Maximum hits per query',
89+
Token::INFINITE_HITS_PER_QUERY
90+
)
91+
->addOption(
92+
'ttl',
93+
null,
94+
InputOption::VALUE_OPTIONAL,
95+
'TTL',
96+
Token::DEFAULT_TTL
97+
);
98+
}
99+
100+
/**
101+
* Dispatch domain event.
102+
*
103+
* @return string
104+
*/
105+
protected function getHeader(): string
106+
{
107+
return 'Add token';
108+
}
109+
110+
/**
111+
* Get success message.
112+
*
113+
* @param InputInterface $input
114+
* @param mixed $result
115+
*
116+
* @return string
117+
*/
118+
protected function getSuccessMessage(
119+
InputInterface $input,
120+
$result
121+
): string {
122+
return sprintf(
123+
'Token with UUID <%s> added properly',
124+
$input->getArgument('uuid')
125+
);
126+
}
127+
128+
/**
129+
* Dispatch domain event.
130+
*
131+
* @param InputInterface $input
132+
* @param OutputInterface $output
133+
*
134+
* @return mixed|null
135+
*/
136+
protected function runCommand(InputInterface $input, OutputInterface $output)
137+
{
138+
$repository = $input->getArgument('repository');
139+
$indexArray = $this
140+
->repositoryBucket
141+
->getConfiguration()[$repository]['indexes'] ?? [];
142+
143+
$indices = array_map(function (string $index) use ($indexArray) {
144+
return $indexArray[$index] ?? null;
145+
}, $input->getOption('index'));
146+
$indices = array_filter($indices);
147+
148+
$this
149+
->repositoryBucket->findRepository($repository)
150+
->addToken(
151+
new Token(
152+
TokenUUID::createById($input->getArgument('uuid')),
153+
(string) $input->getArgument('repository'),
154+
$indices,
155+
$input->getOption('http-referrer'),
156+
$input->getOption('endpoint'),
157+
$input->getOption('plugin'),
158+
(int) $input->getOption('seconds-valid'),
159+
(int) $input->getOption('max-hits-per-query'),
160+
(int) $input->getOption('ttl')
161+
)
162+
);
163+
}
164+
}

Command/DeleteTokenCommand.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Search PHP Bundle.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Feel free to edit as you please, and have fun.
10+
*
11+
* @author Marc Morera <yuhu@mmoreram.com>
12+
* @author PuntMig Technologies
13+
*/
14+
15+
declare(strict_types=1);
16+
17+
namespace Apisearch\Command;
18+
19+
use Apisearch\Token\TokenUUID;
20+
use Symfony\Component\Console\Input\InputArgument;
21+
use Symfony\Component\Console\Input\InputInterface;
22+
use Symfony\Component\Console\Output\OutputInterface;
23+
24+
/**
25+
* Class DeleteTokenCommand.
26+
*/
27+
class DeleteTokenCommand extends WithAppRepositoryBucketCommand
28+
{
29+
/**
30+
* Configures the current command.
31+
*/
32+
protected function configure()
33+
{
34+
$this
35+
->setName('apisearch:delete-token')
36+
->setDescription('Delete a token')
37+
->addArgument(
38+
'uuid',
39+
InputArgument::REQUIRED,
40+
'UUID'
41+
)
42+
->addArgument(
43+
'repository',
44+
InputArgument::REQUIRED,
45+
'Repository Name'
46+
);
47+
}
48+
49+
/**
50+
* Dispatch domain event.
51+
*
52+
* @return string
53+
*/
54+
protected function getHeader(): string
55+
{
56+
return 'Create token';
57+
}
58+
59+
/**
60+
* Get success message.
61+
*
62+
* @param InputInterface $input
63+
* @param mixed $result
64+
*
65+
* @return string
66+
*/
67+
protected function getSuccessMessage(
68+
InputInterface $input,
69+
$result
70+
): string {
71+
return sprintf(
72+
'Token with UUID <%s> added properly',
73+
$input->getArgument('uuid')
74+
);
75+
}
76+
77+
/**
78+
* Dispatch domain event.
79+
*
80+
* @param InputInterface $input
81+
* @param OutputInterface $output
82+
*
83+
* @return mixed|null
84+
*/
85+
protected function runCommand(InputInterface $input, OutputInterface $output)
86+
{
87+
$repository = $input->getArgument('repository');
88+
$this
89+
->repositoryBucket->findRepository($repository)
90+
->deleteToken(
91+
TokenUUID::createById($input->getArgument('uuid'))
92+
);
93+
}
94+
}

Command/PrintTokensCommand.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,30 @@ protected function getSuccessMessage(
7777
*/
7878
protected function runCommand(InputInterface $input, OutputInterface $output)
7979
{
80+
$repository = $input->getArgument('repository');
8081
$tokens = $this
81-
->repositoryBucket->findRepository(
82-
$input->getArgument('repository')
83-
)
82+
->repositoryBucket->findRepository($repository)
8483
->getTokens();
8584

85+
$indexArray = $this
86+
->repositoryBucket
87+
->getConfiguration()[$repository]['indexes'] ?? [];
88+
8689
/**
8790
* @var Token
8891
*/
8992
$table = new Table($output);
9093
$table->setHeaders(['UUID', 'Indices', 'Seconds Valid', 'Max hits per query', 'HTTP Referrers', 'endpoints', 'plugins', 'ttl']);
9194
foreach ($tokens as $token) {
95+
$indicesReversed = array_flip($indexArray);
96+
$indices = array_map(function (string $index) use ($indicesReversed) {
97+
return $indicesReversed[$index] ?? null;
98+
}, $token->getIndices());
99+
$indices = array_filter($indices);
100+
92101
$table->addRow([
93102
$token->getTokenUUID()->composeUUID(),
94-
implode(', ', $token->getIndices()),
103+
implode(', ', $indices),
95104
$token->getSecondsValid(),
96105
$token->getMaxHitsPerQuery(),
97106
implode(', ', $token->getHttpReferrers()),

Resources/config/commands.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ services:
2626

2727
apisearch.print_tokens_command:
2828
class: Apisearch\Command\PrintTokensCommand
29+
arguments:
30+
- "@apisearch.app_repository_bucket"
31+
tags:
32+
- { name: console.command }
33+
34+
apisearch.add_token_command:
35+
class: Apisearch\Command\AddTokenCommand
36+
arguments:
37+
- "@apisearch.app_repository_bucket"
38+
tags:
39+
- { name: console.command }
40+
41+
apisearch.delete_token_command:
42+
class: Apisearch\Command\DeleteTokenCommand
2943
arguments:
3044
- "@apisearch.app_repository_bucket"
3145
tags:

Resources/config/repositories.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ services:
55

66
apisearch.app_repository_bucket:
77
class: Apisearch\App\AppRepositoryBucket
8+
arguments:
9+
- "%apisearch.repository_configuration%"
810

911
apisearch.user_repository_bucket:
1012
class: Apisearch\User\UserRepositoryBucket

0 commit comments

Comments
 (0)