JKULoaderCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\JKUFactory;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
final class JKULoaderCommand extends ObjectOutputCommand
23
{
24
    /**
25
     * @var JKUFactory
26
     */
27
    private $jkuFactory;
28
29
    public function __construct(JKUFactory $jkuFactory, ?string $name = null)
30
    {
31
        $this->jkuFactory = $jkuFactory;
32
        parent::__construct($name);
33
    }
34
35
    protected function configure(): void
36
    {
37
        parent::configure();
38
        $this
39
            ->setName('keyset:load:jku')
40
            ->setDescription('Loads a key set from an url.')
41
            ->setHelp('This command will try to get a key set from an URL. The distant key set is a JWKSet.')
42
            ->addArgument('url', InputArgument::REQUIRED, 'The URL')
43
        ;
44
    }
45
46
    /**
47
     * @throws InvalidArgumentException if the URL is invalid
48
     */
49
    protected function execute(InputInterface $input, OutputInterface $output): ?int
50
    {
51
        $url = $input->getArgument('url');
52
        if (!\is_string($url)) {
53
            throw new InvalidArgumentException('Invalid URL');
54
        }
55
        $result = $this->jkuFactory->loadFromUrl($url);
56
        $this->prepareJsonOutput($input, $output, $result);
57
58
        return 0;
59
    }
60
}
61