Completed
Push — master ( ba8ed9...770316 )
by Jeroen
06:11
created

Command/GeneratePagePartCommand.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\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
85
    /**
86
     * {@inheritdoc}
87
     */
88
    protected function doInteract()
89
    {
90
        if (!$this->isBundleAvailable('KunstmaanPagePartBundle')) {
91
            $this->assistant->writeError('KunstmaanPagePartBundle not found', true);
92
        }
93
94
        $this->assistant->writeLine(array("This command helps you to generate a new pagepart.\n"));
95
96
        /*
97
         * Ask for which bundle we need to create the pagepart
98
         */
99
        $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...
100
101
        /*
102
         * Ask the database table prefix
103
         */
104
        $this->prefix = $this->askForPrefix(null, $this->bundle->getNamespace());
105
106
        /*
107
         * Ask the name of the pagepart
108
         */
109
        $this->assistant->writeLine(array(
110
            '',
111
            'The name of your PagePart: For example: <comment>ContentBoxPagePart</comment>',
112
            '',
113
        ));
114
        $generator = $this->getGenerator();
115
        $bundlePath = $this->bundle->getPath();
116
        $name = $this->assistant->askAndValidate(
117
            'PagePart name',
118 View Code Duplication
            function ($name) use ($generator, $bundlePath) {
119
                // Check reserved words
120
                if ($generator->isReservedKeyword($name)) {
121
                    throw new \InvalidArgumentException(sprintf('"%s" is a reserved word', $name));
122
                }
123
124
                // Name should end on PagePart
125
                if (!preg_match('/PagePart$/', $name)) {
126
                    throw new \InvalidArgumentException('The pagepart name must end with PagePart');
127
                }
128
129
                // Name should contain more characters than PagePart
130
                if (strlen($name) <= strlen('PagePart') || !preg_match('/^[a-zA-Z]+$/', $name)) {
131
                    throw new \InvalidArgumentException('Invalid pagepart name');
132
                }
133
134
                // Check that entity does not already exist
135
                if (file_exists($bundlePath.'/Entity/PageParts/'.$name.'.php')) {
136
                    throw new \InvalidArgumentException(sprintf('PagePart or entity "%s" already exists', $name));
137
                }
138
139
                return $name;
140
            }
141
        );
142
        $this->pagepartName = $name;
143
144
        /*
145
         * Ask which fields need to be present
146
         */
147
        $this->assistant->writeLine(array("\nInstead of starting with a blank pagepart, you can add some fields now.\n"));
148
        $fields = $this->askEntityFields($this->bundle);
149
        $this->fields = array();
150
        foreach ($fields as $fieldInfo) {
151
            if ($fieldInfo['type'] == 'image') {
152
                $this->fields[] = $this->getEntityFields($this->bundle, $this->pagepartName, $this->prefix, $fieldInfo['name'], $fieldInfo['type'],
153
                    $fieldInfo['extra'], true, $fieldInfo['minHeight'], $fieldInfo['maxHeight'], $fieldInfo['minWidth'], $fieldInfo['maxWidth'], $fieldInfo['mimeTypes']);
154
            } elseif ($fieldInfo['type'] == 'media') {
155
                $this->fields[] = $this->getEntityFields($this->bundle, $this->pagepartName, $this->prefix, $fieldInfo['name'], $fieldInfo['type'],
156
                    $fieldInfo['extra'], true, null, null, null, null, $fieldInfo['mimeTypes']);
157
            } else {
158
                $this->fields[] = $this->getEntityFields($this->bundle, $this->pagepartName, $this->prefix, $fieldInfo['name'], $fieldInfo['type'], $fieldInfo['extra'], true);
159
            }
160
        }
161
162
        /**
163
         * Ask for which page sections we should enable this pagepart
164
         */
165
        $question = 'In which page section configuration file(s) do you want to add the pagepart (multiple possible, separated by comma)';
166
        $this->sections = $this->askForSections($question, $this->bundle, true);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->askForSections($q...n, $this->bundle, true) can be null. However, the property $sections is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

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

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
167
168
        /*
169
         * Ask that you want to create behat tests for the new pagepart, if possible
170
         */
171
        if (count($this->sections) > 0 && $this->canGenerateBehatTests($this->bundle)) {
172
            $this->behatTest = $this->assistant->askConfirmation('Do you want to generate behat tests for this pagepart? (y/n)', 'y');
173
        } else {
174
            $this->behatTest = false;
175
        }
176
    }
177
178
    /**
179
     * Get the generator.
180
     *
181
     * @return PagePartGenerator
182
     */
183 View Code Duplication
    protected function createGenerator()
184
    {
185
        $filesystem = $this->getContainer()->get('filesystem');
186
        $registry = $this->getContainer()->get('doctrine');
187
188
        return new PagePartGenerator($filesystem, $registry, '/pagepart', $this->assistant, $this->getContainer());
189
    }
190
}
191