Passed
Push — develop ( ba64a1...18b4ab )
by nguereza
03:11
created

MakeCrudActionCommand::getMessageTemplates()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 21
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 32
rs 9.584
1
<?php
2
3
/**
4
 * Platine PHP
5
 *
6
 * Platine Framework is a lightweight, high-performance, simple and elegant
7
 * PHP Web framework
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine PHP
12
 *
13
 * Permission is hereby granted, free of charge, to any person obtaining a copy
14
 * of this software and associated documentation files (the "Software"), to deal
15
 * in the Software without restriction, including without limitation the rights
16
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
 * copies of the Software, and to permit persons to whom the Software is
18
 * furnished to do so, subject to the following conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all
21
 * copies or substantial portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
 * SOFTWARE.
30
 */
31
32
/**
33
 *  @file MakeCrudActionCommand.php
34
 *
35
 *  The Command to generate new CRUD action class
36
 *
37
 *  @package    Platine\Framework\Console\Command
38
 *  @author Platine Developers team
39
 *  @copyright  Copyright (c) 2020
40
 *  @license    http://opensource.org/licenses/MIT  MIT License
41
 *  @link   https://www.platine-php.com
42
 *  @version 1.0.0
43
 *  @filesource
44
 */
45
46
declare(strict_types=1);
47
48
namespace Platine\Framework\Console\Command;
49
50
use Platine\Filesystem\Filesystem;
51
use Platine\Framework\App\Application;
52
use Platine\Framework\Console\BaseMakeActionCommand;
53
use Platine\Stdlib\Helper\Str;
54
55
/**
56
 * @class MakeCrudActionCommand
57
 * @package Platine\Framework\Console\Command
58
 */
