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

ControllerGenerator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 41
c 1
b 0
f 0
dl 0
loc 99
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B generate() 0 51 9
A __construct() 0 6 1
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