Failed Conditions
Push — master ( 6560de...8483ce )
by Florent
05:17
created

GeneratorCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 3
dl 0
loc 52
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isEnabled() 0 4 1
A configure() 0 9 1
A getOptions() 0 15 4
A generateKeyID() 0 4 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 Base64Url\Base64Url;
17
use Jose\Component\KeyManagement\JWKFactory;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
21
/**
22
 * Class GeneratorCommand.
23
 */
24
abstract class GeneratorCommand extends ObjectOutputCommand
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function isEnabled()
30
    {
31
        return class_exists(JWKFactory::class);
32
    }
33
34
    /**
35
     * Configures the current command.
36
     */
37
    protected function configure()
38
    {
39
        parent::configure();
40
        $this
41
            ->addOption('use', 'u', InputOption::VALUE_OPTIONAL, 'Usage of the key. Must be either "sig" or "enc".')
42
            ->addOption('alg', 'a', InputOption::VALUE_OPTIONAL, 'Algorithm for the key.')
43
            ->addOption('random_id', null, InputOption::VALUE_NONE, 'If this option is set, a random key ID (kid) will be generated.')
44
        ;
45
    }
46
47
    /**
48
     * @param InputInterface $input
49
     *
50
     * @return array
51
     */
52
    protected function getOptions(InputInterface $input): array
53
    {
54
        $args = [];
55
        if ($input->getOption('random_id')) {
56
            $args['kid'] = $this->generateKeyID();
57
        }
58
        foreach (['use', 'alg'] as $key) {
59
            $value = $input->getOption($key);
60
            if (null !== $value) {
61
                $args[$key] = $value;
62
            }
63
        }
64
65
        return $args;
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    private function generateKeyID(): string
72
    {
73
        return Base64Url::encode(random_bytes(64));
74
    }
75
}
76