Failed Conditions
Branch feature/refactoring-samurai (8cc7c1)
by Giuliano
03:47
created

Directive   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 332
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 67
dl 0
loc 332
rs 10
c 4
b 0
f 0
wmc 26

21 Methods

Rating   Name   Duplication   Size   Complexity  
A alias() 0 5 1
A type() 0 3 1
A load() 0 16 2
A setAlias() 0 6 1
A relative() 0 9 1
A setType() 0 8 2
A loadComponent() 0 5 1
A setSentence() 0 14 3
A name() 0 3 1
A path() 0 3 1
A placeholders() 0 3 1
A init() 0 8 1
A __construct() 0 3 1
A filename() 0 3 1
A setName() 0 7 1
A loadKebabAlias() 0 5 1
A create() 0 11 2
A exists() 0 3 1
A sentence() 0 3 1
A setPath() 0 4 1
A loadInclude() 0 5 1
1
<?php
2
3
namespace Maestriam\Samurai\Entities;
4
5
use Illuminate\Support\Str;
6
use Maestriam\Samurai\Entities\Theme;
7
use Illuminate\Support\Facades\Blade;
8
use Maestriam\FileSystem\Support\FileSystem;
9
use Maestriam\Samurai\Contracts\Entities\DirectiveContract;
10
use Maestriam\Samurai\Exceptions\DirectiveExistsException;
11
use Maestriam\Samurai\Exceptions\InvalidDirectiveNameException;
12
use Maestriam\Samurai\Exceptions\InvalidTypeDirectiveException;
13
14
abstract class Directive extends Source implements DirectiveContract
15
{
16
    /**
17
     * Nome da diretiva
18
     *
19
     * @var string
20
     */
21
    protected string $name;
22
23
    /**
24
     * Sentença que foi inserida pelo usuário,
25
     * que irá fornecer o nome/sub-diretório
26
     *
27
     * @var string
28
     */
29
    protected string $sentence;
30
31
    /**
32
     * Tipo da diretiva
33
     *
34
     * @var string
35
     */
36
    protected string $type;
37
38
    /**
39
     * Tema de origem
40
     *
41
     * @var Theme
42
     */
43
    protected $theme;
44
45
    /**
46
     * Caminho do arquivo
47
     *
48
     * @var string
49
     */
50
    protected string $path;
51
52
    /**
53
     * Apelido pelo qual é chamado dentro do projeto,
54
     * como camel-case
55
     * 
56
     * @var string
57
     */
58
    protected string $camelAlias;
59
60
    /**
61
     * Apelido pelo qual é chamado dentro do projeto,
62
     * como camel-case
63
     * 
64
     * @var string
65
     */
66
    protected string $kebabAlias;
67
68
    /**
69
     * Instância as regras de negócio de uma diretiva
70
     *
71
     * @param Theme  $theme
72
     * @param string $sentence
73
     */
74
    public function __construct(Theme $theme, string $sentence)
75
    {
76
        $this->init($theme, $sentence, $this->type());
77
    }
78
79
    /**
80
     * {@inheritDoc}
81
     */
82
    public function sentence() : string
83
    {
84
        return $this->sentence;
85
    }
86
87
    /**
88
     * {@inheritDoc}
89
     */
90
    public function type() : string
91
    {
92
        return $this->type;
93
    }
94
95
    /**
96
     * {@inheritDoc}
97
     */
98
    public function path() : string
99
    {
100
        return $this->path;
101
    }
102
103
    /**
104
     * {@inheritDoc}
105
     */
106
    public function alias() : object
107
    {
108
        return (object) [
109
            'camel' => $this->camelAlias,
110
            'kebab' => $this->kebabAlias,
111
        ];
112
    }
113
114
    /**
115
     * {@inheritDoc}
116
     */
117
    public function filename() : string
118
    {        
119
        return $this->nominator()->filename($this->sentence(), $this->type());
120
    }
121
122
    /**
123
     * {@inheritDoc}
124
     */
125
    public function placeholders() : array
126
    {
127
        return ['name' => $this->name];
128
    }
129
130
    /**
131
     * Retorna o nome da diretiva.  
132
     *
133
     * @return string
134
     */
135
    public function name() : string
136
    {
137
        return $this->name;
138
    }
139
140
    /**
141
     * {@inheritDoc}
142
     */
143
    public function create() : Component|Includer
144
    {
145
        if ($this->exists()) {
146
            throw new DirectiveExistsException($this->name(), $this->theme()->package());
147
        }
148
149
        $this->createFile();
150
151
        $this->setPath();
152
153
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Maestriam\Samurai\Entities\Directive which includes types incompatible with the type-hinted return Maestriam\Samurai\Entiti...murai\Entities\Includer.
Loading history...
154
    }
155
156
    /**
157
     * {@inheritDoc}
158
     */
159
    public function load() : Component|Includer
160
    {                              
161
        $namespace = $this->theme()->namespace();
162
        
163
        $path = $this->relative();
164
        $file = $this->nominator()->blade($namespace, $path);
165
        
166
        $alias = $this->alias();
167
168
        $this->loadKebabAlias($file, $alias->kebab);
169
170
        if ($this->type() == 'include') {
171
            return $this->loadInclude($file, $alias->camel);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->loadInclude($file, $alias->camel) returns the type Maestriam\Samurai\Entities\Directive which includes types incompatible with the type-hinted return Maestriam\Samurai\Entiti...murai\Entities\Includer.
Loading history...
172
        }
173
        
174
        return $this->loadComponent($file, $alias->camel);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->loadComponent($file, $alias->camel) returns the type Maestriam\Samurai\Entities\Directive which includes types incompatible with the type-hinted return Maestriam\Samurai\Entiti...murai\Entities\Includer.
Loading history...
175
    }
176
    
177
    /**
178
     * Retorna o caminho relativo da diretiva dentro do tema
179
     *
180
     * @return string
181
     */
182
    public function relative() : string
183
    {
184
        $path = $this->path();
185
        
186
        $base = $this->theme()->paths()->root();
187
188
        $base = FileSystem::folder($base)->sanitize();
189
190
        return str_replace($base, '', $path);
191
    }
192
193
    /**
194
     * Retorna se a diretiva existe ou não dentro do projeto.  
195
     *
196
     * @return boolean
197
     */
198
    public function exists() : bool
199
    {
200
        return $this->fileExists();
201
    }
202
203
        /**
204
         * Carrega a diretiva (include/component) dentro do projeto como kabe-case.  
205
         * Padrão utilizado no Blade UI.  
206
         *
207
         * @param  string $file
208
         * @param  string $alias
209
         * @return Directive
210
         */
211
    private function loadKebabAlias(string $file, string $alias) : Directive
212
    {
213
        Blade::component($file, $alias);
214
        
215
        return $this;
216
    }
217
    
218
    /**
219
     * Carrega o include dentro do projeto como camel-case.  
220
     * Padrão utilizado no Blade padrão.
221
     *
222
     * @param  string $file
223
     * @param  string $alias
224
     * @return Directive
225
     */
226
    protected function loadInclude(string $file, string $alias) : Directive
227
    {        
228
        Blade::aliasInclude($file, $alias);   
229
230
        return $this;
231
    }
232
233
    /**
234
     * Carrega o include dentro do projeto como camel-case.  
235
     * Padrão utilizado no Blade padrão.
236
     *
237
     * @param  string $file
238
     * @param  string $alias
239
     * @return Directive
240
     */
241
    protected function loadComponent(string $file, string $alias) : Directive
242
    {
243
        Blade::aliasComponent($file, $alias);   
244
        
245
        return $this;
246
    }
247
248
    /**
249
     * Carrega todas as informações da diretiva através do tema,
250
     * nome da diretiva e com seu tipo
251
     *
252
     * @param  Theme  $theme
253
     * @param  string $sentence
254
     * @param  string $type
255
     * @return void
256
     */
257
    protected function init(Theme $theme, string $sentence, string $type) : void
258
    {
259
        $this->setTheme($theme);        
260
        $this->setType($type);
261
        $this->setSentence($sentence);
262
        $this->setName($sentence);
263
        $this->setAlias();
264
        $this->setPath();
265
    }
266
267
    /**
268
     * Define a sentença que foi inserida pelo usuário via console,
269
     * que irá fornecer o nome/sub-diretório
270
     *
271
     * @param  Theme $theme
272
     * @return Directive
273
     * @throws InvalidDirectiveNameException
274
     */
275
    protected function setSentence(string $sentence) : Directive
276
    {
277
        $parsed = $this->parser()->filename($sentence);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $parsed is correct as $this->parser()->filename($sentence) targeting Maestriam\Samurai\Founda...enameParser::filename() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
278
279
        if (! $parsed) {
0 ignored issues
show
introduced by
$parsed is of type null, thus it always evaluated to false.
Loading history...
280
            throw new InvalidDirectiveNameException($sentence);
281
        }
282
283
        if (! $this->valid()->directive($parsed->name)) {
284
            throw new InvalidDirectiveNameException($sentence);
285
        }
286
287
        $this->sentence = strtolower($sentence);
288
        return $this;
289
    }
290
    
291
    /**
292
     * Define o tipo da diretiva
293
     *
294
     * @param  string $type
295
     * @return Directive
296
     * @throws InvalidDirectiveNameException
297
     */
298
    protected function setType(string $type) : Directive
299
    {
300
        if (! $this->valid()->type($type)) {
301
            throw new InvalidTypeDirectiveException($type);
302
        }
303
304
        $this->type = $type;
305
        return $this;
306
    }
307
308
    /**
309
     * Define o caminho absoluto do arquivo composer.json dentro do tema
310
     *
311
     * @param  string $path
312
     * @return Composer
313
     */
314
    protected function setPath() : Directive
315
    {
316
        $this->path = $this->filePath();
317
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Maestriam\Samurai\Entities\Directive which is incompatible with the documented return type Maestriam\Samurai\Entities\Composer.
Loading history...
318
    }
319
320
    /**
321
     * Define o nome da diretiva
322
     *
323
     * @param  string $name
324
     * @return Directive
325
     */
326
    protected function setName(string $name) : Directive
327
    {
328
        $parsed = $this->parser()->filename($name);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $parsed is correct as $this->parser()->filename($name) targeting Maestriam\Samurai\Founda...enameParser::filename() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
329
330
        $this->name = Str::slug($parsed->name);
331
332
        return $this;
333
    }
334
335
    /**
336
     * Define o alias para ser chamado dentro do projeto
337
     *
338
     * @return Directive
339
     */
340
    protected function setAlias() : Directive
341
    {
342
        $this->camelAlias = $this->nominator()->camelAlias($this->name);
343
        $this->kebabAlias = $this->nominator()->kebabAlias($this->name);
344
345
        return $this;
346
    }
347
}
348