Failed Conditions
Push — v7 ( 722dd5...1e67af )
by Florent
03:17
created

OptimizeRsaKeyCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
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\Command;
15
16
use Jose\Component\Core\JWK;
17
use Jose\Component\KeyManagement\KeyConverter\RSAKey;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
final class OptimizeRsaKeyCommand extends AbstractJsonObjectOutputCommand
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected function configure()
28
    {
29
        parent::configure();
30
        $this
31
            ->setName('key:optimize')
32
            ->setDescription('Optimize a RSA key by calculating additional primes (CRT).')
33
            ->addArgument('jwk', InputArgument::REQUIRED, 'The RSA key.');
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    protected function execute(InputInterface $input, OutputInterface $output)
40
    {
41
        $jwk = $input->getArgument('jwk');
42
        $json = $this->jsonConverter->decode($jwk);
43
        if (!is_array($json)) {
44
            throw new \InvalidArgumentException('Invalid input.');
45
        }
46
        $key = RSAKey::createFromJWK(JWK::create($json));
47
        $key->optimize();
48
        $this->prepareOutput($input, $output, $key->toJwk());
49
    }
50
}
51