Passed
Push — master ( 26c354...92479f )
by Richard
05:49 queued 10s
created

ControllerGenerator::generate()   B

Complexity

Conditions 9
Paths 21

Size

Total Lines 51
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 51
rs 8.0555
cc 9
nc 21
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Pwweb\Artomator\Generators\Scaffold;
4
5
use InfyOm\Generator\Generators\Scaffold\ControllerGenerator as Base;
6
use InfyOm\Generator\Utils\FileUtil;
7
use PWWEB\Artomator\Common\CommandData;
8
9
class ControllerGenerator extends Base
10
{
11
    /**
12
     * Command data.
13
     *
14
     * @var CommandData
15
     */
16
    private $commandData;
17
18
    /**
19
     * Path.
20
     *
21
     * @var string
22
     */
23
    private $path;
24
25
    /**
26
     * Template type.
27
     *
28
     * @var string
29
     */
30
    private $templateType;
31
32
    /**
33
     * File name.
34
     *
35
     * @var string
36
     */
37
    private $fileName;
38
39
    /**
40
     * Constructor.
41
     *
42
     * @param CommandData $commandData Command data.
43
     */
44
    public function __construct(CommandData $commandData)
45
    {
46
        $this->commandData = $commandData;
47
        $this->path = $commandData->config->pathController;
48
        $this->templateType = config('infyom.laravel_generator.templates', 'adminlte-templates');
49
        $this->fileName = $this->commandData->modelName.'Controller.php';
50
    }
51
52
    /**
53
     * Generate.
54
     *
55
     * @return void
56
     */
57
    public function generate()
58
    {
59
        if (true === $this->commandData->getAddOn('datatables')) {
60
            if (true === $this->commandData->getOption('repositoryPattern')) {
61
                $templateName = 'datatable_controller';
62
            } else {
63
                $templateName = 'model_datatable_controller';
64
            }
65
66
            if (true === $this->commandData->isLocalizedTemplates()) {
67
                $templateName .= '_locale';
68
            }
69
70
            $templateData = get_template("scaffold.controller.$templateName", 'laravel-generator');
71
72
            parent::generateDataTable();
73
        } elseif (true === $this->commandData->jqueryDT()) {
74
            $templateName = 'jquery_datatable_controller';
75
            $templateData = get_template("scaffold.controller.$templateName", 'laravel-generator');
76
77
            parent::generateDataTable();
78
        } else {
79
            if (true === $this->commandData->getOption('repositoryPattern')) {
80
                $templateName = 'controller';
81
            } else {
82
                $templateName = 'model_controller';
83
            }
84
            if (true === $this->commandData->isLocalizedTemplates()) {
85
                $templateName .= '_locale';
86
            }
87
            if (true === $this->commandData->getOption('vue')) {
88
                $templateName .= '_inertia';
89
            }
90
91
            $templateData = get_template("scaffold.controller.$templateName", 'laravel-generator');
92
93
            $paginate = $this->commandData->getOption('paginate');
94
95
            if (true === $paginate) {
96
                $templateData = str_replace('$RENDER_TYPE$', 'paginate('.$paginate.')', $templateData);
97
            } else {
98
                $templateData = str_replace('$RENDER_TYPE$', 'all()', $templateData);
99
            }
100
        }
101
102
        $templateData = fill_template($this->commandData->dynamicVars, $templateData);
103
104
        FileUtil::createFile($this->path, $this->fileName, $templateData);
105
106
        $this->commandData->commandComment("\nController created: ");
107
        $this->commandData->commandInfo($this->fileName);
108
    }
109
}
110