Failed Conditions
Push — v7 ( 61f7e2...2e98c5 )
by Florent
14:13 queued 10:17
created

RotateKeysetCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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\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 RotateKeysetCommand.
25
 */
26
final class RotateKeysetCommand extends AbstractGeneratorCommand
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:convert:rotate')
47
            ->setDescription('Rotate a key set.')
48
            ->setHelp('This command removes the last key in a key set a place a new one at the beginning.')
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)->all();
60
        $jwk = $this->getKey($input);
61
62
        if (0 !== count($jwkset)) {
63
            array_pop($jwkset);
64
        }
65
        array_unshift($jwkset, $jwk);
66
67
        $this->prepareJsonOutput($input, $output, JWKSet::createFromKeys($jwkset));
68
    }
69
70
    /**
71
     * @param InputInterface $input
72
     *
73
     * @return JWKSet
74
     */
75
    private function getKeyset(InputInterface $input): JWKSet
76
    {
77
        $jwkset = $input->getArgument('jwkset');
78
        $json = $this->jsonConverter->decode($jwkset);
79
        if (is_array($json)) {
80
            return JWKSet::createFromKeyData($json);
81
        }
82
83
        throw new \InvalidArgumentException('The argument must be a valid JWKSet.');
84
    }
85
86
    /**
87
     * @param InputInterface $input
88
     *
89
     * @return JWK
90
     */
91
    private function getKey(InputInterface $input): JWK
92
    {
93
        $jwkset = $input->getArgument('jwk');
94
        $json = $this->jsonConverter->decode($jwkset);
95
        if (is_array($json)) {
96
            return JWK::create($json);
97
        }
98
99
        throw new \InvalidArgumentException('The argument must be a valid JWK.');
100
    }
101
}
102