DeleteCommand::execute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 2
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
final class DeleteCommand extends ContainerAwareCommand
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected function configure()
26
    {
27
        $this
28
            ->setName('spomky-labs:jose:delete')
29
            ->setDescription('Delete a key or key set.')
30
            ->addArgument(
31
                'service',
32
                InputArgument::REQUIRED
33
            )
34
            ->setHelp(<<<'EOT'
35
The <info>%command.name%</info> command will delete a key or key set.
36
If the service is called, then the key will be created again.
37
38
  <info>php %command.full_name%</info>
39
EOT
40
        );
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48
        $service_name = $input->getArgument('service');
49
        if (!$this->getContainer()->has($service_name)) {
50
            $output->writeln(sprintf('<error>The service "%s" does not exist.</error>', $service_name));
51
52
            return 1;
53
        }
54
        $service = $this->getContainer()->get($service_name);
55
        if (!$service instanceof StorableInterface) {
56
            $output->writeln(sprintf('<error>The service "%s" is not a storable object.</error>', $service_name));
57
58
            return 2;
59
        }
60
61
        $service->delete();
62
        $output->writeln('Done.');
63
    }
64
}
65