1
|
|
|
<?php |
2
|
|
|
namespace Shlinkio\Shlink\CLI\Command\Tag; |
3
|
|
|
|
4
|
|
|
use Acelaya\ZsmAnnotatedServices\Annotation as DI; |
5
|
|
|
use Shlinkio\Shlink\Core\Service\Tag\TagService; |
6
|
|
|
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Zend\I18n\Translator\Translator; |
12
|
|
|
use Zend\I18n\Translator\TranslatorInterface; |
13
|
|
|
|
14
|
|
View Code Duplication |
class CreateTagCommand extends Command |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var TagServiceInterface |
18
|
|
|
*/ |
19
|
|
|
private $tagService; |
20
|
|
|
/** |
21
|
|
|
* @var TranslatorInterface |
22
|
|
|
*/ |
23
|
|
|
private $translator; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* CreateTagCommand constructor. |
27
|
|
|
* @param TagServiceInterface $tagService |
28
|
|
|
* @param TranslatorInterface $translator |
29
|
|
|
* |
30
|
|
|
* @DI\Inject({TagService::class, Translator::class}) |
31
|
|
|
*/ |
32
|
2 |
|
public function __construct(TagServiceInterface $tagService, TranslatorInterface $translator) |
33
|
|
|
{ |
34
|
2 |
|
$this->tagService = $tagService; |
35
|
2 |
|
$this->translator = $translator; |
36
|
2 |
|
parent::__construct(); |
37
|
2 |
|
} |
38
|
|
|
|
39
|
2 |
|
protected function configure() |
40
|
|
|
{ |
41
|
|
|
$this |
42
|
2 |
|
->setName('tag:create') |
43
|
2 |
|
->setDescription($this->translator->translate('Creates one or more tags.')) |
44
|
2 |
|
->addOption( |
45
|
2 |
|
'name', |
46
|
2 |
|
't', |
47
|
2 |
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
48
|
2 |
|
$this->translator->translate('The name of the tags to create') |
49
|
|
|
); |
50
|
2 |
|
} |
51
|
|
|
|
52
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
53
|
|
|
{ |
54
|
2 |
|
$tagNames = $input->getOption('name'); |
55
|
2 |
|
if (empty($tagNames)) { |
56
|
1 |
|
$output->writeln(sprintf( |
57
|
1 |
|
'<comment>%s</comment>', |
58
|
1 |
|
$this->translator->translate('You have to provide at least one tag name') |
59
|
|
|
)); |
60
|
1 |
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
$this->tagService->createTags($tagNames); |
64
|
1 |
|
$output->writeln($this->translator->translate('Created tags') . sprintf(': ["<info>%s</info>"]', implode( |
65
|
1 |
|
'</info>", "<info>', |
66
|
1 |
|
$tagNames |
67
|
|
|
))); |
68
|
1 |
|
} |
69
|
|
|
} |
70
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.