PublicKeyCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 1
A execute() 0 9 1
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\Util\JsonConverter;
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 PublicKeyCommand extends ObjectOutputCommand
24
{
25
    protected function configure(): void
26
    {
27
        parent::configure();
28
        $this
29
            ->setName('key:convert:public')
30
            ->setDescription('Convert a private key into public key. Symmetric keys (shared keys) are not changed.')
31
            ->setHelp('This command converts a private key into a public key.')
32
            ->addArgument('jwk', InputArgument::REQUIRED, 'The JWK object')
33
        ;
34
    }
35
36
    protected function execute(InputInterface $input, OutputInterface $output): ?int
37
    {
38
        $jwk = $this->getKey($input);
39
        $jwk = $jwk->toPublic();
40
41
        $this->prepareJsonOutput($input, $output, $jwk);
42
43
        return 0;
44
    }
45
46
    /**
47
     * @throws InvalidArgumentException if the key is invalid
48
     */
49
    private function getKey(InputInterface $input): JWK
50
    {
51
        $jwk = $input->getArgument('jwk');
52
        if (!\is_string($jwk)) {
53
            throw new InvalidArgumentException('Invalid JWK');
54
        }
55
        $json = JsonConverter::decode($jwk);
56
        if (!\is_array($json)) {
57
            throw new InvalidArgumentException('Invalid JWK');
58
        }
59
60
        return new JWK($json);
61
    }
62
}
63