Failed Conditions
Push — v7 ( 1e67af...6a6683 )
by Florent
02:18
created

KeyAnalyzerCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 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\Bundle\Console\Command;
15
16
use Jose\Component\Core\JWK;
17
use Jose\Component\KeyManagement\KeyAnalyzer\JWKAnalyzerManager;
18
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
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 KeyAnalyzerCommand extends ContainerAwareCommand
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function configure()
29
    {
30
        $this
31
            ->setName('key:analyze')
32
            ->setDescription('JWK quality analyzer.')
33
            ->setHelp('This command will analyze a JWK object and find security issues.')
34
            ->addArgument('jwk', InputArgument::REQUIRED, 'The JWK object')
35
        ;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        /** @var JWKAnalyzerManager $analyzerManager */
44
        $analyzerManager = $this->getContainer()->get(JWKAnalyzerManager::class);
45
        $jwk = $this->getKey($input);
46
47
        $result = $analyzerManager->analyze($jwk);
48
        foreach ($result as $message) {
49
            $output->writeln($message);
50
        }
51
    }
52
53
    /**
54
     * @param InputInterface $input
55
     *
56
     * @return JWK
57
     */
58
    private function getKey(InputInterface $input): JWK
59
    {
60
        $jwkset = $input->getArgument('jwk');
61
        $json = json_decode($jwkset, true);
62
        if (is_array($json)) {
63
            return JWK::create($json);
64
        } elseif ($this->getContainer()->has($jwkset)) {
65
            $id = $this->getContainer()->get($jwkset);
66
            if ($id instanceof JWK) {
67
                return $id;
68
            }
69
        }
70
71
        throw new \InvalidArgumentException('The argument must be a valid JWKSet or a service ID to a JWKSet.');
72
    }
73
}
74