Failed Conditions
Push — v7 ( 35cf1c...61f7e2 )
by Florent
24:40
created

PublicKeysetCommand::getKeyset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
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\JWKSet;
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 PublicKeysetCommand
24
 */
25
final class PublicKeysetCommand extends AbstractGeneratorCommand
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('keyset:convert:public')
46
            ->setDescription('Convert private keys in a key set into public keys. Symmetric keys (shared keys) are not changed.')
47
            ->setHelp('This command converts private keys in a key set into public keys.')
48
            ->addArgument('jwkset', InputArgument::REQUIRED, 'The JWKSet object')
49
        ;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function execute(InputInterface $input, OutputInterface $output)
56
    {
57
        $jwkset = $this->getKeyset($input);
58
        $newJwkset = JWKSet::createFromKeys([]);
59
60
        foreach ($jwkset->all() as $jwk) {
61
            $newJwkset = $newJwkset->with($jwk->toPublic());
62
        }
63
        $this->prepareJsonOutput($input, $output, $newJwkset);
64
    }
65
66
    /**
67
     * @param InputInterface $input
68
     *
69
     * @return JWKSet
70
     */
71
    private function getKeyset(InputInterface $input): JWKSet
72
    {
73
        $jwkset = $input->getArgument('jwkset');
74
        $json = $this->jsonConverter->decode($jwkset);
75
        if (is_array($json)) {
76
            return JWKSet::createFromKeyData($json);
77
        }
78
79
        throw new \InvalidArgumentException('The argument must be a valid JWKSet.');
80
    }
81
}
82