Passed
Push — master ( 56d79f...0f1631 )
by Peter
02:35
created

Page::toData()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 53
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 38
c 0
b 0
f 0
nc 8
nop 0
dl 0
loc 53
rs 9.312

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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