Debian Codegraph @

lib/3rdParty/composer/web-token/jwt-library/Console/AddKeyIntoKeysetCommand.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Jose\Component\Console;
6
7 use InvalidArgumentException;
8 use Jose\Component\Core\JWK;
9 use Jose\Component\Core\JWKSet;
10 use Jose\Component\Core\Util\JsonConverter;
11 use Symfony\Component\Console\Attribute\AsCommand;
12 use Symfony\Component\Console\Input\InputArgument;
13 use Symfony\Component\Console\Input\InputInterface;
14 use Symfony\Component\Console\Output\OutputInterface;
15 use function is_array;
16 use function is_string;
17
18 #[AsCommand(name: 'keyset:add:key', description: 'Add a key into a key set.',)]
19 final class AddKeyIntoKeysetCommand extends ObjectOutputCommand
20 {
21 protected static $defaultName = 'keyset:add:key';
22
23 protected static $defaultDescription = 'Add a key into a key set.';
24
25 protected function configure(): void
26 {
27 parent::configure();
28 $this->setHelp('This command adds a key at the end of a key set.')
29 ->addArgument('jwkset', InputArgument::REQUIRED, 'The JWKSet object')
30 ->addArgument('jwk', InputArgument::REQUIRED, 'The new JWK object');
31 }
32
33 protected function execute(InputInterface $input, OutputInterface $output): int
34 {
35 $jwkset = $this->getKeyset($input);
36 $jwk = $this->getKey($input);
37 $jwkset = $jwkset->with($jwk);
38 $this->prepareJsonOutput($input, $output, $jwkset);
39
40 return self::SUCCESS;
41 }
42
43 private function getKeyset(InputInterface $input): JWKSet
44 {
45 $jwkset = $input->getArgument('jwkset');
46 if (! is_string($jwkset)) {
47 throw new InvalidArgumentException('The argument must be a valid JWKSet.');
48 }
49 $json = JsonConverter::decode($jwkset);
50 if (! is_array($json)) {
51 throw new InvalidArgumentException('The argument must be a valid JWKSet.');
52 }
53
54 return JWKSet::createFromKeyData($json);
55 }
56
57 private function getKey(InputInterface $input): JWK
58 {
59 $jwk = $input->getArgument('jwk');
60 if (! is_string($jwk)) {
61 throw new InvalidArgumentException('The argument must be a valid JWK.');
62 }
63 $json = JsonConverter::decode($jwk);
64 if (! is_array($json)) {
65 throw new InvalidArgumentException('The argument must be a valid JWK.');
66 }
67
68 return new JWK($json);
69 }
70 }