Completed
Pull Request — master (#24)
by Florent
06:28 queued 03:25
created

RegenCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace SpomkyLabs\JoseBundle\Command;
13
14
use Jose\Object\StorableInterface;
15
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
class RegenCommand extends ContainerAwareCommand
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected function configure()
26
    {
27
        $this
28
            ->setName('spomky-labs:jose:regen')
29
            ->setDescription('Generate a new key or key set.')
30
            ->addArgument(
31
                'service',
32
                InputArgument::REQUIRED
33
            )
34
            ->setHelp(<<<'EOT'
35
The <info>%command.name%</info> command will generate a new key or key set.
36
37
  <info>php %command.full_name%</info>
38
EOT
39
        );
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    protected function execute(InputInterface $input, OutputInterface $output)
46
    {
47
        $service_name = $input->getArgument('service');
48
        if (!$this->getContainer()->has($service_name)) {
49
            $output->writeln(sprintf('<error>The service "%s" does not exist.</error>', $service_name));
50
51
            return 1;
52
        }
53
        $service = $this->getContainer()->get($service_name);
54
        if (!$service instanceof StorableInterface) {
55
            $output->writeln(sprintf('<error>The service "%s" is not a storable object.</error>', $service_name));
56
57
            return 2;
58
        }
59
60
        $service->regen();
61
        $output->writeln('Done.');
62
    }
63
}
64