|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Api\App\Command; |
| 6 | + |
| 7 | +use Api\App\Service\ErrorReportServiceInterface; |
| 8 | +use Exception; |
| 9 | +use Symfony\Component\Console\Command\Command; |
| 10 | +use Symfony\Component\Console\Input\InputArgument; |
| 11 | +use Symfony\Component\Console\Input\InputInterface; |
| 12 | +use Symfony\Component\Console\Output\OutputInterface; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class TokenGenerateCommand |
| 16 | + * @package Api\App\Command |
| 17 | + */ |
| 18 | +class TokenGenerateCommand extends Command |
| 19 | +{ |
| 20 | + protected static $defaultName = 'token:generate'; |
| 21 | + private string $typeErrorReporting = 'error-reporting'; |
| 22 | + private ErrorReportServiceInterface $errorReportService; |
| 23 | + |
| 24 | + /** |
| 25 | + * TokenGenerateCommand constructor. |
| 26 | + */ |
| 27 | + public function __construct(ErrorReportServiceInterface $errorReportService) |
| 28 | + { |
| 29 | + parent::__construct(self::$defaultName); |
| 30 | + $this->errorReportService = $errorReportService; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @return void |
| 35 | + */ |
| 36 | + protected function configure(): void |
| 37 | + { |
| 38 | + $this |
| 39 | + ->setName(self::$defaultName) |
| 40 | + ->setDescription('Generic token generator.') |
| 41 | + ->addArgument('type', InputArgument::REQUIRED, 'The type of token to be generated.') |
| 42 | + ->addUsage($this->typeErrorReporting) |
| 43 | + ->setHelp(<<<MSG |
| 44 | +<info>%command.name%</info> is a multipurpose command that allows creating tokens required by different parts of the API. |
| 45 | +
|
| 46 | +Usage: |
| 47 | +1. Create token for the error reporting endpoint: |
| 48 | +* run: <info>%command.full_name% {$this->typeErrorReporting}</info> |
| 49 | +* copy the generated token |
| 50 | +* open <comment>config/autoload/error-handling.global.php</comment> |
| 51 | +* paste the copied string inside the <comment>tokens</comment> array found under the <comment>ErrorReportServiceInterface::class</comment> key. |
| 52 | +MSG |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @param InputInterface $input |
| 58 | + * @param OutputInterface $output |
| 59 | + * @return int |
| 60 | + * @throws Exception |
| 61 | + */ |
| 62 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 63 | + { |
| 64 | + $type = $input->getArgument('type'); |
| 65 | + match($type) { |
| 66 | + $this->typeErrorReporting => $this->generateErrorReportingToken($output), |
| 67 | + default => throw new Exception( |
| 68 | + sprintf('Unknown token type: %s', $type) |
| 69 | + ) |
| 70 | + }; |
| 71 | + |
| 72 | + return Command::SUCCESS; |
| 73 | + } |
| 74 | + |
| 75 | + private function generateErrorReportingToken(OutputInterface $output): void |
| 76 | + { |
| 77 | + $token = $this->errorReportService->generateToken(); |
| 78 | + |
| 79 | + $output->writeln(<<<MSG |
| 80 | +Error reporting token: |
| 81 | +
|
| 82 | + <info>{$token}</info> |
| 83 | +
|
| 84 | +* copy the generated token |
| 85 | +* open <comment>config/autoload/error-handling.global.php</comment> |
| 86 | +* paste the copied string inside the <comment>tokens</comment> array found under the <comment>ErrorReportServiceInterface::class</comment> key. |
| 87 | +MSG |
| 88 | +); |
| 89 | + } |
| 90 | +} |
0 commit comments