P12CertificateLoaderCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 34
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 1
A execute() 0 16 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 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 InvalidArgumentException;
17
use Jose\Component\KeyManagement\JWKFactory;
18
use Symfony\Component\Console\Input\InputArgument;
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 P12CertificateLoaderCommand extends GeneratorCommand
24
{
25
    protected function configure(): void
26
    {
27
        parent::configure();
28
        $this
29
            ->setName('key:load:p12')
30
            ->setDescription('Load a key from a P12 certificate file.')
31
            ->addArgument('file', InputArgument::REQUIRED, 'Filename of the P12 certificate.')
32
            ->addOption('secret', 's', InputOption::VALUE_OPTIONAL, 'Secret if the key is encrypted.', null)
33
        ;
34
    }
35
36
    /**
37
     * @throws InvalidArgumentException if the file is not valid
38
     * @throws InvalidArgumentException if the secret is not valid
39
     */
40
    protected function execute(InputInterface $input, OutputInterface $output): ?int
41
    {
42
        $file = $input->getArgument('file');
43
        $password = $input->getOption('secret');
44
        if (!\is_string($file)) {
45
            throw new InvalidArgumentException('Invalid file');
46
        }
47
        if (!\is_string($password)) {
48
            throw new InvalidArgumentException('Invalid secret');
49
        }
50
        $args = $this->getOptions($input);
51
        $jwk = JWKFactory::createFromPKCS12CertificateFile($file, $password, $args);
52
        $this->prepareJsonOutput($input, $output, $jwk);
53
54
        return 0;
55
    }
56
}
57