Failed Conditions
Push — v7 ( 859ab9...badffc )
by Florent
02:23
created

RotateCommand::execute()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.439
c 0
b 0
f 0
cc 6
eloc 21
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 Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
final class RotateCommand extends ContainerAwareCommand
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    protected function configure()
27
    {
28
        $this
29
            ->setName('keyset:rotate')
30
            ->setDescription('Rotate a key set.')
31
            ->addArgument(
32
                'service',
33
                InputArgument::REQUIRED
34
            )
35
            ->addArgument(
36
                'ttl',
37
                InputArgument::OPTIONAL,
38
                '',
39
                3600 * 24 * 7
40
            )
41
            ->setHelp(<<<'EOT'
42
The <info>%command.name%</info> command will rotate a key or keys in the key set.
43
44
  <info>php %command.full_name%</info>
45
EOT
46
        );
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function execute(InputInterface $input, OutputInterface $output)
53
    {
54
        $service_name = $input->getArgument('service');
55
        if (!$this->getContainer()->has($service_name)) {
56
            $output->writeln(sprintf('<error>The service "%s" does not exist.</error>', $service_name));
57
58
            return 1;
59
        }
60
        $service = $this->getContainer()->get($service_name);
61
        if (!$service instanceof JWKSetInterface) {
0 ignored issues
show
Bug introduced by
The class Jose\Component\Console\Command\JWKSetInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
62
            $output->writeln(sprintf('<error>The service "%s" is not a key set.</error>', $service_name));
63
64
            return 2;
65
        }
66
67
        if (!$service instanceof RotatableInterface) {
0 ignored issues
show
Bug introduced by
The class Jose\Component\Console\Command\RotatableInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
68
            $output->writeln(sprintf('<error>The service "%s" is not a rotatable key set.</error>', $service_name));
69
70
            return 3;
71
        }
72
73
        $mtime = $service->getLastModificationTime();
74
75
        if (null === $mtime) {
76
            $service->regen();
77
            $output->writeln('Done.');
78
        } elseif ($mtime + $input->getArgument('ttl') <= time()) {
79
            $service->rotate();
80
            $output->writeln('Done.');
81
        } else {
82
            $output->writeln(sprintf('The key set "%s" has not expired.', $service_name));
83
        }
84
    }
85
}
86