1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IrishDan\NotificationBundle\Command; |
4
|
|
|
|
5
|
|
|
use IrishDan\NotificationBundle\Generator\DatabaseNotificationGenerator; |
6
|
|
|
use Sensio\Bundle\GeneratorBundle\Command\GeneratorCommand; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
11
|
|
|
use Symfony\Component\Console\Question\Question; |
12
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
class CreateDatabaseNotificationCommand extends GeneratorCommand |
16
|
|
|
{ |
17
|
|
|
private $entityName; |
18
|
|
|
|
19
|
|
|
public function setEntityName(array $databaseChannelConfig) |
20
|
|
|
{ |
21
|
|
|
// @TODO: If its not set then we need to ask for it, throw an error |
22
|
|
|
if (!empty($databaseChannelConfig['entity'])) { |
23
|
|
|
$this->entityName = explode(':', $databaseChannelConfig['entity'])[1]; |
24
|
|
|
} else { |
25
|
|
|
$this->entityName = 'Notification'; |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function configure() |
30
|
|
|
{ |
31
|
|
|
$this |
32
|
|
|
->setName('notification:create-database-notification') |
33
|
|
|
->setDescription('Creates a new doctrine database channel notification class') |
34
|
|
|
->setDefinition([ |
35
|
|
|
new InputOption('bundle', '', InputOption::VALUE_REQUIRED, 'The bundle for this notification'), |
36
|
|
|
]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function interact(InputInterface $input, OutputInterface $output) |
40
|
|
|
{ |
41
|
|
|
$questionHelper = $this->getQuestionHelper(); |
42
|
|
|
$questionHelper->writeSection($output, 'Welcome to the database channel Notification generator'); |
43
|
|
|
|
44
|
|
|
// Get the Bundle to generate it in |
45
|
|
|
$output->writeln([ |
46
|
|
|
'This command helps you generate a a database channel Notification class', |
47
|
|
|
'', |
48
|
|
|
'First, give the name of the bundle to generate the notification in (eg <comment>AppBundle</comment>)', |
49
|
|
|
]); |
50
|
|
|
|
51
|
|
|
$question = new Question($questionHelper->getQuestion('The bundle name', $input->getOption('bundle')), $input->getOption('bundle')); |
52
|
|
|
|
53
|
|
|
// @TODO: Add existing bundle validation |
54
|
|
|
$question->setValidator(['Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateBundleName']); |
55
|
|
|
$question->setNormalizer(function ($value) { |
56
|
|
|
return $value ? trim($value) : ''; |
57
|
|
|
}); |
58
|
|
|
$question->setMaxAttempts(2); |
59
|
|
|
|
60
|
|
|
$bundle = $questionHelper->ask($input, $output, $question); |
61
|
|
|
$input->setOption('bundle', $bundle); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
65
|
|
|
{ |
66
|
|
|
$questionHelper = $this->getQuestionHelper(); |
67
|
|
|
|
68
|
|
View Code Duplication |
if ($input->isInteractive()) { |
|
|
|
|
69
|
|
|
$question = new ConfirmationQuestion($questionHelper->getQuestion('Do you confirm generation', 'yes', '?'), true); |
70
|
|
|
if (!$questionHelper->ask($input, $output, $question)) { |
71
|
|
|
$output->writeln('<error>Command aborted</error>'); |
72
|
|
|
|
73
|
|
|
return 1; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$style = new SymfonyStyle($input, $output); |
78
|
|
|
|
79
|
|
|
$bundle = $input->getOption('bundle'); |
80
|
|
|
|
81
|
|
|
$style->text('Generating new database notification class ' . $this->entityName . ' generated in ' . $bundle); |
82
|
|
|
|
83
|
|
|
try { |
84
|
|
|
$bundle = $this->getContainer()->get('kernel')->getBundle($bundle); |
85
|
|
|
} catch (\Exception $e) { |
86
|
|
|
$output->writeln(sprintf('<bg=red>Bundle "%s" does not exist.</>', $bundle)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$generator = $this->getGenerator($bundle); |
90
|
|
|
$generator->generate($bundle, $this->entityName); |
91
|
|
|
|
92
|
|
|
$output->writeln(sprintf('Generated the <info>%s</info> notification in <info>%s</info>', $this->entityName, $bundle->getName())); |
93
|
|
|
$questionHelper->writeGeneratorSummary($output, []); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected function createGenerator() |
97
|
|
|
{ |
98
|
|
|
return new DatabaseNotificationGenerator( |
99
|
|
|
$this->getContainer()->get('filesystem') |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
} |
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.