Completed
Push — 2.0 ( 221cc7...09cdd8 )
by Rafał
73:29 queued 42:56
created

Article::setMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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\Bundle\ContentBundle\Doctrine\ORM\TimestampableCancelTrait;
23
use SWP\Component\Bridge\Model\AuthorsAwareTrait;
24
use SWP\Component\Common\ArrayHelper;
25
use SWP\Component\Common\Model\DateTime;
26
use SWP\Component\Common\Model\SoftDeletableTrait;
27
use SWP\Component\Common\Model\TimestampableTrait;
28
use SWP\Component\Common\Model\TranslatableTrait;
29
use SWP\Component\Seo\Model\SeoMetadataAwareTrait;
30
31
class Article implements ArticleInterface
32
{
33
    use TranslatableTrait;
34
    use SoftDeletableTrait;
35
    use TimestampableTrait;
36
    use AuthorsAwareTrait;
37
    use KeywordsAwareTrait;
38
    use RelatedArticlesAwareTrait;
39
    use TimestampableCancelTrait;
40
    use SeoMetadataAwareTrait;
41
    use MediaAwareTrait;
42
43
    /**
44
     * @var mixed
45
     */
46
    protected $id;
47
48
    /**
49
     * @var string
50
     */
51
    protected $title;
52
53
    /**
54
     * @var string
55
     */
56
    protected $body;
57
58
    /**
59
     * @var string
60
     */
61
    protected $slug;
62
63
    /**
64
     * @var \DateTime
65
     */
66
    protected $publishedAt;
67
68
    /**
69
     * @var string
70
     */
71
    protected $status = ArticleInterface::STATUS_NEW;
72
73
    /**
74
     * @var RouteInterface
75
     */
76
    protected $route;
77
78
    /**
79
     * @var string
80
     */
81
    protected $templateName;
82
83
    /**
84
     * @var \DateTime
85
     */
86
    protected $publishStartDate;
87
88
    /**
89
     * @var \DateTime
90
     */
91
    protected $publishEndDate;
92
93
    /**
94
     * @var bool
95
     */
96
    protected $isPublishable;
97
98
    /**
99
     * @var array
100
     */
101
    protected $metadata = [];
102
103
    /**
104
     * @var string
105
     */
106
    protected $lead;
107
108
    /**
109
     * @var string
110
     */
111
    protected $code;
112
113
    /**
114
     * @var Collection|ArticleSourceInterface[]
115
     */
116
    protected $sources;
117
118
    /**
119
     * @var array|null
120
     */
121
    protected $extra;
122
123
    /**
124
     * @var Collection|SlideshowInterface[]
125
     */
126
    protected $slideshows;
127
128
    public function __construct()
129
    {
130
        $this->createdAt = DateTime::getCurrentDateTime();
131
        $this->setPublishable(false);
132
        $this->setMedia(new ArrayCollection());
133
        $this->sources = new ArrayCollection();
134
        $this->authors = new ArrayCollection();
135
        $this->keywords = new ArrayCollection();
136
        $this->slideshows = new ArrayCollection();
137
        $this->relatedArticles = new ArrayCollection();
138
    }
139
140
    public function setPublishStartDate(\DateTime $startDate = null)
141
    {
142
        $this->publishStartDate = $startDate;
143
    }
144
145
    public function getPublishStartDate()
146
    {
147
        return $this->publishStartDate;
148
    }
149
150
    public function setPublishEndDate(\DateTime $endDate = null)
151
    {
152
        $this->publishEndDate = $endDate;
153
    }
154
155
    public function getPublishEndDate()
156
    {
157
        return $this->publishEndDate;
158
    }
159
160
    public function isPublishable()
161
    {
162
        return $this->isPublishable;
163
    }
164
165
    public function setPublishable($boolean)
166
    {
167
        $this->isPublishable = $boolean;
168
    }
169
170
    public function setIsPublishable(bool $boolean): void
171
    {
172
        $this->setPublishable($boolean);
173
    }
174
175
    public function isPublished()
176
    {
177
        return ArticleInterface::STATUS_PUBLISHED === $this->getStatus();
178
    }
179
180
    public function setRoute(RouteInterface $route = null)
181
    {
182
        $this->route = $route;
183
    }
184
185
    public function getRoute()
186
    {
187
        return $this->route;
188
    }
189
190
    public function getId()
191
    {
192
        return $this->id;
193
    }
194
195
    public function getBody()
196
    {
197
        return $this->body;
198
    }
199
200
    public function setBody($body)
201
    {
202
        $this->body = \trim($body);
203
    }
204
205
    public function getTitle()
206
    {
207
        return $this->title;
208
    }
209
210
    public function getPlace(): ?array
211
    {
212
        $metadata = $this->getMetadata();
213
        if (is_array($metadata['place']) && count($metadata['place']) > 0) {
214
            return $metadata['place'][array_key_first($metadata['place'])];
215
        }
216
217
        return null;
218
    }
219
220
    public function setTitle($title)
221
    {
222
        $this->title = $title;
223
224
        if (null !== $this->slug) {
225
            $this->setSlug($this->slug);
226
227
            return;
228
        }
229
230
        $this->setSlug($this->title);
231
    }
232
233
    public function getSlug()
234
    {
235
        return $this->slug;
236
    }
237
238
    public function setSlug($slug)
239
    {
240
        $urlizedSlug = Transliterator::urlize($slug);
241
242
        if ('' === $urlizedSlug) {
243
            $slug = str_replace('\'', '-', $slug);
244
            $this->slug = Transliterator::transliterate($slug);
245
246
            return;
247
        }
248
249
        $this->slug = $urlizedSlug;
250
    }
251
252
    public function getPublishedAt()
253
    {
254
        return $this->publishedAt;
255
    }
256
257
    public function setPublishedAt(\DateTime $publishedAt)
258
    {
259
        $this->publishedAt = $publishedAt;
260
    }
261
262
    public function getStatus()
263
    {
264
        return $this->status;
265
    }
266
267
    public function setStatus($status)
268
    {
269
        $this->status = $status;
270
    }
271
272
    public function getTemplateName()
273
    {
274
        return $this->templateName;
275
    }
276
277
    public function setTemplateName($templateName)
278
    {
279
        $this->templateName = $templateName;
280
    }
281
282
    public function getMetadata()
283
    {
284
        return $this->metadata;
285
    }
286
287
    public function getMetadataByKey(string $key)
288
    {
289
        $metadata = $this->getMetadata();
290
291
        if (isset($metadata[$key])) {
292
            return $metadata[$key];
293
        }
294
    }
295
296
    public function setMetadata(array $metadata)
297
    {
298
        $this->metadata = ArrayHelper::sortNestedArrayAssocAlphabeticallyByKey($metadata);
299
    }
300
301
    public function getSubjectType()
302
    {
303
        return 'article';
304
    }
305
306
    public function getLead()
307
    {
308
        return $this->lead;
309
    }
310
311
    public function setLead($lead)
312
    {
313
        $this->lead = $lead;
314
    }
315
316
    public function getCode(): string
317
    {
318
        return $this->code;
319
    }
320
321
    public function setCode(string $code)
322
    {
323
        $this->code = $code;
324
    }
325
326
    public function addSourceReference(ArticleSourceReferenceInterface $source)
327
    {
328
        if (!$this->hasSourceReference($source)) {
329
            $this->sources->add($source);
330
        }
331
    }
332
333
    public function removeSourceReference(ArticleSourceReferenceInterface $source)
334
    {
335
        $this->sources->removeElement($source);
336
    }
337
338
    public function hasSourceReference(ArticleSourceReferenceInterface $source): bool
339
    {
340
        return $this->sources->contains($source);
341
    }
342
343
    public function getSources(): Collection
344
    {
345
        if (0 < $this->sources->count()) {
346
            $sources = new ArrayCollection();
347
            /** @var ArticleSourceReferenceInterface $source */
348
            foreach ($this->sources as $source) {
349
                $sources->add($source->getArticleSource());
350
            }
351
352
            return $sources;
353
        }
354
355
        return $this->sources;
356
    }
357
358
    public function getExtra(): ?array
359
    {
360
        return $this->extra;
361
    }
362
363
    public function setExtra(?array $extra): void
364
    {
365
        $this->extra = $extra;
366
    }
367
368
    public function getSlideshows(): Collection
369
    {
370
        return $this->slideshows;
371
    }
372
373
    public function hasSlideshow(SlideshowInterface $slideshow): bool
374
    {
375
        return $this->slideshows->contains($slideshow);
376
    }
377
378
    public function addSlideshow(SlideshowInterface $slideshow): void
379
    {
380
        if (!$this->hasSlideshow($slideshow)) {
381
            $slideshow->setArticle($this);
382
            $this->slideshows->add($slideshow);
383
        }
384
    }
385
386
    public function removeSlideshow(SlideshowInterface $slideshow): void
387
    {
388
        if ($this->hasSlideshow($slideshow)) {
389
            $slideshow->setArticle(null);
390
            $this->slideshows->removeElement($slideshow);
391
        }
392
    }
393
}
394