Failed Conditions
Push — v7 ( 6a6683...1d7ef8 )
by Florent
03:11
created

PemConverterCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 9 1
A execute() 0 20 4
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\Core\Util\ECKey;
18
use Jose\Component\Core\Util\RSAKey;
19
use Symfony\Component\Console\Input\InputArgument;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
final class PemConverterCommand extends AbstractObjectOutputCommand
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function configure()
29
    {
30
        parent::configure();
31
        $this
32
            ->setName('key:convert:pkcs1')
33
            ->setDescription('Converts a RSA or EC key into PKCS#1 key.')
34
            ->addArgument('jwk', InputArgument::REQUIRED, 'The key')
35
        ;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $jwk = $input->getArgument('jwk');
44
        $json = $this->jsonConverter->decode($jwk);
45
        if (!is_array($json)) {
46
            throw new \InvalidArgumentException('Invalid key.');
47
        }
48
        $key = JWK::create($json);
49
        switch ($key->get('kty')) {
50
            case 'RSA';
51
                $pem = RSAKey::createFromJWK($key)->toPem();
52
                break;
53
            case 'EC';
54
                $pem = ECKey::createFromJWK($key)->toPem();
55
                break;
56
            default:
57
                throw new \InvalidArgumentException('Not a RSA or EC key.');
58
        }
59
        $this->prepareOutput($input, $output, $pem);
60
    }
61
}
62