Failed Conditions
Push — v7 ( 35cf1c...61f7e2 )
by Florent
24:40
created

MergeKeysetCommand::execute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 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 Jose\Component\Core\Converter\JsonConverterInterface;
17
use Jose\Component\Core\JWKSet;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
/**
23
 * Class MergeKeysetCommand
24
 */
25
final class MergeKeysetCommand extends AbstractGeneratorCommand
26
{
27
    /**
28
     * KeyAnalyzerCommand constructor.
29
     *
30
     * @param JsonConverterInterface $jsonConverter
31
     * @param string|null            $name
32
     */
33
    public function __construct(JsonConverterInterface $jsonConverter, string $name = null)
34
    {
35
        parent::__construct($jsonConverter, $name);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function configure()
42
    {
43
        parent::configure();
44
        $this
45
            ->setName('keyset:convert:merge')
46
            ->setDescription('Merge several key sets into one.')
47
            ->setHelp('This command merge 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.')
48
            ->addArgument('jwksets', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'The JWKSet objects')
49
        ;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function execute(InputInterface $input, OutputInterface $output)
56
    {
57
        $keySets = $input->getArgument('jwksets');
58
        $newJwkset = JWKSet::createFromKeys([]);
59
        foreach ($keySets as $keySet) {
60
            $json = $this->jsonConverter->decode($keySet);
61
            if (!is_array($json)) {
62
                throw new \InvalidArgumentException('The argument must be a valid JWKSet.');
63
            }
64
            $jwkset = JWKSet::createFromKeyData($json);
65
            foreach ($jwkset->all() as $jwk) {
66
                $newJwkset = $newJwkset->with($jwk);
67
            }
68
        }
69
        $this->prepareJsonOutput($input, $output, $newJwkset);
70
    }
71
}
72