Completed
Push — master ( 6e92d2...d7f3a5 )
by
unknown
109:07 queued 86:32
created

GeneratorBundle/Command/GenerateArticleCommand.php (4 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\ArticleGenerator;
6
use Symfony\Component\Console\Input\InputOption;
7
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
8
9
/**
10
 * Generates classes based on the AbstractArticle classes from KunstmaanArticleBundle
11
 */
12
class GenerateArticleCommand extends KunstmaanGenerateCommand
13
{
14
    /**
15
     * @var BundleInterface
16
     */
17
    private $bundle;
18
19
    /**
20
     * @var string
21
     */
22
    private $entity;
23
24
    /**
25
     * @var string
26
     */
27
    private $prefix;
28
29
    /**
30
     * @var string
31
     */
32
    private $bundleWithHomePage;
33
34
    /**
35
     * @var array
36
     */
37
    private $parentPages = array();
38
39
    /**
40
     * @var bool
41
     */
42
    private $usesAuthor;
43
44
    /**
45
     * @var bool
46
     */
47
    private $usesCategories;
48
49
    /**
50
     * @var bool
51
     */
52
    private $usesTags;
53
54
    /**
55
     * @var bool
56
     */
57
    private $dummydata;
58
59
    /**
60
     * @see Command
61
     */
62
    protected function configure()
63
    {
64
        $this
65
            ->setDefinition(
66
                array(
67
                    new InputOption('namespace', '', InputOption::VALUE_REQUIRED, 'The namespace to generate the Article classes in'),
68
                    new InputOption('entity', '', InputOption::VALUE_REQUIRED, 'The article class name ("News", "Press", ..."'),
69
                    new InputOption('prefix', '', InputOption::VALUE_OPTIONAL, 'The prefix to be used in the table names of the generated entities'),
70
                    new InputOption('namespacehomepage', '', InputOption::VALUE_OPTIONAL, 'The namespace of the home page entity'),
71
                    new InputOption('articleoverviewpageparent', '', InputOption::VALUE_OPTIONAL, 'Shortnames of the pages that can have the article overview page as a child (comma separated)'),
72
                    new InputOption('with-author', null, InputOption::VALUE_NONE, 'If set, you can use authors'),
73
                    new InputOption('with-category', null, InputOption::VALUE_NONE, 'If set, you can use categories'),
74
                    new InputOption('with-tag', null, InputOption::VALUE_NONE, 'If set, the you can use tags'),
75
                    new InputOption('dummydata', null, InputOption::VALUE_NONE, 'If set, the task will generate data fixtures to populate your database')
76
                )
77
            )
78
            ->setDescription('Generates Article classes based on KunstmaanArticleBundle')
79
            ->setHelp(<<<EOT
80
The <info>kuma:generate:article</info> command generates classes for Articles using the KunstmaanArticleBundle
81
82
<info>php bin/console kuma:generate:article --namespace=Namespace/NamedBundle --entity=Article</info>
83
84
Use the <info>--prefix</info> option to add a prefix to the table names of the generated entities
85
86
<info>php bin/console kuma:generate:article --namespace=Namespace/NamedBundle --prefix=demo_</info>
87
88
Add the <info>--dummydata</info> option to create data fixtures to populate your database
89
90
<info>php bin/console kuma:generate:article --namespace=Namespace/NamedBundle --dummydata</info>
91
EOT
92
            )
93
            ->setName('kuma:generate:article');
94
    }
95
96
    /**
97
     * @return ArticleGenerator
98
     */
99 View Code Duplication
    protected function createGenerator()
100
    {
101
        $filesystem = $this->getContainer()->get('filesystem');
102
        $registry = $this->getContainer()->get('doctrine');
103
104
        return new ArticleGenerator($filesystem, $registry, '/article', $this->parentPages, $this->assistant, $this->getContainer());
105
    }
106
107
    /**
108
     * Do the interaction with the end user.
109
     */
110
    protected function doInteract()
111
    {
112
        /**
113
         * Ask for which bundle we need to create the layout
114
         */
115
        $bundleNamespace = $this->assistant->getOptionOrDefault('namespace', null);
116
        $this->bundle = $this->askForBundleName('article classes', $bundleNamespace);
117
118
        /**
119
         * Entity
120
         */
121
        $this->entity = $this->assistant->getOptionOrDefault('entity', null);
122
123
        if (is_null($this->entity)) {
124
            $this->assistant->writeLine(array(
125
                'You must specify a name for the collection of Article entities.',
126
                'This name will be prefixed before every new entity.',
127
                'For example entering <comment>News</comment> will result in:',
128
                '<comment>News</comment>OverviewPage, <comment>News</comment>Page and <comment>News</comment>Author'
129
            ));
130
131
            $generator = $this->getGenerator();
132
            $this->entity = $this->assistant->askAndValidate(
133
                'Article class name',
134
                function ($name) use ($generator) {
135
                    // Check reserved words
136
                    if ($generator->isReservedKeyword($name)){
137
                        throw new \InvalidArgumentException(sprintf('"%s" is a reserved word', $name));
138
                    }
139
                    if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $name)) {
140
                        throw new \InvalidArgumentException(sprintf('Invalid entity name: %s', $name));
141
                    }
142
                    return $name;
143
                }
144
            );
145
        }
146
147
        /**
148
         * Ask if author will be used
149
         */
150
        $this->usesAuthor = $this->askForAuthor();
151
152
        /**
153
         * Ask if category will be used
154
         */
155
        $this->usesCategories = $this->askForCategories();
156
157
        /**
158
         * Ask if tag will be used
159
         */
160
        $this->usesTags = $this->askForTags();
161
162
        /**
163
         * Ask the parent pages
164
         */
165
        $bundleWithHomePageNamespace = $this->assistant->getOptionOrDefault('namespacehomepage', null);
166
        $this->bundleWithHomePage = $this->askForBundleName('', $bundleWithHomePageNamespace, 'In which bundle is the page that you will use as parent for the overview page ?', "%sThe bundle %s will be used for the parent of the overview page.\n");
167
        $parentPages = $this->getAvailablePages($this->bundleWithHomePage);
168
        $pagesSelect = array_map(function ($item) { return $item['name']; }, $parentPages);
169
        if (count($pagesSelect) > 0) {
170
            $this->assistant->writeLine('');
171
            $parentPageNames = $this->assistant->getOptionOrDefault('articleoverviewpageparent', null);
172
            if (null !== $parentPageNames) {
173
                $parentPageNames = explode(',', $parentPageNames);
174
                foreach ($parentPageNames as $parentPageName) {
175
                    $id = array_search($parentPageName, $pagesSelect);
176
                    if (false !== $id) {
177
                        $this->parentPages[] = $parentPages[$id]['path'];
178
                    }
179
                }
180
            }
181
182
            if (empty($this->parentPages)) {
183
                $parentPageIds = $this->assistant->askSelect('Which existing page(s) can have the new overview page as sub-page (multiple possible, separated by comma)', $pagesSelect, null, true);
184
                foreach ($parentPageIds as $id) {
185
                    $this->parentPages[] = $parentPages[$id]['path'];
186
                }
187
            }
188
        }
189
190
        /**
191
         * Ask the database table prefix
192
         */
193
        $this->prefix = $this->assistant->getOptionOrDefault('prefix', null);
194
        if (is_null($this->prefix)) {
195
            $this->prefix = $this->askForPrefix(null, $this->bundle->getNamespace());
196
        }
197
198
        /**
199
         * Ask for data fixtures
200
         */
201
        $this->dummydata = $this->askForDummydata();
202
    }
