Passed
Push — master ( 6af672...0107a4 )
by Peter
07:37
created

Page::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
c 1
b 0
f 0
nc 2
nop 13
dl 0
loc 28
rs 9.8333

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
declare(strict_types=1);
4
5
namespace AbterPhp\Website\Domain\Entities;
6
7
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
8
use AbterPhp\Website\Domain\Entities\Page\Assets;
9
use AbterPhp\Website\Domain\Entities\Page\Meta;
10
11
/**
12
 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
13
 */
14
class Page implements IStringerEntity
15
{
16
    /** @var string */
17
    protected $id;
18
19
    /** @var string */
20
    protected $identifier;
21
22
    /** @var string */
23
    protected $title;
24
25
    /** @var string */
26
    protected $classes;
27
28
    /** @var string */
29
    protected $lede;
30
31
    /** @var string */
32
    protected $body;
33
34
    /** @var bool */
35
    protected $markedAsDraft;
36
37
    /** @var PageCategory|null */
38
    protected $category;
39
40
    /** @var string */
41
    protected $layout;
42
43
    /** @var string|null */
44
    protected $layoutId;
45
46
    /** @var Meta */
47
    protected $meta;
48
49
    /** @var Assets|null */
50
    protected $assets;
51
52
    /** @var string|null */
53
    protected $renderedBody;
54
55
    /**
56
     * Page constructor.
57
     *
58
     * @param string            $id
59
     * @param string            $identifier
60
     * @param string            $title
61
     * @param string            $classes
62
     * @param string            $lede
63
     * @param string            $body
64
     * @param PageCategory|null $category
65
     * @param string            $layout
66
     * @param string|null       $layoutId
67
     * @param Meta|null         $meta
68
     * @param Assets|null       $assets
69
     * @param string|null       $renderedBody
70
     */
71
    public function __construct(
72
        string $id,
73
        string $identifier,
74
        string $title,
75
        string $classes,
76
        string $lede,
77
        string $body,
78
        bool $isDraft,
79
        ?PageCategory $category = null,
80
        string $layout = '',
81
        ?string $layoutId = null,
82
        ?Meta $meta = null,
83
        ?Assets $assets = null,
84
        ?string $renderedBody = null
85
    ) {
86
        $this->id            = $id;
87
        $this->identifier    = $identifier;
88
        $this->title         = $title;
89
        $this->classes       = $classes;
90
        $this->lede          = $lede;
91
        $this->body          = $body;
92
        $this->markedAsDraft = $isDraft;
93
        $this->category      = $category;
94
        $this->layout        = $layout;
95
        $this->layoutId      = $layoutId ? $layoutId : null;
96
        $this->meta          = $meta ?: new Meta('', '', '', '', '', '', '', '');
97
        $this->assets        = $assets;
98
        $this->renderedBody  = $renderedBody;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getId()
105
    {
106
        return $this->id;
107
    }
108
109
    /**
110
     * @param string $id
111
     */
112
    public function setId($id)
113
    {
114
        $this->id = $id;
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function getIdentifier(): string
121
    {
122
        return $this->identifier;
123
    }
124
125
    /**
126
     * @param string $identifier
127
     *
128
     * @return $this
129
     */
130
    public function setIdentifier(string $identifier): Page
131
    {
132
        $this->identifier = $identifier;
133
134
        return $this;
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public function getTitle(): string
141
    {
142
        return $this->title;
143
    }
144
145
    /**
146
     * @param string $title
147
     *
148
     * @return $this
149
     */
150
    public function setTitle(string $title): Page
151
    {
152
        $this->title = $title;
153
154
        return $this;
155
    }
156
157
    /**
158
     * @return string
159
     */
160
    public function getClasses(): string
161
    {
162
        return $this->classes;
163
    }
164
165
    /**
166
     * @param string $classes
167
     *
168
     * @return $this
169
     */
170
    public function setClasses(string $classes): Page
171
    {
172
        $this->classes = $classes;
173
174
        return $this;
175
    }
176
177
    /**
178
     * @return string
179
     */
180
    public function getLede(): string
181
    {
182
        return $this->lede;
183
    }
184
185
    /**
186
     * @param string $lede
187
     *
188
     * @return $this
189
     */
190
    public function setLede(string $lede): Page
191
    {
192
        $this->lede = $lede;
193
194
        return $this;
195
    }
196
197
    /**
198
     * @return string
199
     */
200
    public function getBody(): string
201
    {
202
        return $this->body;
203
    }
204
205
    /**
206
     * @param string $body
207
     *
208
     * @return $this
209
     */
210
    public function setBody(string $body): Page
211
    {
212
        $this->body = $body;
213
214
        return $this;
215
    }
216
217
    /**
218
     * @return bool
219
     */
220
    public function isDraft(): bool
221
    {
222
        return $this->markedAsDraft;
223
    }
224
225
    /**
226
     * @param string $isDraft
227
     *
228
     * @return $this
229
     */
230
    public function setIsDraft(bool $isDraft): Page
231
    {
232
        $this->markedAsDraft = $isDraft;
233
234
        return $this;
235
    }
236
237
    /**
238
     * @return bool
239
     */
240
    public function hasRenderedBody(): bool
241
    {
242
        return !($this->renderedBody === null);
243
    }
244
245
    /**
246
     * @return string|null
247
     */
248
    public function getRenderedBody(): ?string
249
    {
250
        return $this->renderedBody;
251
    }
252
253
    /**
254
     * @param string|null $renderedBody
255
     *
256
     * @return $this
257
     */
258
    public function setRenderedBody(?string $renderedBody): Page
259
    {
260
        $this->renderedBody = $renderedBody;
261
262
        return $this;
263
    }
264
265
    /**
266
     * @return PageCategory|null
267
     */
268
    public function getCategory(): ?PageCategory
269
    {
270
        return $this->category;
271
    }
272
273
    /**
274
     * @param PageCategory|null $category
275
     *
276
     * @return $this
277
     */
278
    public function setCategory(?PageCategory $category): Page
279
    {
280
        $this->category = $category;
281
282
        return $this;
283
    }
284
285
    /**
286
     * @return string
287
     */
288
    public function getLayout(): string
289
    {
290
        return $this->layout;
291
    }
292
293
    /**
294
     * @param string $layout
295
     *
296
     * @return $this
297
     */
298
    public function setLayout(string $layout): Page
299
    {
300
        $this->layout = $layout;
301
302
        return $this;
303
    }
304
305
    /**
306
     * @return string|null
307
     */
308
    public function getLayoutId(): ?string
309
    {
310
        return $this->layoutId;
311
    }
312
313
    /**
314
     * @param string|null $layoutId
315
     *
316
     * @return $this
317
     */
318
    public function setLayoutId(?string $layoutId): Page
319
    {
320
        if ($layoutId === '') {
321
            $layoutId = null;
322
        }
323
324
        $this->layoutId = $layoutId;
325
326
        return $this;
327
    }
328
329
    /**
330
     * @return Meta
331
     */
332
    public function getMeta(): Meta
333
    {
334
        return $this->meta;
335
    }
336
337
    /**
338
     * @param Meta $meta
339
     *
340
     * @return $this
341
     */
342
    public function setMeta(Meta $meta): Page
343
    {
344
        $this->meta = $meta;
345
346
        return $this;
347
    }
348
349
    /**
350
     * @return Assets|null
351
     */
352
    public function getAssets(): ?Assets
353
    {
354
        return $this->assets;
355
    }
356
357
    /**
358
     * @param Assets|null $assets
359
     *
360
     * @return $this
361
     */
362
    public function setAssets(?Assets $assets): Page
363
    {
364
        $this->assets = $assets;
365
366
        return $this;
367
    }
368
369
    /**
370
     * @return string
371
     */
372
    public function __toString(): string
373
    {
374
        return $this->getIdentifier();
375
    }
376
377
    /**
378
     * @return array|null
379
     */
380
    public function toData(): ?array
381
    {
382
        $meta   = $this->getMeta();
383
        $assets = $this->getAssets();
384
385
        $category = null;
386
        if ($this->getCategory()) {
387
            $category = [
388
                'id' => $this->getCategory()->getId(),
389
            ];
390
        }
391
392
        $assetsData = null;
393
        if ($assets) {
394
            $assetsData = [
395
                'key'       => $assets->getKey(),
396
                'header'    => $assets->getHeader(),
397
                'footer'    => $assets->getFooter(),
398
                'css_files' => $assets->getCssFiles(),
399
                'js_files'  => $assets->getJsFiles(),
400
            ];
401
        }
402
403
        $meta = [
404
            'description'    => $meta->getDescription(),
405
            'robots'         => $meta->getRobots(),
406
            'author'         => $meta->getAuthor(),
407
            'copyright'      => $meta->getCopyright(),
408
            'keywords'       => $meta->getKeywords(),
409
            'og_title'       => $meta->getOGTitle(),
410
            'og_image'       => $meta->getOGImage(),
411
            'og_description' => $meta->getOGDescription(),
412
        ];
413
414
        $data = [
415
            'id'         => $this->getId(),
416
            'identifier' => $this->getIdentifier(),
417
            'title'      => $this->getTitle(),
418
            'classes'    => $this->getTitle(),
419
            'lede'       => $this->getLede(),
420
            'body'       => $this->getBody(),
421
            'is_draft'   => $this->isDraft(),
422
            'category'   => $category,
423
            'layout'     => $this->getLayout(),
424
            'layout_id'  => $this->getLayoutId(),
425
            'meta'       => $meta,
426
            'assets'     => $assetsData,
427
        ];
428
429
        if ($this->hasRenderedBody()) {
430
            $data['rendered'] = $this->getRenderedBody();
431
        }
432
433
        return $data;
434
    }
435
436
    /**
437
     * @return string
438
     */
439
    public function toJSON(): string
440
    {
441
        return json_encode($this->toData());
442
    }
443
}
444