Passed
Push — main ( 5c7fda...7a087f )
by Garbuz
02:45
created

Package::setGeneratorLaravelAdmin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GarbuzIvan\LaravelGeneratorPackage\Builder;
6
7
use GarbuzIvan\LaravelGeneratorPackage\Configuration;
8
use GarbuzIvan\LaravelGeneratorPackage\Facades\Field;
9
10
class Package
11
{
12
    /**
13
     * @var string
14
     */
15
    private string $name = 'name new package';
16
    private string $description = 'Description new package';
17
18
    /**
19
     * @var string
20
     */
21
    private string $packageVendor = '';
22
    private string $packageName = '';
23
    private string $model = 'Test';
24
    private string $table = 'test';
25
26
    /**
27
     * @var bool
28
     */
29
    private bool $generator_tests = true;
30
    private bool $generator_seed = true;
31
    private bool $generator_api = true;
32
    private bool $generator_api_frontend = true;
33
    private bool $generator_laravel_admin = true;
34
35
    /**
36
     * @var array
37
     */
38
    private array $fields = [];
39
    private array $form = [];
40
    private array $filter = [];
41
42
    /**
43
     * @param array $package
44
     * @return $this
45
     */
46
    public function init(array $package): self
47
    {
48
        $this->setName($package['name'] ?? $this->name);
49
        $this->setDescription($package['description'] ?? $this->description);
50 15
        $this->setPackageVendor($package['vendor'] ?? $this->packageVendor);
51
        $this->setPackageName($package['package'] ?? $this->packageName);
52 15
        $this->setModel($package['model'] ?? ucfirst($this->getPackageVendor()) . ucfirst($this->getPackageName()));
53 15
        $this->setTable($package['table'] ?? $this->getPackageVendor() . '_' . $this->getPackageName());
54 15
55 15
        $this->setGeneratorTests($package['generator']['tests'] ?? $this->generator_tests);
56 15
        $this->setGeneratorSeed($package['generator']['seed'] ?? $this->generator_seed);
57 15
        $this->setGeneratorApi($package['generator']['api'] ?? $this->generator_api);
58
        $this->setGeneratorApiFrontend($package['generator']['api_frontend'] ?? $this->generator_api_frontend);
59 15
        $this->setGeneratorLaravelAdmin($package['generator']['laravel_admin'] ?? $this->generator_laravel_admin);
60 15
61 15
        $this->setFields($package['fields'] ?? null);
62 15
        $this->setForm($package['form'] ?? $this->form);
63 15
        $this->setFilter($package['filter'] ?? $this->filter);
64
65 15
        return $this;
66 15
    }
67 15
68
    /**
69 15
     * @param string $name
70
     * @return $this
71
     */
72
    public function setName(string $name): self
73
    {
74
        $this->name = $name;
75
        return $this;
76 15
    }
77
78 15
    /**
79 15
     * @param string $description
80
     * @return $this
81
     */
82
    public function setDescription(string $description): self
83
    {
84
        $this->description = $description;
85
        return $this;
86 15
    }
87
88 15
    /**
89 15
     * @param string $packageVendor
90
     * @return $this
91
     */
92
    public function setPackageVendor(string $packageVendor): self
93
    {
94
        $this->packageVendor = mb_strtolower($packageVendor);
95
        return $this;
96 16
    }
97
98 16
    /**
99 16
     * @param string $packageName
100
     * @return $this
101
     */
102
    public function setPackageName(string $packageName): self
103
    {
104
        $this->packageName = mb_strtolower($packageName);
105
        return $this;
106 16
    }
107
108 16
    /**
109 16
     * @param string $model
110
     * @return $this
111
     */
112
    public function setModel(string $model): self
113
    {
114
        $this->model = ucfirst(mb_strtolower($model));
115
        return $this;
116 15
    }
117
118 15
    /**
119 15
     * @param string $table
120
     * @return $this
121
     */
122
    public function setTable(string $table): self
123
    {
124
        $this->table = mb_strtolower($table);
125
        return $this;
126 15
    }
127
128 15
    /**
129 15
     * @param bool $generator_tests
130
     * @return $this
131
     */
132
    public function setGeneratorTests(bool $generator_tests = true): self
133
    {
134
        $this->generator_tests = $generator_tests;
135
        return $this;
136 16
    }
137
138 16
    /**
139 16
     * @param bool $generator_seed
140
     * @return $this
141
     */
142
    public function setGeneratorSeed(bool $generator_seed = true): self
143
    {
144
        $this->generator_seed = $generator_seed;
145
        return $this;
146 16
    }
147
148 16
    /**
149 16
     * @param bool $generator_api
150
     * @return $this
151
     */
152
    public function setGeneratorApi(bool $generator_api = true): self
153
    {
154
        $this->generator_api = $generator_api;
155
        return $this;
156 16
    }
157
158 16
    /**
159 16
     * @param bool $generator_api_frontend
160
     * @return $this
161
     */
162
    public function setGeneratorApiFrontend(bool $generator_api_frontend = true): self
163
    {
164
        $this->generator_api_frontend = $generator_api_frontend;
165
        return $this;
166 16
    }
167
168 16
    /**
169 16
     * @param bool $generator_laravel_admin
170
     * @return $this
171
     */
172
    public function setGeneratorLaravelAdmin(bool $generator_laravel_admin = true): self
173
    {
174
        $this->generator_laravel_admin = $generator_laravel_admin;
175
        return $this;
176 16
    }
177
178 16
    /**
179 16
     * @param array|null $fields
180
     * @return $this
181
     */
182
    public function setFields(?array $fields = null): self
183
    {
184
        if (is_null($fields)) {
185
            return $this;
186 15
        }
187
        foreach ($fields as $fieldKey => $fieldParam) {
188 15
            $this->fields[$fieldKey] = Field::loadFieldFromArray($fieldKey, $fieldParam);
189 1
        }
190
        return $this;
191 15
    }
192 15
193
    /**
194 15
     * @param array $form
195
     * @return $this
196
     */
197
    public function setForm(array $form): self
198
    {
199
        $this->form = $form;
200
        return $this;
201 15
    }
202
203 15
    /**
204 15
     * @param array $filter
205
     * @return $this
206
     */
207
    public function setFilter(array $filter): self
208
    {
209
        $this->filter = $filter;
210
        return $this;
211 15
    }
212
213 15
    /**
214 15
     * @return $this
215
     */
216
    public function getNamespace(): string
217
    {
218
        return $this->spacer($this->getPackageVendor()) . '\\' . $this->spacer($this->getPackageName());;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->spacer($th...this->getPackageName()) returns the type string which is incompatible with the documented return type GarbuzIvan\LaravelGeneratorPackage\Builder\Package.
Loading history...
219
    }
220 2
221
    /**
222 2
     * @return $this
223
     */
224
    public function getNamespaceSlashes(): string
225
    {
226
        return $this->spacer($this->getPackageVendor()) . '\\\\' . $this->spacer($this->getPackageName());;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->spacer($th...this->getPackageName()) returns the type string which is incompatible with the documented return type GarbuzIvan\LaravelGeneratorPackage\Builder\Package.
Loading history...
227
    }
228 2
229
    /**
230 2
     * @return string
231
     */
232
    public function getName(): string
233
    {
234
        return $this->name;
235
    }
236 3
237
    /**
238 3
     * @return string
239
     */
240
    public function getDescripton(): string
241
    {
242
        return $this->description;
243
    }
244 3
245
    /**
246 3
     * @return string|null
247
     */
248
    public function getPackageVendor(): ?string
249
    {
250
        return $this->packageVendor;
251
    }
252 4
253
    /**
254 4
     * @return string|null
255
     */
256
    public function getPackageName(): ?string
257
    {
258
        return $this->packageName;
259
    }
260 5
261
    /**
262 5
     * @return string
263
     */
264
    public function getModel(): string
265
    {
266
        return $this->model;
267
    }
268 2
269
    /**
270 2
     * @return string
271
     */
272
    public function getTable(): string
273
    {
274
        return $this->table;
275
    }
276 2
277
    /**
278 2
     * @return bool
279
     */
280
    public function getGeneratorTests(): bool
281
    {
282
        return $this->generator_tests;
283
    }
284 4
285
    /**
286 4
     * @return bool
287
     */
288
    public function getGeneratorSeed(): bool
289
    {
290
        return $this->generator_seed;
291
    }
292 4
293
    /**
294 4
     * @return bool
295
     */
296
    public function getGeneratorApi(): bool
297
    {
298
        return $this->generator_api;
299
    }
300 4
301
    /**
302 4
     * @return bool
303
     */
304
    public function getGeneratorApiFrontend(): bool
305
    {
306
        return $this->generator_api_frontend;
307
    }
308 4
309
    /**
310 4
     * @return bool
311
     */
312
    public function getGeneratorLaravelAdmin(): bool
313
    {
314
        return $this->generator_laravel_admin;
315
    }
316 4
317
    /**
318 4
     * @return array
319
     */
320
    public function getFields(): array
321
    {
322
        return $this->fields;
323
    }
324 4
325
    /**
326 4
     * @return array
327
     */
328
    public function getForm(): array
329
    {
330
        return $this->form;
331
    }
332 1
333
    /**
334 1
     * @return array
335
     */
336
    public function getFilter(): array
337
    {
338
        return $this->filter;
339
    }
340 1
341
    /**
342 1
     * @param string|null $path
343
     * @return string
344
     */
345
    public function getPath(?string $path = null): ?string
346
    {
347
        $basePath = app(Configuration::class)->getBasePath();
348
        return $basePath . '/packages/' . $this->getPackageVendor() . '/' . $this->getPackageName() . '/' . $path;
349 3
    }
350
351 3
    /**
352 3
     * @param string $name
353
     * @return string
354
     */
355
    public function spacer(string $name): string
356
    {
357
        $name = preg_replace('~[^a-z0-9]~isuU', ' ', $name);
358
        $name = ucwords($name);
359 2
        $name = str_replace(' ', '', $name);
360
        return $name;
361 2
    }
362
}
363