MergeKeysetCommand::execute()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 4
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Component\Console;
15
16
use InvalidArgumentException;
17
use Jose\Component\Core\JWKSet;
18
use Jose\Component\Core\Util\JsonConverter;
19
use Symfony\Component\Console\Input\InputArgument;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
final class MergeKeysetCommand extends ObjectOutputCommand
24
{
25
    protected function configure(): void
26
    {
27
        parent::configure();
28
        $this
29
            ->setName('keyset:merge')
30
            ->setDescription('Merge several key sets into one.')
31
            ->setHelp('This command merges several key sets into one. It is very useful when you generate e.g. RSA, EC and OKP keys and you want only one key set to rule them all.')
32
            ->addArgument('jwksets', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'The JWKSet objects')
33
        ;
34
    }
35
36
    /**
37
     * @throws InvalidArgumentException if the JWKSet is invalid
38
     */
39
    protected function execute(InputInterface $input, OutputInterface $output): ?int
40
    {
41
        /** @var string[] $keySets */
42
        $keySets = $input->getArgument('jwksets');
43
        $newJwkset = new JWKSet([]);
44
        foreach ($keySets as $keySet) {
45
            $json = JsonConverter::decode($keySet);
46
            if (!\is_array($json)) {
47
                throw new InvalidArgumentException('The argument must be a valid JWKSet.');
48
            }
49
            $jwkset = JWKSet::createFromKeyData($json);
50
            foreach ($jwkset->all() as $jwk) {
51
                $newJwkset = $newJwkset->with($jwk);
52
            }
53
        }
54
        $this->prepareJsonOutput($input, $output, $newJwkset);
55
56
        return 0;
57
    }
58
}
59