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

KeyAnalyzerCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

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

4 Methods

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