Passed
Push — master ( f5c22c...929873 )
by Chris
16:12
created

ModelComponentFactory::__construct()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 22
c 1
b 0
f 1
nc 1
nop 8
dl 0
loc 35
ccs 0
cts 23
cp 0
crap 6
rs 9.568

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;
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 function __construct(
56
        string $model,
57
        string $namespace,
58
        string $contracts,
59
        string $abstracts,
60
        string $entity,
61
        ?string $single,
62
        ?string $plural = null,
63
        ?string $template = null
64
    ) {
65
        $this->namespace = $namespace;
66
        $this->contracts = $contracts;
67
        $this->abstracts = $abstracts;
68
        $this->entity = $entity;
69
        $this->single = $single;
70
71
        $this->plural = $plural ?: $this->single;
72
        $this->template = $template ?? 'post';
73
74
        $model = $this->convert($model)->toPascal();
75
76
        $this->model = $model;
77
        $this->modelInterface = $model . 'Interface';
78
        $this->collectionInterface = $model . 'CollectionInterface';
79
        $this->abstractCollection = 'Abstract' . $model . 'Collection';
80
        $this->collection = $model . 'Collection';
81
        $this->query = $model . 'Query';
82
        $this->repositoryInterface = $model . 'RepositoryInterface';
83
        $this->repository = $model . 'Repository';
84
        $this->modelConverter = $model . 'Converter';
85
        $this->collectionFactory = $model . 'CollectionFactory';
86
        $this->queryFactory = $model . 'QueryFactory';
87
        $this->getAccessProvider = $model . 'GetAccessProvider';
88
        $this->setAccessProvider = $model . 'SetAccessProvider';
89
        $this->tagAccessProvider = $model . 'TagAccessProvider';
90
    }
91
92
    public function isPostTemplate(): bool
93
    {
94
        return in_array($this->template, ['post', 'post:h', 'attachment']);
95
    }
96
97
    public function isTermTemplate(): bool
98
    {
99
        return in_array($this->template, ['term', 'term:h']);
100
    }
101
102
    public function isUserTemplate(): bool
103
    {
104
        return 'user' === $this->template;
105
    }
106
107
    public function isCommentTemplate(): bool
108
    {
109
        return 'comment' === $this->template;
110
    }
111
112
    public function getModelInterfacePrinter(): ModelInterfacePrinter
113
    {
114
        return new ModelInterfacePrinter(
115
            $this->contracts,
116
            $this->modelInterface,
117
            $this->template
118
        );
119
    }
120
121
    public function getModelPrinter(): ModelPrinter
122
    {
123
        return new ModelPrinter(
124
            $this->namespace,
125
            $this->model,
126
            $this->getContractFqn($this->modelInterface),
127
            $this->entity,
128
            $this->template
129
        );
130
    }
131
132
    public function getCollectionInterfacePrinter(): ModelCollectionInterfacePrinter
133
    {
134
        return new ModelCollectionInterfacePrinter(
135
            $this->getContractFqn($this->modelInterface),
136
            $this->single,
137
            $this->plural,
138
            $this->contracts,
139
            $this->collectionInterface
140
        );
141
    }
142
143
    public function getCollectionPrinter(): ModelCollectionPrinter
144
    {
145
        return new ModelCollectionPrinter(
146
            $this->getContractFqn($this->modelInterface),
147
            $this->single,
148
            $this->plural,
149
            $this->namespace,
150
            $this->collection,
151
            $this->getContractFqn($this->collectionInterface),
152
        );
153
    }
154
155
    public function getAbstractCollectionPrinter(): ModelCollectionAbstractPrinter
156
    {
157
        return new ModelCollectionAbstractPrinter(
158
            $this->getContractFqn($this->modelInterface),
159
            $this->single,
160
            $this->plural,
161
            $this->abstracts,
162
            $this->abstractCollection,
163
            $this->getContractFqn($this->collectionInterface),
164
        );
165
    }
166
167
    public function getChildCollectionPrinter(): ModelCollectionAsChildPrinter
168
    {
169
        return new ModelCollectionAsChildPrinter(
170
            $this->getAbstractFqn($this->abstractCollection),
171
            $this->getContractFqn($this->modelInterface),
172
            $this->single,
173
            $this->plural,
174
            $this->namespace,
175
            $this->collection,
176
            $this->getContractFqn($this->collectionInterface),
177
        );
178
    }
179
180
    public function getChildQueryPrinter(): ModelQueryAsChildPrinter
