Completed
Push — master ( b53e51...1009f9 )
by Alejandro
04:13 queued 38s
created

RenameTagCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 49
ccs 21
cts 21
cp 1
rs 10
wmc 4
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 8 1
A execute() 0 14 2
1
<?php
2
namespace Shlinkio\Shlink\CLI\Command\Tag;
3
4
use Acelaya\ZsmAnnotatedServices\Annotation as DI;
5
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
6
use Shlinkio\Shlink\Core\Service\Tag\TagService;
7
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Zend\I18n\Translator\Translator;
13
use Zend\I18n\Translator\TranslatorInterface;
14
15
class RenameTagCommand extends Command
16
{
17
    /**
18
     * @var TagServiceInterface
19
     */
20
    private $tagService;
21
    /**
22
     * @var TranslatorInterface
23
     */
24
    private $translator;
25
26
    /**
27
     * RenameTagCommand constructor.
28
     * @param TagServiceInterface $tagService
29
     * @param TranslatorInterface $translator
30
     *
31
     * @DI\Inject({TagService::class, Translator::class})
32
     */
33 2
    public function __construct(TagServiceInterface $tagService, TranslatorInterface $translator)
34
    {
35 2
        $this->tagService = $tagService;
36 2
        $this->translator = $translator;
37 2
        parent::__construct();
38 2
    }
39
40 2
    protected function configure()
41
    {
42 2
        $this
43 2
            ->setName('tag:rename')
44 2
            ->setDescription($this->translator->translate('Renames one existing tag.'))
45 2
            ->addArgument('oldName', InputArgument::REQUIRED, $this->translator->translate('Current name of the tag.'))
46 2
            ->addArgument('newName', InputArgument::REQUIRED, $this->translator->translate('New name of the tag.'));
47 2
    }
48
49 2
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51 2
        $oldName = $input->getArgument('oldName');
52 2
        $newName = $input->getArgument('newName');
53
54
        try {
55 2
            $this->tagService->renameTag($oldName, $newName);
56 1
            $output->writeln(sprintf('<info>%s</info>', $this->translator->translate('Tag properly renamed.')));
57 2
        } catch (EntityDoesNotExistException $e) {
58 1
            $output->writeln('<error>' . sprintf($this->translator->translate(
59
                'A tag with name "%s" was not found'
60 1
            ), $oldName) . '</error>');
61
        }
62 2
    }
63
}
64