irishdan /
NotificationBundle
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace IrishDan\NotificationBundle\Command; |
||
| 4 | |||
| 5 | use IrishDan\NotificationBundle\Generator\NotificationGenerator; |
||
| 6 | // use Sensio\Bundle\GeneratorBundle\Command\GeneratorCommand; |
||
| 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 Symfony\Component\Console\Question\ConfirmationQuestion; |
||
| 12 | use Symfony\Component\Console\Question\Question; |
||
| 13 | use Symfony\Component\Console\Style\SymfonyStyle; |
||
| 14 | |||
| 15 | |||
| 16 | // class CreateNotificationCommand extends GeneratorCommand |
||
| 17 | class CreateNotificationCommand extends Command |
||
| 18 | { |
||
| 19 | private $channels; |
||
| 20 | private $channelTemplates = []; |
||
| 21 | |||
| 22 | public function setEnabledChannels(array $channels) |
||
| 23 | { |
||
| 24 | $this->channels = $channels; |
||
| 25 | } |
||
| 26 | |||
| 27 | protected function configure() |
||
| 28 | { |
||
| 29 | $this |
||
| 30 | ->setName('notification:create') |
||
| 31 | ->setDescription('Create a new notification class') |
||
| 32 | ->setDefinition( |
||
| 33 | [ |
||
| 34 | new InputOption('bundle', '', InputOption::VALUE_REQUIRED, 'The bundle for this notification'), |
||
| 35 | new InputOption( |
||
| 36 | 'notification_name', '', InputOption::VALUE_REQUIRED, 'The name of the notification' |
||
| 37 | ), |
||
| 38 | new InputOption( |
||
| 39 | 'channel_templates', '', InputOption::VALUE_REQUIRED, 'The name of the notification' |
||
| 40 | ), |
||
| 41 | ] |
||
| 42 | ); |
||
| 43 | } |
||
| 44 | |||
| 45 | protected function interact(InputInterface $input, OutputInterface $output) |
||
| 46 | { |
||
| 47 | $questionHelper = $this->getQuestionHelper(); |
||
|
0 ignored issues
–
show
|
|||
| 48 | $questionHelper->writeSection($output, 'Welcome to the Notification generator'); |
||
| 49 | |||
| 50 | // Get the Bundle to generate it in |
||
| 51 | $output->writeln( |
||
| 52 | [ |
||
| 53 | 'This command helps you generate a Notification class', |
||
| 54 | '', |
||
| 55 | 'First, give the name of the bundle to generate the notification in (eg <comment>AppBundle</comment>)', |
||
| 56 | ] |
||
| 57 | ); |
||
| 58 | |||
| 59 | $question = new Question( |
||
| 60 | $questionHelper->getQuestion('The bundle name', $input->getOption('bundle')), |
||
| 61 | $input->getOption('bundle') |
||
| 62 | ); |
||
| 63 | |||
| 64 | // @TODO: Add existing bundle validation |
||
| 65 | $question->setValidator(['Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateBundleName']); |
||
| 66 | $question->setNormalizer( |
||
| 67 | function ($value) { |
||
| 68 | return $value ? trim($value) : ''; |
||
| 69 | } |
||
| 70 | ); |
||
| 71 | $question->setMaxAttempts(2); |
||
| 72 | |||
| 73 | $bundle = $questionHelper->ask($input, $output, $question); |
||
| 74 | $input->setOption('bundle', $bundle); |
||
| 75 | |||
| 76 | // Get the Bundle to generate it in |
||
| 77 | $output->writeln( |
||
| 78 | [ |
||
| 79 | '', |
||
| 80 | 'Now, give the name of the new notification class (eg <comment>NewMember</comment>)', |
||
| 81 | ] |
||
| 82 | ); |
||
| 83 | |||
| 84 | // Get the new class name and validate it. |
||
| 85 | $question = new Question( |
||
| 86 | $questionHelper->getQuestion('The notification name', $input->getOption('notification_name')), |
||
| 87 | $input->getOption('notification_name') |
||
| 88 | ); |
||
| 89 | $question->setValidator( |
||
| 90 | function ($answer) { |
||
| 91 | // Should only contain letters. |
||
| 92 | $valid = preg_match('/^[a-zA-Z]+$/', $answer); |
||
| 93 | if (!$valid) { |
||
| 94 | throw new \RuntimeException( |
||
| 95 | 'The class name should only contain letters' |
||
| 96 | ); |
||
| 97 | } |
||
| 98 | |||
| 99 | return $answer; |
||
| 100 | } |
||
| 101 | ); |
||
| 102 | $question->setNormalizer( |
||
| 103 | function ($value) { |
||
| 104 | return $value ? trim($value) : ''; |
||
| 105 | } |
||
| 106 | ); |
||
| 107 | |||
| 108 | $notificationName = $questionHelper->ask($input, $output, $question); |
||
| 109 | $input->setOption('notification_name', $notificationName); |
||
| 110 | |||
| 111 | // ask whether to generate templates for enabled channels. |
||
| 112 | foreach ($this->channels as $channel) { |
||
| 113 | $question = $this->createYesNoQuestion($questionHelper, $input, $channel); |
||
| 114 | |||
| 115 | $generateTemplate = $questionHelper->ask($input, $output, $question); |
||
| 116 | if ($generateTemplate == 'y') { |
||
| 117 | $this->channelTemplates[] = $channel; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 123 | { |
||
| 124 | $questionHelper = $this->getQuestionHelper(); |
||
|
0 ignored issues
–
show
The method
getQuestionHelper() does not seem to exist on object<IrishDan\Notifica...ateNotificationCommand>.
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||
| 125 | |||
| 126 | View Code Duplication | if ($input->isInteractive()) { |
|
| 127 | $question = new ConfirmationQuestion( |
||
| 128 | $questionHelper->getQuestion('Do you confirm generation', 'yes', '?'), |
||
| 129 | true |
||
| 130 | ); |
||
| 131 | if (!$questionHelper->ask($input, $output, $question)) { |
||
| 132 | $output->writeln('<error>Command aborted</error>'); |
||
| 133 | |||
| 134 | return 1; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | $style = new SymfonyStyle($input, $output); |
||
| 139 | |||
| 140 | $bundle = $input->getOption('bundle'); |
||
| 141 | $name = $input->getOption('notification_name'); |
||
| 142 | |||
| 143 | $style->text('Generating New notification class ' . $name . ' generated in ' . $bundle); |
||
| 144 | |||
| 145 | try { |
||
| 146 | $bundle = $this->getContainer()->get('kernel')->getBundle($bundle); |
||
|
0 ignored issues
–
show
The method
getContainer() does not seem to exist on object<IrishDan\Notifica...ateNotificationCommand>.
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||
| 147 | } catch (\Exception $e) { |
||
| 148 | $output->writeln(sprintf('<bg=red>Bundle "%s" does not exist.</>', $bundle)); |
||
| 149 | } |
||
| 150 | |||
| 151 | $generator = $this->getGenerator($bundle); |
||
|
0 ignored issues
–
show
The method
getGenerator() does not seem to exist on object<IrishDan\Notifica...ateNotificationCommand>.
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||
| 152 | $generator->generate($bundle, $name); |
||
| 153 | |||
| 154 | $output->writeln( |
||
| 155 | sprintf('Generated the <info>%s</info> notification in <info>%s</info>', $name, $bundle->getName()) |
||
| 156 | ); |
||
| 157 | $questionHelper->writeGeneratorSummary($output, []); |
||
| 158 | } |
||
| 159 | |||
| 160 | protected function createYesNoQuestion($questionHelper, $input, $channel) |
||
|
0 ignored issues
–
show
|
|||
| 161 | { |
||
| 162 | $question = new Question( |
||
| 163 | $questionHelper->getQuestion( |
||
| 164 | 'Generate a message template for the "' . $channel . '" adapter <comment>[yes]</comment>', |
||
| 165 | 'channel_templates' |
||
| 166 | ), 'yes' |
||
| 167 | ); |
||
| 168 | $question->setNormalizer( |
||
| 169 | function ($value) { |
||
| 170 | return $value[0] == 'y' ? 'y' : 'n'; |
||
| 171 | } |
||
| 172 | ); |
||
| 173 | |||
| 174 | $question->setValidator( |
||
| 175 | function ($answer) { |
||
| 176 | // Should only contain letters. |
||
| 177 | $allowed = [ |
||
| 178 | 'y', |
||
| 179 | 'n', |
||
| 180 | ]; |
||
| 181 | $valid = in_array($answer, $allowed); |
||
| 182 | if (!$valid) { |
||
| 183 | throw new \RuntimeException( |
||
| 184 | 'Only allowed value are ' . implode(', ', $allowed) |
||
| 185 | ); |
||
| 186 | } |
||
| 187 | |||
| 188 | return $answer; |
||
| 189 | } |
||
| 190 | ); |
||
| 191 | |||
| 192 | return $question; |
||
| 193 | } |
||
| 194 | |||
| 195 | protected function createGenerator() |
||
| 196 | { |
||
| 197 | return new NotificationGenerator( |
||
| 198 | $this->getContainer()->get('filesystem'), |
||
|
0 ignored issues
–
show
The method
getContainer() does not seem to exist on object<IrishDan\Notifica...ateNotificationCommand>.
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||
| 199 | $this->channelTemplates, |
||
| 200 | $this->getContainer()->getParameter('kernel.root_dir') |
||
|
0 ignored issues
–
show
The method
getContainer() does not seem to exist on object<IrishDan\Notifica...ateNotificationCommand>.
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||
| 201 | ); |
||
| 202 | } |
||
| 203 | } |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.