Completed
Push — master ( 06c1ce...67d37c )
by Jeroen
06:20
created

Command/GenerateFormPageCommand.php (2 issues)

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 Kunstmaan\GeneratorBundle\Command;
4
5
use Kunstmaan\GeneratorBundle\Generator\FormPageGenerator;
6
use Symfony\Component\Console\Input\ArrayInput;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\ConsoleOutput;
9
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
10
11
/**
12
 * Generates a new formPage
13
 */
14
class GenerateFormPageCommand extends KunstmaanGenerateCommand
15
{
16
    /**
17
     * @var BundleInterface
18
     */
19
    private $bundle;
20
21
    /**
22
     * @var string
23
     */
24
    private $prefix;
25
26
    /**
27
     * @var string
28
     */
29
    private $pageName;
30
31
    /**
32
     * @var array
33
     */
34
    private $fields;
35
36
    /**
37
     * @var array
38
     */
39
    private $template;
40
41
    /**
42
     * @var array
43
     */
44
    private $sections = [];
45
46
    /**
47
     * @var array
48
     */
49
    private $parentPages = [];
50
51
    /**
52
     * @var bool
53
     */
54
    private $generateFormPageParts;
55
56
    /**
57
     * @see Command
58
     */
59
    protected function configure()
60
    {
61
        $this->setDescription('Generates a new formpage')
62
            ->setHelp(<<<'EOT'
63
The <info>kuma:generate:formpage</info> command generates a new formpage and its configuration.
64
65
<info>php bin/console kuma:generate:formpage</info>
66
EOT
67
            )
68
            ->addOption('prefix', '', InputOption::VALUE_OPTIONAL, 'The prefix to be used in the table name of the generated entity')
69
            ->setName('kuma:generate:formpage');
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    protected function getWelcomeText()
76
    {
77
        return 'Welcome to the Kunstmaan formpage generator';
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    protected function doExecute()
84
    {
85
        $this->assistant->writeSection('FormPage generation');
86
        $this->template = strtolower($this->pageName);
87
        $this->sections = array(strtolower($this->pageName));
88
89
        // Generate the default form pageparts if requested.
90
        if ($this->generateFormPageParts) {
91
            $command = $this->getApplication()->find('kuma:generate:form-pageparts');
92
            $arguments = [
93
                'command' => 'kuma:generate:form-pageparts',
94
                '--namespace' => str_replace('\\', '/', $this->bundle->getNamespace()),
95
                '--prefix' => $this->prefix,
96
                '--quiet' => false,
97
            ];
98
99
            $output = new ConsoleOutput(ConsoleOutput::VERBOSITY_DEBUG);
100
            $input = new ArrayInput($arguments);
101
            $command->run($input, $output);
102
            $this->assistant->writeLine('Generating default form pageparts : <info>OK</info>');
103
        }
104
105
        $this->createGenerator()->generate($this->bundle, $this->pageName, $this->prefix, $this->fields, $this->template, $this->sections, $this->parentPages, $this->generateFormPageParts);
106
107
        $this->assistant->writeSection('FormPage successfully created', 'bg=green;fg=black');
108
109 View Code Duplication
        if (count($this->parentPages) == 0) {
110
            $this->assistant->writeLine(array(
111
                'To use this page you must first add the definition below to the <comment>getPossibleChildTypes</comment> funtion of the parent page:',
112
                '<comment>    array(</comment>',
113
                "<comment>        'name' => '".$this->pageName."',</comment>",
114
                "<comment>        'class'=> '".$this->bundle->getNamespace().'\\Entity\\Pages\\'.$this->pageName."'</comment>",
115
                '<comment>    ),</comment>',
116
                '',
117
            ));
118
        }
119
120
        $this->assistant->writeLine(array(
121
            'Make sure you update your database first before you use the page:',
122
            '    Directly update your database:          <comment>bin/console doctrine:schema:update --force</comment>',
123
            '    Create a Doctrine migration and run it: <comment>bin/console doctrine:migrations:diff && bin/console doctrine:migrations:migrate</comment>',
124
            '',
125
        ));
126
127
        return 0;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    protected function doInteract()
134
    {
135
        if (!$this->isBundleAvailable('KunstmaanPagePartBundle')) {
136
            $this->assistant->writeError('KunstmaanPagePartBundle not found', true);
137
        }
138
139
        $this->assistant->writeLine(array("This command helps you to generate a new formpage.\n"));
140
141
        /*
142
         * Ask for which bundle we need to create the pagepart
143
         */
144
        $this->bundle = $this->askForBundleName('page');
145
146
        /*
147
         * Ask the database table prefix
148
         */
149
        $this->prefix = $this->askForPrefix(null, $this->bundle->getNamespace());
150
151
        /*
152
         * Ask the name of the pagepart
153
         */
154
        $this->assistant->writeLine(array(
155
            '',
156
            'The name of your FormPage: For example: <comment>ContactPage</comment>, <comment>OrderPage</comment>',
157
            '',
158
        ));
159
        $generator = $this->getGenerator();
160
        $bundlePath = $this->bundle->getPath();
161
162
        $name = $this->assistant->askAndValidate(
163
            'FormPage name',
164 View Code Duplication
            function ($name) use ($generator, $bundlePath) {
165
                // Check reserved words
166
                if ($generator->isReservedKeyword($name)) {
167
                    throw new \InvalidArgumentException(sprintf('"%s" is a reserved word', $name));
168
                }
169
170
                // Name should end on Page
171
                if (!preg_match('/Page$/', $name)) {
172
                    throw new \InvalidArgumentException('The page name must end with Page');
173
                }
174
175
                // Name should contain more characters than Page
176
                if (strlen($name) <= strlen('Page') || !preg_match('/^[a-zA-Z]+$/', $name)) {
177
                    throw new \InvalidArgumentException('Invalid page name');
178
                }
179
180
                // Check that entity does not already exist
181
                if (file_exists($bundlePath . '/Entity/Pages/' . $name . '.php')) {
182
                    throw new \InvalidArgumentException(sprintf('Page or entity "%s" already exists', $name));
183
                }
184
185
                return $name;
186
            }
187
        );
188
        $this->pageName = $name;
189
190
        /*
191
         * Ask which fields need to be present
192
         */
193
        $this->assistant->writeLine(array("\nInstead of starting with a blank page, you can add some fields now.\n"));
194
        $fields = $this->askEntityFields($this->bundle, array('title', 'pageTitle', 'parent', 'id', 'thanks', 'subject', 'fromEmail', 'toEmail'));
195
        $this->fields = array();
196
        foreach ($fields as $fieldInfo) {
197
            $this->fields[] = $this->getEntityFields($this->bundle, $this->pageName, $this->prefix, $fieldInfo['name'], $fieldInfo['type'], $fieldInfo['extra'], true);
198
        }
199
200
        /**
201
         * Ask the parent pages
202
         */
203
        $parentPages = $this->getAvailablePages($this->bundle);
204
        $pagesSelect = array_map(function ($item) {
205
            return $item['name'];
206
        }, $parentPages);
207 View Code Duplication
        if (count($pagesSelect) > 0) {
208
            $this->assistant->writeLine('');
209
            $parentPageIds = $this->assistant->askSelect('Which existing page(s) can have the new page as sub-page (multiple possible, separated by comma)', $pagesSelect, null, true);
210
            foreach ($parentPageIds as $id) {
211
                $this->parentPages[] = $parentPages[$id]['path'];
212
            }
213
        }
214
215
        // Ask if you want to generate form pageparts.
216
        $this->generateFormPageParts = $this->assistant->askConfirmation('Do you want to generate default form pageparts in your bundle? (y/n)', 'y');
217
    }
218
219
    /**
220
     * Get the generator.
221
     *
222
     * @return FormPageGenerator
223
     */
224 View Code Duplication
    protected function createGenerator()
225
    {
226
        $filesystem = $this->getContainer()->get('filesystem');
227
        $registry = $this->getContainer()->get('doctrine');
228
229
        return new FormPageGenerator($filesystem, $registry, '/page', $this->assistant, $this->getContainer());
0 ignored issues
show
$filesystem is of type object|null, but the function expects a object<Symfony\Component\Filesystem\Filesystem>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
$registry is of type object|null, but the function expects a object<Symfony\Bridge\Doctrine\RegistryInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
230
    }
231
}
232