181
    {
182
        return new ModelQueryAsChildPrinter(
183
            $this->getAbstractFqn($this->abstractCollection),
184
            $this->model,
185
            $this->single,
186
            $this->plural,
187
            $this->namespace,
188
            $this->query,
189
            $this->getContractFqn($this->collectionInterface),
190
            $this->entity,
191
            $this->template
192
        );
193
    }
194
195
    public function getRepositoryInterfacePrinter(): ModelRepositoryInterfacePrinter
196
    {
197
        return new ModelRepositoryInterfacePrinter(
198
            $this->getContractFqn($this->modelInterface),
199
            $this->getContractFqn($this->collectionInterface),
200
            $this->single,
201
            $this->plural,
202
            $this->contracts,
203
            $this->repositoryInterface,
204
            $this->template
205
        );
206
    }
207
208
    public function getRepositoryPrinter(): ModelRepositoryPrinter
209
    {
210
        return new ModelRepositoryPrinter(
211
            $this->getContractFqn($this->modelInterface),
212
            $this->getContractFqn($this->collectionInterface),
213
            $this->single,
214
            $this->plural,
215
            $this->namespace,
216
            $this->repository,
217
            $this->getContractFqn($this->repositoryInterface),
218
            $this->template
219
        );
220
    }
221
222
    public function getModelConverterPrinter(): ModelConverterPrinter
223
    {
224
        return new ModelConverterPrinter(
225
            $this->namespace,
226
            $this->modelConverter,
227
            $this->getClassFqn($this->model),
228
            $this->getContractFqn($this->modelInterface),
229
            $this->template
230
        );
231
    }
232
233
    public function getCollectionFactoryPrinter(): ModelCollectionFactoryPrinter
234
    {
235
        return new ModelCollectionFactoryPrinter(
236
            $this->namespace,
237
            $this->collectionFactory,
238
            $this->getClassFqn($this->collection)
239
        );
240
    }
241
242
    public function getQueryFactoryPrinter(): ModelQueryFactoryPrinter
243
    {
244
        return new ModelQueryFactoryPrinter(
245
            $this->namespace,
246
            $this->queryFactory,
247
            $this->getClassFqn($this->query),
248
            $this->template
249
        );
250
    }
251
252
    public function getGetAccessProviderPrinter(): ModelGetAccessProviderPrinter
253
    {
254
        return new ModelGetAccessProviderPrinter(
255
            $this->namespace,
256
            $this->getAccessProvider,
257
            $this->getContractFqn($this->modelInterface),
258
            $this->single,
259
            $this->isDatableModel()
260
        );
261
    }
262
263
    public function getSetAccessProviderPrinter(): ModelSetAccessProviderPrinter
264
    {
265
        return new ModelSetAccessProviderPrinter(
266
            $this->namespace,
267
            $this->setAccessProvider,
268
            $this->getContractFqn($this->modelInterface),
269
            $this->single,
270
            $this->isMutableDatableModel()
271
        );
272
    }
273
274
    public function getTagAccessProviderPrinter(): ModelTemplateTagsProviderPrinter
275
    {
276
        return new ModelTemplateTagsProviderPrinter(
277
            $this->namespace,
278
            $this->tagAccessProvider,
279
            $this->getContractFqn($this->modelInterface),
280
            $this->single,
281
            $this->getClassFqn($this->getAccessProvider),
282
            $this->template
283
        );
284
    }
285
286
    protected function isDatableModel(): bool
287
    {
288
        return interface_exists($this->modelInterface)
289
            && is_subclass_of($this->modelInterface, DatableInterface::class);
290
    }
291
292
    protected function isMutableDatableModel(): bool
293
    {
294
        return interface_exists($this->modelInterface)
295
            && is_subclass_of($this->modelInterface, MutableDatableInterface::class);
296
    }
297
298
    protected function getAbstractFqn(string $class): string
299
    {
300
        return $this->abstracts . '\\' . $class;
301
    }
302
303
    protected function getContractFqn(string $interface): string
304
    {
305
        return $this->contracts . '\\' . $interface;
306
    }
307
308
    protected function getClassFqn(string $class): string
309
    {
310
        return $this->namespace . '\\' . $class;
311
    }
312
313
    public static function build(array $args = []): ModelComponentFactory
314
    {
315
        return new static(
316
            $args['model'],
317
            $args['namespace'],
318
            $args['contracts'],
319
            $args['abstracts'],
320
            $args['entity'],
321
            $args['single'] ?? null,
322
            $args['plural'] ?? null,
323
            $args['template'] ?? null
324
        );
325
    }
326
}
327