Failed Conditions
Push — v7 ( 2e98c5...18cb20 )
by Florent
12:17 queued 04:38
created

AddKeyIntoKeysetCommand::getKeyset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
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\JWK;
18
use Jose\Component\Core\JWKSet;
19
use Symfony\Component\Console\Input\InputArgument;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
/**
24
 * Class AddKeyIntoKeysetCommand.
25
 */
26
final class AddKeyIntoKeysetCommand extends AbstractObjectOutputCommand
27
{
28
    /**
29
     * KeyAnalyzerCommand constructor.
30
     *
31
     * @param JsonConverterInterface $jsonConverter
32
     * @param string|null            $name
33
     */
34
    public function __construct(JsonConverterInterface $jsonConverter, string $name = null)
35
    {
36
        parent::__construct($jsonConverter, $name);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function configure()
43
    {
44
        parent::configure();
45
        $this
46
            ->setName('keyset:add:key')
47
            ->setDescription('Add a key into a key set.')
48
            ->setHelp('This command adds a key at the end of a key set.')
49
            ->addArgument('jwkset', InputArgument::REQUIRED, 'The JWKSet object')
50
            ->addArgument('jwk', InputArgument::REQUIRED, 'The new JWK object')
51
        ;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    protected function execute(InputInterface $input, OutputInterface $output)
58
    {
59
        $jwkset = $this->getKeyset($input);
60
        $jwk = $this->getKey($input);
61
        $jwkset = $jwkset->with($jwk);
62
        $this->prepareJsonOutput($input, $output,$jwkset);
63
    }
64
65
    /**
66
     * @param InputInterface $input
67
     *
68
     * @return JWKSet
69
     */
70
    private function getKeyset(InputInterface $input): JWKSet
71
    {
72
        $jwkset = $input->getArgument('jwkset');
73
        $json = $this->jsonConverter->decode($jwkset);
74
        if (is_array($json)) {
75
            return JWKSet::createFromKeyData($json);
76
        }
77
78
        throw new \InvalidArgumentException('The argument must be a valid JWKSet.');
79
    }
80
81
    /**
82
     * @param InputInterface $input
83
     *
84
     * @return JWK
85
     */
86
    private function getKey(InputInterface $input): JWK
87
    {
88
        $jwkset = $input->getArgument('jwk');
89
        $json = $this->jsonConverter->decode($jwkset);
90
        if (is_array($json)) {
91
            return JWK::create($json);
92
        }
93
94
        throw new \InvalidArgumentException('The argument must be a valid JWK.');
95
    }
96
}
97