Completed
Push — master ( 6da6df...dc6a94 )
by Rafał
09:28
created

Article::getKeywords()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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