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

KeyFileLoaderCommand::execute()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 22
rs 8.9197
c 1
b 0
f 1
cc 4
eloc 15
nc 6
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\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 KeyFileLoaderCommand extends Command
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function configure()
29
    {
30
        $this
31
            ->setName('key:load:key')
32
            ->setDescription('Load a key from a PEM or a DER key file. Encrypted keys are supported.')
33
            ->setDefinition(
34
                new InputDefinition([
35
                    new InputOption('file', 'f', InputOption::VALUE_REQUIRED, 'Filename of the key.'),
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::createFromKeyFile($filename, $password, $args);
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