Failed Conditions
Push — master ( 49c8cc...07673b )
by Florent
01:50
created

PublicKeyCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
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;
15
16
use Jose\Component\Core\Converter\JsonConverterInterface;
17
use Jose\Component\Core\JWK;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
/**
23
 * Class PublicKeyCommand.
24
 */
25
final class PublicKeyCommand extends AbstractObjectOutputCommand
26
{
27
    /**
28
     * KeyAnalyzerCommand constructor.
29
     *
30
     * @param JsonConverterInterface $jsonConverter
31
     * @param string|null            $name
32
     */
33
    public function __construct(JsonConverterInterface $jsonConverter, string $name = null)
34
    {
35
        parent::__construct($jsonConverter, $name);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function configure()
42
    {
43
        parent::configure();
44
        $this
45
            ->setName('key:convert:public')
46
            ->setDescription('Convert a private key into public key. Symmetric keys (shared keys) are not changed.')
47
            ->setHelp('This command converts a private key into a public key.')
48
            ->addArgument('jwk', InputArgument::REQUIRED, 'The JWK object')
49
        ;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function execute(InputInterface $input, OutputInterface $output)
56
    {
57
        $jwk = $this->getKey($input);
58
        $jwk = $jwk->toPublic();
59
60
        $this->prepareJsonOutput($input, $output, $jwk);
61
    }
62
63
    /**
64
     * @param InputInterface $input
65
     *
66
     * @return JWK
67
     */
68
    private function getKey(InputInterface $input): JWK
69
    {
70
        $jwk = $input->getArgument('jwk');
71
        $json = $this->jsonConverter->decode($jwk);
72
        if (is_array($json)) {
73
            return JWK::create($json);
74
        }
75
76
        throw new \InvalidArgumentException('The argument must be a valid JWK.');
77
    }
78
}
79