AddKeyIntoKeysetCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 5
dl 0
loc 58
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 11 1
A execute() 0 9 1
A getKeyset() 0 13 3
A getKey() 0 13 3
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\JWK;
18
use Jose\Component\Core\JWKSet;
19
use Jose\Component\Core\Util\JsonConverter;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Output\OutputInterface;
23
24
final class AddKeyIntoKeysetCommand extends ObjectOutputCommand
25
{
26
    protected function configure(): void
27
    {
28
        parent::configure();
29
        $this
30
            ->setName('keyset:add:key')
31
            ->setDescription('Add a key into a key set.')
32
            ->setHelp('This command adds a key at the end of a key set.')
33
            ->addArgument('jwkset', InputArgument::REQUIRED, 'The JWKSet object')
34
            ->addArgument('jwk', InputArgument::REQUIRED, 'The new JWK object')
35
        ;
36
    }
37
38
    protected function execute(InputInterface $input, OutputInterface $output): ?int
39
    {
40
        $jwkset = $this->getKeyset($input);
41
        $jwk = $this->getKey($input);
42
        $jwkset = $jwkset->with($jwk);
43
        $this->prepareJsonOutput($input, $output, $jwkset);
44
45
        return 0;
46
    }
47
48
    /**
49
     * @throws InvalidArgumentException if the key set is invalid
50
     */
51
    private function getKeyset(InputInterface $input): JWKSet
52
    {
53
        $jwkset = $input->getArgument('jwkset');
54
        if (!\is_string($jwkset)) {
55
            throw new InvalidArgumentException('The argument must be a valid JWKSet.');
56
        }
57
        $json = JsonConverter::decode($jwkset);
58
        if (!\is_array($json)) {
59
            throw new InvalidArgumentException('The argument must be a valid JWKSet.');
60
        }
61
62
        return JWKSet::createFromKeyData($json);
63
    }
64
65
    /**
66
     * @throws InvalidArgumentException if the key is invalid
67
     */
68
    private function getKey(InputInterface $input): JWK
69
    {
70
        $jwk = $input->getArgument('jwk');
71
        if (!\is_string($jwk)) {
72
            throw new InvalidArgumentException('The argument must be a valid JWK.');
73
        }
74
        $json = JsonConverter::decode($jwk);
75
        if (!\is_array($json)) {
76
            throw new InvalidArgumentException('The argument must be a valid JWK.');
77
        }
78
79
        return new JWK($json);
80
    }
81
}
82