| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Jose\Component\Console; |
| 6 | |
| 7 | use InvalidArgumentException; |
| 8 | use Jose\Component\KeyManagement\X5UFactory; |
| 9 | use Symfony\Component\Console\Attribute\AsCommand; |
| 10 | use Symfony\Component\Console\Input\InputArgument; |
| 11 | use Symfony\Component\Console\Input\InputInterface; |
| 12 | use Symfony\Component\Console\Output\OutputInterface; |
| 13 | use function is_string; |
| 14 | |
| 15 | #[AsCommand(name: 'keyset:load:x5u', description: 'Loads a key set from an url.',)] |
| 16 | final class X5ULoaderCommand extends ObjectOutputCommand |
| 17 | { |
| 18 | protected static $defaultName = 'keyset:load:x5u'; |
| 19 | |
| 20 | protected static $defaultDescription = 'Loads a key set from an url.'; |
| 21 | |
| 22 | public function __construct( |
| 23 | private readonly X5UFactory $x5uFactory, |
| 24 | ?string $name = null |
| 25 | ) { |
| 26 | parent::__construct($name); |
| 27 | } |
| 28 | |
| 29 | protected function configure(): void |
| 30 | { |
| 31 | parent::configure(); |
| 32 | $this->setHelp( |
| 33 | 'This command will try to get a key set from an URL. The distant key set is list of X.509 certificates.' |
| 34 | ) |
| 35 | ->addArgument('url', InputArgument::REQUIRED, 'The URL'); |
| 36 | } |
| 37 | |
| 38 | protected function execute(InputInterface $input, OutputInterface $output): int |
| 39 | { |
| 40 | $url = $input->getArgument('url'); |
| 41 | if (! is_string($url)) { |
| 42 | throw new InvalidArgumentException('Invalid URL'); |
| 43 | } |
| 44 | $result = $this->x5uFactory->loadFromUrl($url); |
| 45 | $this->prepareJsonOutput($input, $output, $result); |
| 46 | |
| 47 | return self::SUCCESS; |
| 48 | } |
| 49 | } |