203
204
    /**
205
     * This function implements the final execution of the Generator.
206
     * It calls the execute function with the correct parameters.
207
     */
208
    protected function doExecute()
209
    {
210
        $this->assistant->writeSection('Article classes generation');
211
212
        $this->createGenerator()->generate($this->bundle, $this->entity, $this->prefix, $this->getContainer()->getParameter('multilanguage'), $this->usesAuthor, $this->usesCategories, $this->usesTags, $this->bundleWithHomePage, $this->dummydata);
213
214
        $this->assistant->writeSection('Article classes successfully created', 'bg=green;fg=black');
215
        $this->assistant->writeLine(array(
216
            'Make sure you update your database first before you test the pagepart:',
217
            '    Directly update your database:          <comment>bin/console doctrine:schema:update --force</comment>',
218
            '    Create a Doctrine migration and run it: <comment>bin/console doctrine:migrations:diff && bin/console doctrine:migrations:migrate</comment>'
219
        ));
220
    }
221
222
    /**
223
     * The text to be displayed on top of the generator.
224
     *
225
     * @return string|array
226
     */
227
    protected function getWelcomeText()
228
    {
229
        return 'Welcome to the Kunstmaan article generator';
230
    }
231
232
    /**
233
     * @return bool
234
     */
235 View Code Duplication
    protected function askForDummydata()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
236
    {
237
        $dummydataOption = $this->assistant->getOption('dummydata');
238
        if ($dummydataOption != 'y' && $dummydataOption != 'n') {
239
            /** @var  $question */
240
            $dummydataOption = $this->assistant->askConfirmation("\nDo you want to generate data fixtures to populate your database ? (y/n)\n", 'n', '?', false);
241
        }
242
        return $dummydataOption == 'y';
243
    }
244
245
    /**
246
     * @return bool
247
     */
248 View Code Duplication
    protected function askForCategories()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
249
    {
250
        $categoryOption = $this->assistant->getOption('with-category');
251
        if ($categoryOption != 'y' && $categoryOption != 'n') {
252
            /** @var  $question */
253
            $categoryOption = $this->assistant->askConfirmation("\nDo you want to use categories ? (y/n)\n", 'y', '?', true);
254
        }
255
        return $categoryOption == 'y';
256
    }
257
258
    /**
259
     * @return bool
260
     */
261 View Code Duplication
    protected function askForTags()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
262
    {
263
        $tagOption = $this->assistant->getOption('with-tag');
264
        if ($tagOption != 'y' && $tagOption != 'n') {
265
            /** @var  $question */
266
            $tagOption = $this->assistant->askConfirmation("\nDo you want to use tags ? (y/n)\n", 'y', '?', true);
267
        }
268
        return $tagOption == 'y';
269
    }
270
271
    /**
272
     * @return bool
273
     */
274 View Code Duplication
    protected function askForAuthor()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
275
    {
276
        $authorOption = $this->assistant->getOption('with-author');
277
        if ($authorOption != 'y' && $authorOption != 'n') {
278
            /** @var  $question */
279
            $authorOption = $this->assistant->askConfirmation("\nDo you want to authors ? (y/n)\n", 'y', '?', true);
280
        }
281
        return $authorOption == 'y';
282
    }
283
284
}
285