Passed
Push — main ( dcb144...5c7fda )
by Garbuz
03:59
created

Package::getGeneratorSeed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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