Completed
Pull Request — master (#4)
by dan
14:39
created

Command/CreateDatabaseNotificationCommand.php (5 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

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\DatabaseNotificationGenerator;
6
use Symfony\Component\Console\Command\Command;
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
class CreateDatabaseNotificationCommand extends Command
17
{
18
    private $entityName;
19
20
    public function setEntityName(array $databaseChannelConfig)
21
    {
22
        // @TODO: If its not set then we need to ask for it, throw an error
23
        if (!empty($databaseChannelConfig['entity'])) {
24
            $this->entityName = explode(':', $databaseChannelConfig['entity'])[1];
25
        } else {
26
            $this->entityName = 'Notification';
27
        }
28
    }
29
30
    protected function configure()
31
    {
32
        $this
33
            ->setName('notification:create-database-notification')
34
            ->setDescription('Creates a new doctrine database channel notification class')
35
            ->setDefinition([
36
                new InputOption('bundle', '', InputOption::VALUE_REQUIRED, 'The bundle for this notification'),
37
            ]);
38
    }
39
40
    protected function interact(InputInterface $input, OutputInterface $output)
41
    {
42
        $questionHelper = $this->getQuestionHelper();
0 ignored issues
show
The method getQuestionHelper() does not seem to exist on object<IrishDan\Notifica...aseNotificationCommand>.

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...
43
        $questionHelper->writeSection($output, 'Welcome to the database channel Notification generator');
44
45
        // Get the Bundle to generate it in
46
        $output->writeln([
47
            'This command helps you generate a a database channel Notification class',
48
            '',
49
            'First, give the name of the bundle to generate the notification in (eg <comment>AppBundle</comment>)',
50
        ]);
51
52
        $question = new Question($questionHelper->getQuestion('The bundle name', $input->getOption('bundle')), $input->getOption('bundle'));
53
54
        // @TODO: Add existing bundle validation
55
        $question->setValidator(['Sensio\Bundle\GeneratorBundle\Command\Validators', 'validateBundleName']);
56
        $question->setNormalizer(function ($value) {
57
            return $value ? trim($value) : '';
58
        });
59
        $question->setMaxAttempts(2);
60
61
        $bundle = $questionHelper->ask($input, $output, $question);
62
        $input->setOption('bundle', $bundle);
63
    }
64
65
    protected function execute(InputInterface $input, OutputInterface $output)
66
    {
67
        $questionHelper = $this->getQuestionHelper();
0 ignored issues
show
The method getQuestionHelper() does not seem to exist on object<IrishDan\Notifica...aseNotificationCommand>.

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...
68
69 View Code Duplication
        if ($input->isInteractive()) {
70
            $question = new ConfirmationQuestion($questionHelper->getQuestion('Do you confirm generation', 'yes', '?'), true);
71
            if (!$questionHelper->ask($input, $output, $question)) {
72
                $output->writeln('<error>Command aborted</error>');
73
74
                return 1;
75
            }
76
        }
77
78
        $style = new SymfonyStyle($input, $output);
79
80
        $bundle = $input->getOption('bundle');
81
82
        $style->text('Generating new database notification class ' . $this->entityName . ' generated in ' . $bundle);
83
84
        try {
85
            $bundle = $this->getContainer()->get('kernel')->getBundle($bundle);
0 ignored issues
show
The method getContainer() does not seem to exist on object<IrishDan\Notifica...aseNotificationCommand>.

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...
86
        } catch (\Exception $e) {
87
            $output->writeln(sprintf('<bg=red>Bundle "%s" does not exist.</>', $bundle));
88
        }
89
90
        $generator = $this->getGenerator($bundle);
0 ignored issues
show
The method getGenerator() does not seem to exist on object<IrishDan\Notifica...aseNotificationCommand>.

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...
91
        $generator->generate($bundle, $this->entityName);
92
93
        $output->writeln(sprintf('Generated the <info>%s</info> notification in <info>%s</info>', $this->entityName, $bundle->getName()));
94
        $questionHelper->writeGeneratorSummary($output, []);
95
    }
96
97
    protected function createGenerator()
98
    {
99
        return new DatabaseNotificationGenerator(
100
            $this->getContainer()->get('filesystem')
0 ignored issues
show
The method getContainer() does not seem to exist on object<IrishDan\Notifica...aseNotificationCommand>.

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...
101
        );
102
    }
103
}