Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

Command/GeneratePagePartCommand.php (1 issue)

assigning incompatible types to properties.

Bug Documentation Major

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\PagePartGenerator;
6
use Symfony\Component\Console\Input\InputOption;
7
8
/**
9
 * Generates a new pagepart
10
 */
11
class GeneratePagePartCommand extends KunstmaanGenerateCommand
12
{
13
    /**
14
     * @var BundleInterface
15
     */
16
    private $bundle;
17
18
    /**
19
     * @var string
20
     */
21
    private $prefix;
22
23
    /**
24
     * @var string
25
     */
26
    private $pagepartName;
27
28
    /**
29
     * @var array
30
     */
31
    private $fields;
32
33
    /**
34
     * @var array
35
     */
36
    private $sections = array();
37
38
    /**
39
     * @var bool
40
     */
41
    private $behatTest;
42
43
    /**
44
     * @see Command
45
     */
46
    protected function configure()
47
    {
48
        $this->setDescription('Generates a new pagepart')
49
            ->setHelp(<<<'EOT'
50
The <info>kuma:generate:pagepart</info> command generates a new pagepart and the pagepart configuration.
51
52
<info>php bin/console kuma:generate:pagepart</info>
53
EOT
54
            )
55
            ->addOption('prefix', '', InputOption::VALUE_OPTIONAL, 'The prefix to be used in the table name of the generated entity')
56
            ->setName('kuma:generate:pagepart');
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    protected function getWelcomeText()
63
    {
64
        return 'Welcome to the Kunstmaan pagepart generator';
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    protected function doExecute()
71
    {
72
        $this->assistant->writeSection('PagePart generation');
73
74
        $this->createGenerator()->generate($this->bundle, $this->pagepartName, $this->prefix, $this->fields, $this->sections, $this->behatTest);
75
76
        $this->assistant->writeSection('PagePart successfully created', 'bg=green;fg=black');
77
        $this->assistant->writeLine(array(
78
            'Make sure you update your database first before you test the pagepart:',
79
            '    Directly update your database:          <comment>bin/console doctrine:schema:update --force</comment>',
80
            '    Create a Doctrine migration and run it: <comment>bin/console doctrine:migrations:diff && bin/console doctrine:migrations:migrate</comment>',
81
            ($this->behatTest ? 'A new behat test is created, to run it: <comment>bin/behat --tags \'@'.$this->pagepartName.'\' @'.$this->bundle->getName().'</comment>' : ''),
82
        ));
83
84
        return 0;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    protected function doInteract()
91
    {
92
        if (!$this->isBundleAvailable('KunstmaanPagePartBundle')) {
93
            $this->assistant->writeError('KunstmaanPagePartBundle not found', true);
94
        }
95
96
        $this->assistant->writeLine(array("This command helps you to generate a new pagepart.\n"));
97
98
        /*
99
         * Ask for which bundle we need to create the pagepart
100
         */
101
        $this->bundle = $this->askForBundleName('pagepart');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->askForBundleName('pagepart') of type object<Symfony\Component...Bundle\BundleInterface> is incompatible with the declared type object<Kunstmaan\Generat...ommand\BundleInterface> of property $bundle.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
102
103
        /*
104
         * Ask the database table prefix
105
         */
106
        $this->prefix = $this->askForPrefix(null, $this->bundle->getNamespace());
107
108
        /*
109
         * Ask the name of the pagepart
110
         */
111
        $this->assistant->writeLine(array(
112
            '',
113
            'The name of your PagePart: For example: <comment>ContentBoxPagePart</comment>',
114
            '',
115
        ));
116
        $generator = $this->getGenerator();
117
        $bundlePath = $this->bundle->getPath();
118
        $name = $this->assistant->askAndValidate(
119
            'PagePart name',
120 View Code Duplication
            function ($name) use ($generator, $bundlePath) {
121
                // Check reserved words
122
                if ($generator->isReservedKeyword($name)) {
123
                    throw new \InvalidArgumentException(sprintf('"%s" is a reserved word', $name));
124
                }
125
126
                // Name should end on PagePart
127
                if (!preg_match('/PagePart$/', $name)) {
128
                    throw new \InvalidArgumentException('The pagepart name must end with PagePart');
129
                }
130
131
                // Name should contain more characters than PagePart
132
                if (strlen($name) <= strlen('PagePart') || !preg_match('/^[a-zA-Z]+$/', $name)) {
133
                    throw new \InvalidArgumentException('Invalid pagepart name');
134
                }
135
136
                // Check that entity does not already exist
137
                if (file_exists($bundlePath.'/Entity/PageParts/'.$name.'.php')) {
138
                    throw new \InvalidArgumentException(sprintf('PagePart or entity "%s" already exists', $name));
139
                }
140
141
                return $name;
142
            }
143
        );
144
        $this->pagepartName = $name;
145
146
        /*
147
         * Ask which fields need to be present
148
         */
149
        $this->assistant->writeLine(array("\nInstead of starting with a blank pagepart, you can add some fields now.\n"));
150
        $fields = $this->askEntityFields($this->bundle);
151
        $this->fields = array();
152
        foreach ($fields as $fieldInfo) {
153
            if ($fieldInfo['type'] == 'image') {
154
                $this->fields[] = $this->getEntityFields($this->bundle, $this->pagepartName, $this->prefix, $fieldInfo['name'], $fieldInfo['type'],
155
                    $fieldInfo['extra'], true, $fieldInfo['minHeight'], $fieldInfo['maxHeight'], $fieldInfo['minWidth'], $fieldInfo['maxWidth'], $fieldInfo['mimeTypes']);
156
            } elseif ($fieldInfo['type'] == 'media') {
157
                $this->fields[] = $this->getEntityFields($this->bundle, $this->pagepartName, $this->prefix, $fieldInfo['name'], $fieldInfo['type'],
158
                    $fieldInfo['extra'], true, null, null, null, null, $fieldInfo['mimeTypes']);
159
            } else {
160
                $this->fields[] = $this->getEntityFields($this->bundle, $this->pagepartName, $this->prefix, $fieldInfo['name'], $fieldInfo['type'], $fieldInfo['extra'], true);
161
            }
162
        }
163
164
        /**
165
         * Ask for which page sections we should enable this pagepart
166
         */
167
        $question = 'In which page section configuration file(s) do you want to add the pagepart (multiple possible, separated by comma)';
168
        $this->sections = $this->askForSections($question, $this->bundle, true);
169
170
        /*
171
         * Ask that you want to create behat tests for the new pagepart, if possible
172
         */
173
        if (count($this->sections) > 0 && $this->canGenerateBehatTests($this->bundle)) {
174
            $this->behatTest = $this->assistant->askConfirmation('Do you want to generate behat tests for this pagepart? (y/n)', 'y');
175
        } else {
176
            $this->behatTest = false;
177
        }
178
    }
179
180
    /**
181
     * Get the generator.
182
     *
183
     * @return PagePartGenerator
184
     */
185 View Code Duplication
    protected function createGenerator()
186
    {
187
        $filesystem = $this->getContainer()->get('filesystem');
188
        $registry = $this->getContainer()->get('doctrine');
189
190
        return new PagePartGenerator($filesystem, $registry, '/pagepart', $this->assistant, $this->getContainer());
191
    }
192
}
193