Failed Conditions
Push — v7 ( 809898...b3c0a6 )
by Florent
05:05
created

AbstractGeneratorCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
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\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
abstract class AbstractGeneratorCommand extends Command
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function isEnabled()
29
    {
30
        return class_exists(JWKFactory::class);
31
    }
32
33
    /**
34
     * Configures the current command.
35
     */
36
    protected function configure()
37
    {
38
        $this
39
            ->setName('key:generate:ec')
40
            ->setDescription('Generate an EC key (JWK format)')
41
            ->setDefinition(
42
                new InputDefinition([
43
                    new InputOption('use', 'u', InputOption::VALUE_OPTIONAL, 'Usage of the key. Must be either "sig" or "enc".'),
44
                    new InputOption('alg', 'a', InputOption::VALUE_OPTIONAL, 'Algorithm for the key.'),
45
                    new InputOption('out', 'o', InputOption::VALUE_OPTIONAL, 'File where to save the key. Must be a valid and writable file name.'),
46
                ])
47
            )
48
        ;
49
    }
50
51
    /**
52
     * @param InputInterface $input
53
     *
54
     * @return array
55
     */
56
    protected function getOptions(InputInterface $input): array
57
    {
58
        $args = [];
59
        foreach (['use', 'alg'] as $key) {
60
            $value = $input->getOption($key);
61
            if (null !== $value) {
62
                $args[$key] = $value;
63
            }
64
        }
65
66
        return $args;
67
    }
68
69
    /**
70
     * @param InputInterface $input
71
     * @param OutputInterface $output
72
     * @param string $json
73
     */
74
    protected function prepareOutput(InputInterface $input, OutputInterface $output, string $json)
75
    {
76
        $file = $input->getOption('out');
77
        if (null !== $file) {
78
            file_put_contents($file, $json, LOCK_EX);
79
        } else {
80
            $output->write($json);
81
        }
82
    }
83
}
84