59
class MakeCrudActionCommand extends BaseMakeActionCommand
60
{
61
    /**
62
     * {@inheritdoc}
63
     */
64
    protected string $type = 'crud';
65
66
    /**
67
     * Create new instance
68
     * @param Application $application
69
     * @param Filesystem $filesystem
70
     */
71
    public function __construct(
72
        Application $application,
73
        Filesystem $filesystem
74
    ) {
75
        parent::__construct($application, $filesystem);
76
        $this->setName('make:crud')
77
              ->setDescription('Command to generate platine CRUD action');
78
    }
79
80
     /**
81
     * {@inheritdoc}
82
     */
83
    public function getClassTemplate(): string
84
    {
85
        return <<<EOF
86
        <?php
87
        
88
        declare(strict_types=1);
89
        
90
        namespace %namespace%;
91
        
92
        use Platine\Framework\Helper\Flash;
93
        use Platine\Framework\Http\Action\CrudAction;
94
        use Platine\Framework\Http\RouteHelper;
95
        use Platine\Lang\Lang;
96
        use Platine\Logger\LoggerInterface;
97
        use Platine\Pagination\Pagination;
98
        use Platine\Template\Template;
99
        %uses%
100
101
        /**
102
        * @class %classname%
103
        * @package %namespace%
104
        * @extends CrudAction<%base_entity%>
105
        */
106
        class %classname% extends CrudAction
107
        {
108
            
109
            %attributes%
110
            /**
111
            * {@inheritdoc}
112
            */
113
            protected string \$paramClass = %base_param%::class;
114
115
            /**
116
            * {@inheritdoc}
117
            */
118
            protected string \$validatorClass = %base_validator%::class;
119
        
120
            /**
121
            * Create new instance
122
            * {@inheritdoc}
123
            * @param %base_repository%<%base_entity%> \$repository
124
            */
125
            public function __construct(
126
                Lang \$lang,
127
                Pagination \$pagination,
128
                Template \$template,
129
                Flash \$flash,
130
                RouteHelper \$routeHelper,
131
                LoggerInterface \$logger,
132
                %base_repository% \$repository
133
            ) {
134
                parent::__construct(
135
                    \$lang,
136
                    \$pagination,
137
                    \$template,
138
                    \$flash,
139
                    \$routeHelper,
140
                    \$logger
141
                );
142
                \$this->repository = \$repository;
143
            }
144
        }
145
        
146
        EOF;
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    protected function createClass(): string
153
    {
154
        $content = parent::createClass();
155
        $entityBaseClass = $this->getClassBaseName($this->entityClass);
156
        $repositoryBaseClass = $this->getClassBaseName($this->repositoryClass);
157
        $paramBaseClass = $this->getClassBaseName($this->paramClass);
158
        $validatorBaseClass = $this->getClassBaseName($this->validatorClass);
159
160
        $replace = '';
161
        $fields = $this->getFieldsPropertyBody();
162
        if (!empty($fields)) {
163
            $replace .= $fields . PHP_EOL . PHP_EOL;
164
        }
165
166
        $orderFields = $this->getOrderByFieldsPropertyBody();
167
        if (!empty($orderFields)) {
168
            $replace .= $orderFields . PHP_EOL . PHP_EOL;
169
        }
170
171
        $uniqueFields = $this->getUniqueFieldsPropertyBody();
172
        if (!empty($uniqueFields)) {
173
            $replace .= $uniqueFields . PHP_EOL . PHP_EOL;
174
        }
175
176
        $templatePrefix = $this->getTemplatePrefix();
177
        if (!empty($templatePrefix)) {
178
            $replace .= <<<EOF
179
            /**
180
            * {@inheritdoc}
181
            */
182
            protected string \$templatePrefix = '$templatePrefix';
183
        
184
        
185
        EOF;
186
        }
187
188
        $routePrefix = $this->getRoutePrefix();
189
        if (!empty($routePrefix)) {
190
            $replace .= <<<EOF
191
            /**
192
            * {@inheritdoc}
193
            */
194
            protected string \$routePrefix = '$routePrefix';
195
        
196
        
197
        EOF;
198
        }
199
200
        $entityContextName = $this->getEntityContextKey(true);
201
        $optionEntityContext = $this->getOptionForArgument('--entity-context-key');
202
        if ($optionEntityContext !== null && $optionEntityContext->getDefault() !== $entityContextName) {
203
            $replace .= <<<EOF
204
            /**
205
            * {@inheritdoc}
206
            */
207
            protected string \$entityContextName = '$entityContextName';
208
        
209
        
210
        EOF;
211
        }
212
213
        $messages = $this->getMessageTemplates();
214
        if (!empty($messages)) {
215
            $replace .= $messages;
216
        }
217
218
219
        $content = str_replace('%base_entity%', $entityBaseClass, $content);
220
        $content = str_replace('%base_repository%', $repositoryBaseClass, $content);
221
        $content = str_replace('%base_param%', $paramBaseClass, $content);
222
        $content = str_replace('%base_validator%', $validatorBaseClass, $content);
223
224
        return str_replace('%attributes%', $replace, $content);
225
    }
226
227
    /**
228
     * Return the messages template
229
     * @return string
230
     */
231
    protected function getMessageTemplates(): string
232
    {
233
        $replace = '';
234
235
        $messages = [
236
            'create',
237
            'update',
238
            'delete',
239
            'duplicate',
240
            'not-found',
241
            'process-error',
242
        ];
243
244
        foreach ($messages as $val) {
245
            $optionName = sprintf('--message-%s', $val);
246
            $optionKey = sprintf('message%s', Str::camel($val, false));
247
248
            $message = $this->getMessage($optionKey);
249
            $option = $this->getOptionForArgument($optionName);
250
            if ($option !== null && addslashes($option->getDefault()) !== $message) {
0 ignored issues
show
Bug introduced by
It seems like $option->getDefault() can also be of type array; however, parameter $string of addslashes() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

250
            if ($option !== null && addslashes(/** @scrutinizer ignore-type */ $option->getDefault()) !== $message) {
Loading history...
251
                $replace .= <<<EOF
252
                /**
253
                * {@inheritdoc}
254
                */
255
                protected string \${$optionKey} = '$message';
256
257
258
            EOF;
259
            }
260
        }
261
262
        return $replace;
263
    }
264
265
    /**
266
     * Return the fields property body
267
     * @return string
268
     */
269
    protected function getFieldsPropertyBody(): string
270
    {
271
        $fields = $this->getOptionValue('fields');
272
        if ($fields === null) {
273
            return '';
274
        }
275
276
        $columns = $this->formatFields($fields);
277
        $str = $this->formatFieldStr($columns);
278
279
        $result = <<<EOF
280
        /**
281
            * {@inheritdoc}
282
            */
283
            protected array \$fields = [$str];
284
        EOF;
285
286
        return $result;
287
    }
288
289
    /**
290
     * Return the order by fields property body
291
     * @return string
292
     */
293
    protected function getOrderByFieldsPropertyBody(): string
294
    {
295
        $fields = $this->getOptionValue('fieldsOrder');
296
297
        if ($fields === null) {
298
            return '';
299
        }
300
301
        $columns = $this->formatFields($fields);
302
        $str = $this->formatFieldStr($columns, true);
303
304
        $result = <<<EOF
305
            /**
306
            * {@inheritdoc}
307
            */
308
            protected array \$orderFields = [$str];
309
        EOF;
310
311
        return $result;
312
    }
313
314
    /**
315
     * Return the unique fields property body
316
     * @return string
317
     */
318
    protected function getUniqueFieldsPropertyBody(): string
319
    {
320
        $fields = $this->getOptionValue('fieldsUnique');
321
322
        if ($fields === null) {
323
            return '';
324
        }
325
326
        $columns = $this->formatFields($fields);
327
        $str = $this->formatFieldStr($columns);
328
329
        $result = <<<EOF
330
            /**
331
            * {@inheritdoc}
332
            */
333
            protected array \$uniqueFields = [$str];
334
        EOF;
335
336
        return $result;
337
    }
338
339
    /**
340
     * {@inheritdoc}
341
     */
342
    protected function getUsesContent(): string
343
    {
344
        $uses = parent::getUsesContent();
345
346
        $uses .= $this->getUsesTemplate($this->entityClass);
347
        $uses .= $this->getUsesTemplate($this->paramClass);
348
        $uses .= $this->getUsesTemplate($this->validatorClass);
349
        $uses .= $this->getUsesTemplate($this->repositoryClass);
350
351
        return <<<EOF
352
        $uses
353
        EOF;
354
    }
355
}
356