ModelComponentFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 22
nc 1
nop 9
dl 0
loc 37
rs 9.568
c 0
b 0
f 0
ccs 0
cts 22
cp 0
crap 2

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Leonidas\Console\Library\Printer\Model;
4
5
use Leonidas\Contracts\System\Model\DatableInterface;
6
use Leonidas\Contracts\System\Model\MutableDatableInterface;
7
use Leonidas\Library\Core\Abstracts\ConvertsCaseTrait;
8
9
class ModelComponentFactory
10
{
11
    use ConvertsCaseTrait;
12
13
    protected string $model;
14
15
    protected string $namespace;
16
17
    protected string $contracts;
18
19
    protected string $abstracts;
20
21
    protected string $entity;
22
23
    protected string $single;
24
25
    protected string $plural;
26
27
    protected string $template;
28
29
    protected string $modelInterface;
30
31
    protected string $collectionInterface;
32
33
    protected string $abstractCollection;
34
35
    protected string $collection;
36
37
    protected string $query;
38
39
    protected string $repositoryInterface;
40
41
    protected string $repository;
42
43
    protected string $collectionFactory;
44
45
    protected string $queryFactory;
46
47
    protected string $modelConverter;
48
49
    protected string $getAccessProvider;
50
51
    protected string $setAccessProvider;
52
53
    protected string $tagAccessProvider;
54
55
    protected string $facades;
56
57
    protected function __construct(
58
        string $model,
59
        string $namespace,
60
        string $contracts,
61
        string $abstracts,
62
        string $facades,
63
        string $entity,
64
        string $single,
65
        string $plural,
66
        ?string $template = null
67
    ) {
68
        $this->namespace = $namespace;
69
        $this->contracts = $contracts;
70
        $this->abstracts = $abstracts;
71
        $this->facades = $facades;
72
        $this->entity = $entity;
73
74
        $this->template = $template ?? 'post';
75
76
        $this->single = $this->convert($single)->toCamel();
77
        $this->plural = $this->convert($plural)->toCamel();
78
79
        $model = $this->model = $this->convert($model)->toPascal();
80
81
        $this->modelInterface = $model . 'Interface';
82
        $this->collectionInterface = $model . 'CollectionInterface';
83
        $this->abstractCollection = 'Abstract' . $model . 'Collection';
84
        $this->collection = $model . 'Collection';
85
        $this->query = $model . 'Query';
86
        $this->repositoryInterface = $model . 'RepositoryInterface';
87
        $this->repository = $model . 'Repository';
88
        $this->modelConverter = $model . 'Converter';
89
        $this->collectionFactory = $model . 'CollectionFactory';
90
        $this->queryFactory = $model . 'QueryFactory';
91
        $this->getAccessProvider = $model . 'GetAccessProvider';
92
        $this->setAccessProvider = $model . 'SetAccessProvider';
93
        $this->tagAccessProvider = $model . 'TagAccessProvider';
94
    }
95
96
    public function getEntity(): string
97
    {
98
        return $this->entity;
99
    }
100
101
    public function getSingle(): string
102
    {
103
        return $this->single;
104
    }
105
106
    public function getPlural(): string
107
    {
108
        return $this->plural;
109
    }
110
111
    public function getTemplate(): string
112
    {
113
        return $this->template;
114
    }
115
116
    public function isPostTemplate(): bool
117
    {
118
        return in_array($this->template, ['post', 'post:h', 'attachment']);
119
    }
120
121
    public function isTermTemplate(): bool
122
    {
123
        return in_array($this->template, ['term', 'term:h']);
124
    }
125
126
    public function isUserTemplate(): bool
127
    {
128
        return 'user' === $this->template;
129
    }
130
131
    public function isCommentTemplate(): bool
132
    {
133
        return 'comment' === $this->template;
134
    }
135
136
    public function getModelInterfacePrinter(): ModelInterfacePrinter
137
    {
138
        return new ModelInterfacePrinter(
139
            $this->contracts,
140
            $this->modelInterface,
141
            $this->template
142
        );
143
    }
144
145
    public function getModelPrinter(): ModelPrinter
146
    {
147
        return new ModelPrinter(
148
            $this->namespace,
149
            $this->model,
150
            $this->getContractFqn($this->modelInterface),
151
            $this->entity,
152
            $this->template
153
        );
154
    }
155
156
    public function getCollectionInterfacePrinter(): ModelCollectionInterfacePrinter
157
    {
158
        return new ModelCollectionInterfacePrinter(
159
            $this->getContractFqn($this->modelInterface),
160
            $this->single,
161
            $this->plural,
162
            $this->contracts,
163
            $this->collectionInterface
164
        );
165
    }
166
167
    public function getCollectionPrinter(): ModelCollectionPrinter
168
    {
169
        return new ModelCollectionPrinter(
170
            $this->getContractFqn($this->modelInterface),
171
            $this->single,
172
            $this->plural,
173
            $this->namespace,
174
            $this->collection,
175
            $this->getContractFqn($this->collectionInterface),
176
        );
177
    }
178
179
    public function getAbstractCollectionPrinter(): ModelCollectionAbstractPrinter
180
    {
181
        return new ModelCollectionAbstractPrinter(
182
            $this->getContractFqn($this->modelInterface),
183
            $this->single,
184
            $this->plural,
185
            $this->abstracts,
186
            $this->abstractCollection,
187
            $this->getContractFqn($this->collectionInterface),
188
        );
189
    }
190
191
    public function getChildCollectionPrinter(): ModelCollectionAsChildPrinter
192
    {
193
        return new ModelCollectionAsChildPrinter(
194
            $this->getAbstractFqn($this->abstractCollection),
195
            $this->getContractFqn($this->modelInterface),
196
            $this->single,
197
            $this->plural,
198
            $this->namespace,
199
            $this->collection,
200
            $this->getContractFqn($this->collectionInterface),
201
        );
202
    }
203
204
    public function getChildQueryPrinter(): ModelQueryAsChildPrinter
205
    {
206
        return new ModelQueryAsChildPrinter(
207
            $this->getAbstractFqn($this->abstractCollection),
208
            $this->model,
209
            $this->single,
210
            $this->plural,
211
            $this->namespace,
212
            $this->query,
213
            $this->getContractFqn($this->collectionInterface),
214
            $this->entity,
215
            $this->template
216
        );
217
    }
218
219
    public function getRepositoryInterfacePrinter(): ModelRepositoryInterfacePrinter
220
    {
221
        return new ModelRepositoryInterfacePrinter(
222
            $this->getContractFqn($this->modelInterface),
223
            $this->getContractFqn($this->collectionInterface),
224
            $this->single,
225
            $this->plural,
226
            $this->contracts,
227
            $this->repositoryInterface,
228
            $this->template
229
        );
230
    }
231
232
    public function getRepositoryPrinter(): ModelRepositoryPrinter
233
    {
234
        return new ModelRepositoryPrinter(
235
            $this->getContractFqn($this->modelInterface),
236
            $this->getContractFqn($this->collectionInterface),
237
            $this->single,
238
            $this->plural,
239
            $this->namespace,
240
            $this->repository,
241
            $this->getContractFqn($this->repositoryInterface),
242
            $this->template
243
        );
244
    }
245
246
    public function getModelConverterPrinter(): ModelConverterPrinter
247
    {
248
        return new ModelConverterPrinter(
249
            $this->namespace,
250
            $this->modelConverter,
251
            $this->getClassFqn($this->model),
252
            $this->getContractFqn($this->modelInterface),
253
            $this->template
254
        );
255
    }
256
257
    public function getCollectionFactoryPrinter(): ModelCollectionFactoryPrinter
258
    {
259
        return new ModelCollectionFactoryPrinter(
260
            $this->namespace,
261
            $this->collectionFactory,
262
            $this->getClassFqn($this->collection)
263
        );
264
    }
265
266
    public function getQueryFactoryPrinter(): ModelQueryFactoryPrinter
267
    {
268
        return new ModelQueryFactoryPrinter(
269
            $this->namespace,
270
            $this->queryFactory,
271
            $this->getClassFqn($this->query),
272
            $this->template
273
        );
274
    }
275
276
    public function getGetAccessProviderPrinter(): ModelGetAccessProviderPrinter
277
    {
278
        return new ModelGetAccessProviderPrinter(
279
            $this->namespace,
280
            $this->getAccessProvider,
281
            $this->getContractFqn($this->modelInterface),
282
            $this->single,
283
            $this->isDatableModel()
284
        );
285
    }
286
287
    public function getSetAccessProviderPrinter(): ModelSetAccessProviderPrinter
288
    {
289
        return new ModelSetAccessProviderPrinter(
290
            $this->namespace,
291
            $this->setAccessProvider,
292
            $this->getContractFqn($this->modelInterface),
293
            $this->single,
294
            $this->isMutableDatableModel()
295
        );
296
    }
297
298
    public function getTagAccessProviderPrinter(): ModelTemplateTagsProviderPrinter
299
    {
300
        return new ModelTemplateTagsProviderPrinter(
301
            $this->namespace,
302
            $this->tagAccessProvider,
303
            $this->getContractFqn($this->modelInterface),
304
            $this->single,
305
            $this->getClassFqn($this->getAccessProvider),
306
            $this->template
307
        );
308
    }
309
310
    public function getRepositoryFacadePrinter(): ModelRepositoryFacadePrinter
311
    {
312
        return new ModelRepositoryFacadePrinter(
313
            $this->convert($this->plural)->toPascal(),
314
            $this->facades,
315
            $this->getContractFqn($this->repositoryInterface),
316
            $this->getClassFqn($this->queryFactory),
317
            $this->getClassFqn($this->query),
318
            $this->template
319
        );
320
    }
321
322
    protected function isDatableModel(): bool
323
    {
324
        return interface_exists($this->modelInterface)
325
            && is_subclass_of($this->modelInterface, DatableInterface::class);
326
    }
327
328
    protected function isMutableDatableModel(): bool
329
    {
330
        return interface_exists($this->modelInterface)
331
            && is_subclass_of($this->modelInterface, MutableDatableInterface::class);
332
    }
333
334
    protected function getAbstractFqn(string $class): string
335
    {
336
        return $this->abstracts . '\\' . $class;
337
    }
338
339
    protected function getContractFqn(string $interface): string
340
    {
341
        return $this->contracts . '\\' . $interface;
342
    }
343
344
    protected function getClassFqn(string $class): string
345
    {
346
        return $this->namespace . '\\' . $class;
347
    }
348
349
    public static function build(array $args): ModelComponentFactory
350
    {
351
        return new static(
352
            $args['model'],
353
            $args['namespace'],
354
            $args['contracts'],
355
            $args['abstracts'],
356
            $args['facades'],
357
            $args['entity'],
358
            $args['single'],
359
            $args['plural'],
360
            $args['template'] ?? null
361
        );
362
    }
363
}
364