Test Failed
Push — master ( 37e761...633337 )
by Chris
18:21
created

getGetAccessProviderPrinter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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