Failed Conditions
Push — v7 ( 12d27f...276ae4 )
by Florent
03:49
created

KeyAnalyzerCommand::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 Jose\Component\KeyManagement\KeyAnalyzer\JWKAnalyzerManager;
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Output\OutputInterface;
23
24
/**
25
 * Class KeyAnalyzerCommand.
26
 */
27
final class KeyAnalyzerCommand extends Command
28
{
29
    /**
30
     * @var JWKAnalyzerManager
31
     */
32
    private $analyzerManager;
33
34
    /**
35
     * @var JsonConverterInterface
36
     */
37
    private $jsonConverter;
38
39
    /**
40
     * KeyAnalyzerCommand constructor.
41
     *
42
     * @param JWKAnalyzerManager     $analyzerManager
43
     * @param JsonConverterInterface $jsonConverter
44
     * @param string|null            $name
45
     */
46
    public function __construct(JWKAnalyzerManager $analyzerManager, JsonConverterInterface $jsonConverter, string $name = null)
47
    {
48
        parent::__construct($name);
49
        $this->analyzerManager = $analyzerManager;
50
        $this->jsonConverter = $jsonConverter;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    protected function configure()
57
    {
58
        parent::configure();
59
        $this
60
            ->setName('key:analyze')
61
            ->setDescription('JWK quality analyzer.')
62
            ->setHelp('This command will analyze a JWK object and find security issues.')
63
            ->addArgument('jwk', InputArgument::REQUIRED, 'The JWK object')
64
        ;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    protected function execute(InputInterface $input, OutputInterface $output)
71
    {
72
        $jwk = $this->getKey($input);
73
74
        $result = $this->analyzerManager->analyze($jwk);
75
        foreach ($result as $message) {
76
            $output->writeln($message);
77
        }
78
    }
79
80
    /**
81
     * @param InputInterface $input
82
     *
83
     * @return JWK
84
     */
85
    private function getKey(InputInterface $input): JWK
86
    {
87
        $jwk = $input->getArgument('jwk');
88
        $json = $this->jsonConverter->decode($jwk);
89
        if (is_array($json)) {
90
            return JWK::create($json);
91
        }
92
93
        throw new \InvalidArgumentException('The argument must be a valid JWKSet.');
94
    }
95
}
96