Completed
Push — master ( 5dab40...ca534d )
by Maxime
120:18 queued 114:35
created

ComponentMakeCommand::fire()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php namespace Distilleries\Expendable\Console;
2
3
use Illuminate\Console\DetectsApplicationNamespace;
4
use Illuminate\Filesystem\Filesystem;
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputOption;
7
use Distilleries\Expendable\Console\Lib\Generators\ComponentGenerator;
8
9
class ComponentMakeCommand extends \Illuminate\Console\GeneratorCommand {
10
11
    use DetectsApplicationNamespace;
12
    /**
13
     * The console command name.
14
     *
15
     * @var string
16
     */
17
    protected $name = 'expendable:component.make';
18
19
20
    protected $states;
21
    protected $model;
22
    protected $form;
23
    protected $datatable;
24
    protected $template;
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'Creates a controller for a backend component builder class.';
32
33
    /**
34
     * @var ComponentGenerator
35
     */
36
    protected $formGenerator;
37
38 576
    public function __construct(Filesystem $files, ComponentGenerator $formGenerator)
39
    {
40 576
        parent::__construct($files);
41 576
        $this->formGenerator = $formGenerator;
42
    }
43
44 24
    public function handle()
45
    {
46 24
        $this->initOptions();
47 24
        parent::handle();
48
    }
49
50
51
52
    /**
53
     * Get the console command arguments.
54
     *
55
     * @return array
56
     */
57 576
    protected function getArguments()
58
    {
59
        return array(
60 576
            array('name', InputArgument::REQUIRED, 'Full path for Component class.'),
61
        );
62
    }
63
64
    /**
65
     * Get the console command options.
66
     *
67
     * @return array
68
     */
69 576
    protected function getOptions()
70
    {
71
        return array(
72 576
            array('states', [], InputOption::VALUE_OPTIONAL, 'States use on the controller', ''),
73
            array('template', null, InputOption::VALUE_OPTIONAL, 'Template use to generate the controller', ''),
74
            array('model', null, InputOption::VALUE_OPTIONAL, 'Model use', ''),
75
            array('datatable', null, InputOption::VALUE_OPTIONAL, 'Datatable use', ''),
76
            array('form', null, InputOption::VALUE_OPTIONAL, 'Form Use', ''),
77
78
        );
79
    }
80
81 24
    protected function initOptions()
82
    {
83 24
        $states = $this->option('states');
84
85 24
        if (!empty($states))
86
        {
87 16
            $this->states = explode(',', $states);
88
        } else
89
        {
90 8
            $this->states = [];
91
        }
92
93 24
        $this->template  = $this->option('template');
94 24
        $this->model     = $this->option('model');
95 24
        $this->datatable = $this->option('datatable');
96 24
        $this->form      = $this->option('form');
97
98
    }
99
100
101 24
    protected function getTemplate()
102
    {
103
104 24
        if (empty($this->template))
105
        {
106 20
            $defaultComponent = 0;
107
108
109 20
            foreach ($this->states as $state)
110
            {
111 16
                if (strpos($state, 'DatatableStateContract') !== false || (strpos($state, 'FormStateContract') !== false))
112
                {
113 16
                    $defaultComponent++;
114
115
                }
116
            }
117
118
119 20
            if ($defaultComponent == 2 && !empty($this->model))
120
            {
121 8
                $template = 'controller-base-component-class-template';
122 12
            } else if (!empty($this->model))
123
            {
124 4
                $template = 'controller-base-model-class-template';
125
            } else
126
            {
127 8
                $template = 'controller-base-class-template';
128
            }
129
130 20
            return $template;
131
        }
132
133 4
        return $this->template;
134
135
    }
136
137
    /**
138
     * Replace the class name for the given stub.
139
     *
140
     * @param  string $stub
141
     * @param  string $name
142
     * @return string
143
     */
144 24
    protected function replaceClass($stub, $name)
145
    {
146 24
        $formGenerator = $this->formGenerator;
147
148 24
        $stub = str_replace(
149 24
            '{{class}}',
150 24
            $formGenerator->getClassInfo($name)->className,
151 24
            $stub
152
        );
153
154 24
        $stub = str_replace(
155 24
            '{{model}}',
156 24
            $formGenerator->getClassModel($this->model),
157 24
            $stub
158
        );
159
160 24
        $stub = str_replace(
161 24
            '{{datatable}}',
162 24
            $this->datatable,
163 24
            $stub
164
        );
165
166
167 24
        $stub = str_replace(
168 24
            '{{trait}}',
169 24
            $formGenerator->getTrait($this->states),
170 24
            $stub
171
        );
172
173
174 24
        $stub = str_replace(
175 24
            '{{app}}',
176 24
            $this->getAppNamespace(),
177 24
            $stub
178
        );
179
180 24
        $stub = str_replace(
181 24
            '{{implement}}',
182 24
            $formGenerator->getImplementation($this->states),
183 24
            $stub
184
        );
185
186 24
        $stub = str_replace(
187 24
            '{{form}}',
188 24
            $this->form,
189 24
            $stub
190
        );
191
192 24
        return $stub;
193
    }
194
195
    /**
196
     * Replace the namespace for the given stub.
197
     *
198
     * @param  string $stub
199
     * @param  string $name
200
     * @return $this
201
     */
202 24
    protected function replaceNamespace(&$stub, $name)
203
    {
204 24
        $stub = str_replace(
205 24
            '{{namespace}}',
206 24
            $this->formGenerator->getClassInfo($name)->namespace,
207 24
            $stub
208
        );
209
210 24
        return $this;
211
    }
212
213
    /**
214
     * Get the desired class name from the input.
215
     *
216
     * @return string
217
     */
218 24
    protected function getNameInput()
219
    {
220 24
        return str_replace('/', '\\', $this->argument('name'));
221
    }
222
223
    /**
224
     * Get the stub file for the generator.
225
     *
226
     * @return string
227
     */
228 24
    protected function getStub()
229
    {
230 24
        return __DIR__ . '/Lib/stubs/' . $this->getTemplate() . '.stub';
231
    }
232
}