Completed
Push — master ( 3b32ca...e1f264 )
by Rafał
16:19 queued 07:01
created

Article::setExtraFields()   B

Complexity

Conditions 6
Paths 13

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 0
cp 0
rs 8.9457
c 0
b 0
f 0
cc 6
nc 13
nop 1
crap 42
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Content Bundle.
7
 *
8
 * Copyright 2016 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2016 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\ContentBundle\Model;
18
19
use Behat\Transliterator\Transliterator;
20
use Doctrine\Common\Collections\ArrayCollection;
21
use Doctrine\Common\Collections\Collection;
22
use SWP\Bundle\ContentBundle\Doctrine\ORM\TimestampableCancelTrait;
23
use SWP\Component\Bridge\Model\AuthorsAwareTrait;
24
use SWP\Component\Common\Model\DateTime;
25
use SWP\Component\Common\Model\SoftDeletableTrait;
26
use SWP\Component\Common\Model\TimestampableTrait;
27
use SWP\Component\Common\Model\TranslatableTrait;
28
use SWP\Component\Seo\Model\SeoMetadataAwareTrait;
29
30
class Article implements ArticleInterface
31
{
32
    use TranslatableTrait;
33
    use SoftDeletableTrait;
34
    use TimestampableTrait;
35
    use AuthorsAwareTrait;
36
    use KeywordsAwareTrait;
37
    use RelatedArticlesAwareTrait;
38
    use TimestampableCancelTrait;
39
    use SeoMetadataAwareTrait;
40
    use MediaAwareTrait;
41
42
    /**
43
     * @var mixed
44
     */
45
    protected $id;
46
47
    /**
48
     * @var string
49
     */
50
    protected $title;
51
52
    /**
53
     * @var string
54
     */
55
    protected $body;
56
57
    /**
58
     * @var string
59
     */
60
    protected $slug;
61
62
    /**
63
     * @var \DateTime
64
     */
65
    protected $publishedAt;
66
67
    /**
68
     * @var string
69
     */
70
    protected $status = ArticleInterface::STATUS_NEW;
71
72
    /**
73
     * @var RouteInterface
74
     */
75
    protected $route;
76
77
    /**
78
     * @var string
79
     */
80
    protected $templateName;
81
82
    /**
83
     * @var \DateTime
84
     */
85
    protected $publishStartDate;
86
87
    /**
88
     * @var \DateTime
89
     */
90
    protected $publishEndDate;
91
92
    /**
93
     * @var bool
94
     */
95
    protected $isPublishable;
96
97
    /** @var array */
98
    protected $metadata = [];
99
100
    /**
101
     * @var string
102
     */
103
    protected $lead;
104
105
    /**
106
     * @var string
107
     */
108
    protected $code;
109
110
    /**
111
     * @var Collection|ArticleSourceInterface[]
112
     */
113
    protected $sources;
114
115
    /**
116
     * @var Collection|SlideshowInterface[]
117
     */
118 33
    protected $slideshows;
119
120 33
    /** @var Collection|ArticlePreviousRelativeUrlInterface[] * */
121 33
    protected $previousRelativeUrls;
122 33
123 33
    /** @var MetadataInterface|null */
124
    protected $data;
125
126
    /**
127
     * @var Collection|ArticleExtraTextFieldInterface[]
128
     */
129
    protected $extraTextFields;
130
131
    /**
132
     * @var Collection|ArticleExtraTextFieldInterface[]
133
     */
134
    protected $extraEmbedFields;
135
136 9
    public function __construct()
137
    {
138 9
        $this->createdAt = DateTime::getCurrentDateTime();
139
        $this->setPublishable(false);
140
        $this->setMedia(new ArrayCollection());
141
        $this->sources = new ArrayCollection();
142
        $this->authors = new ArrayCollection();
143
        $this->keywords = new ArrayCollection();
144
        $this->slideshows = new ArrayCollection();
145
        $this->relatedArticles = new ArrayCollection();
146
        $this->previousRelativeUrls = new ArrayCollection();
147
        $this->extraTextFields = new ArrayCollection();
148
        $this->extraEmbedFields = new ArrayCollection();
149
    }
150
151
    public function setPublishStartDate(\DateTime $startDate = null)
152 9
    {
153
        $this->publishStartDate = $startDate;
154 9
    }
155
156
    public function getPublishStartDate()
157
    {
158
        return $this->publishStartDate;
159
    }
160 5
161
    public function setPublishEndDate(\DateTime $endDate = null)
162 5
    {
163
        $this->publishEndDate = $endDate;
164
    }
165
166
    public function getPublishEndDate()
167
    {
168 32
        return $this->publishEndDate;
169
    }
170 32
171 32
    public function isPublishable()
172
    {
173
        return $this->isPublishable;
174
    }
175
176 11
    public function setPublishable($boolean)
177
    {
178 11
        $this->isPublishable = $boolean;
179
    }
180
181
    public function setIsPublishable(bool $boolean): void
182
    {
183
        $this->setPublishable($boolean);
184 9
    }
185
186 9
    public function isPublished()
187
    {
188
        return ArticleInterface::STATUS_PUBLISHED === $this->getStatus();
189
    }
190
191
    public function setRoute(RouteInterface $route = null)
192 9
    {
193
        $this->route = $route;
194 9
    }
195
196
    public function getRoute()
197
    {
198
        return $this->route;
199
    }
200 32
201
    public function getId()
202 32
    {
203 32
        return $this->id;
204
    }
205
206
    public function getBody()
207
    {
208 4
        return $this->body;
209
    }
210 4
211 4
    public function setBody($body)
212
    {
213
        $this->body = \trim($body);
214
    }
215
216 27
    public function getTitle()
217
    {
218 27
        return $this->title;
219 27
    }
220
221
    public function getPlace(): ?array
222
    {
223
        $metadata = $this->getMetadata();
224 23
225
        if (isset($metadata['place']) && is_array($metadata['place']) && count($metadata['place']) > 0) {
226 23
            return $metadata['place'][array_key_first($metadata['place'])];
227
        }
228
229
        return null;
230
    }
231
232 12
    public function getPlaces(): array
233
    {
234 12
        $metadata = $this->getMetadata();
235
236
        if (isset($metadata['place']) && is_array($metadata['place']) && count($metadata['place']) > 0) {
237
            return $metadata['place'];
238
        }
239
240 9
        return [];
241
    }
242 9
243
    public function setTitle($title)
244
    {
245
        $this->title = $title;
246
247
        if (null !== $this->slug && '' !== $this->slug) {
248 32
            $this->setSlug($this->slug);
249
250 32
            return;
251 32
        }
252
253
        $this->setSlug($this->title);
254
    }
255
256
    public function getSlug()
257
    {
258
        return $this->slug;
259
    }
260
261
    public function setSlug($slug)
262
    {
263
        $urlizedSlug = Transliterator::urlize($slug);
264 32
265
        if ('' === $urlizedSlug) {
266 32
            $slug = str_replace('\'', '-', $slug);
267 32
            $this->slug = Transliterator::transliterate($slug);
268
269
            return;
270
        }
271
272 9
        $this->slug = $urlizedSlug;
273
    }
274 9
275
    public function getPublishedAt()
276
    {
277
        return $this->publishedAt;
278
    }
279
280 32
    public function setPublishedAt(\DateTime $publishedAt)
281
    {
282 32
        $this->publishedAt = $publishedAt;
283
    }
284 32
285 32
    public function getStatus()
286
    {
287
        return $this->status;
288
    }
289
290 27
    public function setStatus($status)
291
    {
292 27
        $this->status = $status;
293
    }
294
295
    public function getTemplateName()
296
    {
297
        return $this->templateName;
298 32
    }
299
300 32
    public function setTemplateName($templateName)
301 32
    {
302
        $this->templateName = $templateName;
303
    }
304
305
    public function getMetadataByKey(string $key)
306 12
    {
307
        $metadata = $this->getMetadata();
308 12
309
        if (isset($metadata[$key])) {
310
            return $metadata[$key];
311
        }
312
    }
313
314 19
    public function getExtraByKey(string $key): ?ArticleExtraFieldInterface
315
    {
316 19
        foreach ($this->getExtraCollection() as $extraField) {
317 19
            if ($key === $extraField->getFieldName()) {
318
                return $extraField;
319
            }
320
        }
321
322 11
        return null;
323
    }
324 11
325
    private function getExtraCollection(): Collection
326
    {
327
        return new ArrayCollection(
328
            array_merge($this->extraTextFields->toArray(), $this->extraEmbedFields->toArray())
329
        );
330 24
    }
331
332 24
    public function getExtraArray(): array
333 24
    {
334
        return $this->getExtraCollection()
335
            ->map(
336
                function (ArticleExtraFieldInterface $field) {
337
                    return $field->toApiFormat();
338 9
                }
339
            )->toArray();
340 9
    }
341
342
    public function setData(?MetadataInterface $metadata): void
343
    {
344
        $this->data = $metadata;
345
346 13
        if ($metadata instanceof MetadataInterface) {
347
            $metadata->setArticle($this);
348 13
        }
349 13
    }
350
351
    public function getData(): ?MetadataInterface
352
    {
353
        return $this->data;
354 23
    }
355
356 23
    public function getSubjectType()
357
    {
358
        return 'article';
359
    }
360
361
    public function getLead()
362 9
    {
363
        return $this->lead;
364 9
    }
365
366 9
    public function setLead($lead)
367 9
    {
368
        $this->lead = $lead;
369
    }
370
371
    public function getCode(): string
372
    {
373
        return $this->code;
374 12
    }
375
376 12
    public function setCode(string $code)
377 12
    {
378
        $this->code = $code;
379
    }
380
381
    public function addSourceReference(ArticleSourceReferenceInterface $source)
382 8
    {
383
        if (!$this->hasSourceReference($source)) {
384 8
            $this->sources->add($source);
385
        }
386
    }
387
388
    public function removeSourceReference(ArticleSourceReferenceInterface $source)
389
    {
390 9
        $this->sources->removeElement($source);
391
    }
392 9
393
    public function hasSourceReference(ArticleSourceReferenceInterface $source): bool
394
    {
395
        return $this->sources->contains($source);
396
    }
397
398 12
    public function getSources(): Collection
399
    {
400 12
        if (0 < $this->sources->count()) {
401 12
            $sources = new ArrayCollection();
402
            /** @var ArticleSourceReferenceInterface $source */
403
            foreach ($this->sources as $source) {
404
                $sources->add($source->getArticleSource());
405
            }
406 9
407
            return $sources;
408 9
        }
409
410
        return $this->sources;
411
    }
412
413
    public function getExtra(): array
414 12
    {
415
        return $this->getExtraArray();
416 12
    }
417 12
418
    public function setExtra(?array $extra): void
419
    {
420
        $this->setExtraFields($extra);
421
    }
422
423
    public function getSlideshows(): Collection
424
    {
425
        return $this->slideshows;
426
    }
427
428
    public function hasSlideshow(SlideshowInterface $slideshow): bool
429
    {
430
        return $this->slideshows->contains($slideshow);
431
    }
432
433
    public function addSlideshow(SlideshowInterface $slideshow): void
434
    {
435
        if (!$this->hasSlideshow($slideshow)) {
436
            $slideshow->setArticle($this);
437
            $this->slideshows->add($slideshow);
438
        }
439
    }
440
441
    public function removeSlideshow(SlideshowInterface $slideshow): void
442
    {
443
        if ($this->hasSlideshow($slideshow)) {
444
            $slideshow->setArticle(null);
445
            $this->slideshows->removeElement($slideshow);
446
        }
447
    }
448
449
    public function getPreviousRelativeUrl(): Collection
450
    {
451
        return $this->previousRelativeUrls;
452
    }
453
454
    public function hasPreviousRelativeUrl(ArticlePreviousRelativeUrlInterface $previousRelativeUrl): bool
455
    {
456
        return $this->previousRelativeUrls->contains($previousRelativeUrl);
457
    }
458
459
    public function addPreviousRelativeUrl(ArticlePreviousRelativeUrlInterface $previousRelativeUrl): void
460
    {
461
        if (!$this->hasPreviousRelativeUrl($previousRelativeUrl)) {
462
            $previousRelativeUrl->setArticle($this);
463
            $this->previousRelativeUrls->add($previousRelativeUrl);
464
        }
465
    }
466
467
    public function removePreviousRelativeUrl(ArticlePreviousRelativeUrlInterface $previousRelativeUrl): void
468
    {
469
        if ($this->hasPreviousRelativeUrl($previousRelativeUrl)) {
470
            $previousRelativeUrl->setArticle(null);
471
            $this->previousRelativeUrls->removeElement($previousRelativeUrl);
472
        }
473
    }
474
475
    public function getMetadata(): array
476
    {
477
        return $this->metadata;
478
    }
479
480
    public function setMetadata(array $metadata): void
481
    {
482
        $this->metadata = $metadata;
483
    }
484
485
    public function addTextExtra(ArticleExtraTextFieldInterface $articleExtra): void
486
    {
487
        if (!$this->extraTextFields->contains($articleExtra)) {
488
            $this->extraTextFields[$articleExtra->getFieldName()] = $articleExtra;
489
            $articleExtra->setArticle($this);
490
        }
491
    }
492
493
    public function addEmbedExtra(ArticleExtraEmbedFieldInterface $articleExtra): void
494
    {
495
        if (!$this->extraEmbedFields->contains($articleExtra)) {
496
            $this->extraEmbedFields[$articleExtra->getFieldName()] = $articleExtra;
497
            $articleExtra->setArticle($this);
498
        }
499
    }
500
501
    public function removeExtraTextFields(ArticleExtraFieldInterface $articleExtra): void
502
    {
503
        $this->extraTextFields->removeElement($articleExtra);
504
    }
505
506
    public function removeExtraEmbedFields(ArticleExtraEmbedFieldInterface $articleExtra): void
507
    {
508
        $this->extraEmbedFields->removeElement($articleExtra);
509
    }
510
511
    public function getExtraTextFields(): Collection
512
    {
513
        return $this->extraTextFields;
514
    }
515
516
    public function getExtraEmbedFields(): Collection
517
    {
518
        return $this->extraEmbedFields;
519
    }
520
521
    public function setExtraFields(array $extra): void
522
    {
523
        if (0 === count($extra)) {
524
            return;
525
        }
526
527
        foreach ($this->getExtraTextFields() as $extraTextField) {
528
            $this->removeExtraTextFields($extraTextField);
529
        }
530
531
        foreach ($this->getExtraEmbedFields() as $extraEmbedField) {
532
            $this->removeExtraEmbedFields($extraEmbedField);
533
        }
534
535
        foreach ($extra as $key => $value) {
536
            if (is_array($value)) {
537
                $this->addEmbedExtra(ArticleExtraEmbedField::newFromValue($key, $value));
538
            } else {
539
                $this->addTextExtra(ArticleExtraTextField::newFromValue($key, $value));
540
            }
541
        }
542
    }
543
}
544