Package   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 351
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 35
eloc 83
c 1
b 0
f 0
dl 0
loc 351
ccs 101
cts 101
cp 1
rs 9.6

33 Methods

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