Failed Conditions
Push — v7 ( 137ba9...eb2dfc )
by Florent
03:53
created

X509CertificateLoaderCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 56
rs 10
c 1
b 0
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 16 1
B execute() 0 22 4
A isEnabled() 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\Command;
15
16
use Jose\Component\KeyManagement\JWKFactory;
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Input\InputDefinition;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
final class X509CertificateLoaderCommand extends Command
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function configure()
29
    {
30
        $this
31
            ->setName('key:load:x509')
32
            ->setDescription('Load a key from a X.509 certificate file.')
33
            ->setDefinition(
34
                new InputDefinition([
35
                    new InputOption('file', 'f', InputOption::VALUE_REQUIRED, 'Filename of the X.509 certificate.'),
36
                    new InputOption('secret', 's', InputOption::VALUE_OPTIONAL, 'Secret if the key is encrypted.'),
37
                    new InputOption('use', 'u', InputOption::VALUE_OPTIONAL, 'Usage of the key. Must be either "sig" or "enc".'),
38
                    new InputOption('alg', 'a', InputOption::VALUE_OPTIONAL, 'Algorithm for the key.'),
39
                    new InputOption('out', 'o', InputOption::VALUE_OPTIONAL, 'File where to save the key. Must be a valid and writable file name.'),
40
                ])
41
            )
42
        ;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function execute(InputInterface $input, OutputInterface $output)
49
    {
50
        $filename = $input->getOption('file');
51
        $password = $input->getOption('secret');
52
        $args = [];
53
        foreach (['use', 'alg'] as $key) {
54
            $value = $input->getOption($key);
55
            if (null !== $value) {
56
                $args[$key] = $value;
57
            }
58
        }
59
60
        $jwk = JWKFactory::createFromCertificateFile($filename, $password, $args);
0 ignored issues
show
Unused Code introduced by
The call to JWKFactory::createFromCertificateFile() has too many arguments starting with $args.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
61
        $json = json_encode($jwk);
62
63
        $file = $input->getOption('out');
64
        if (null !== $file) {
65
            file_put_contents($file, $json, LOCK_EX);
66
        } else {
67
            $output->write($json);
68
        }
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function isEnabled()
75
    {
76
        return class_exists('\Jose\Component\KeyManagement\JWKFactory');
77
    }
78
}
79