Completed
Push — 2.0 ( 359ae1...221cc7 )
by Rafał
51:27 queued 18:07
created

Article::setPublishable()   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
214
        if (isset($metadata['place']) && is_array($metadata['place']) && count($metadata['place']) > 0) {
215
            return $metadata['place'][array_key_first($metadata['place'])];
216
        }
217
218
        return null;
219
    }
220
221
    public function getPlaces(): array
222
    {
223
        $metadata = $this->getMetadata();
224
225
        if (isset($metadata['place']) && is_array($metadata['place']) && count($metadata['place']) > 0) {
226
            return $metadata['place'];
227
        }
228
229
        return [];
230
    }
231
232
    public function setTitle($title)
233
    {
234
        $this->title = $title;
235
236
        if (null !== $this->slug) {
237
            $this->setSlug($this->slug);
238
239
            return;
240
        }
241
242
        $this->setSlug($this->title);
243
    }
244
245
    public function getSlug()
246
    {
247
        return $this->slug;
248
    }
249
250
    public function setSlug($slug)
251
    {
252
        $urlizedSlug = Transliterator::urlize($slug);
253
254
        if ('' === $urlizedSlug) {
255
            $slug = str_replace('\'', '-', $slug);
256
            $this->slug = Transliterator::transliterate($slug);
257
258
            return;
259
        }
260
261
        $this->slug = $urlizedSlug;
262
    }
263
264
    public function getPublishedAt()
265
    {
266
        return $this->publishedAt;
267
    }
268
269
    public function setPublishedAt(\DateTime $publishedAt)
270
    {
271
        $this->publishedAt = $publishedAt;
272
    }
273
274
    public function getStatus()
275
    {
276
        return $this->status;
277
    }
278
279
    public function setStatus($status)
280
    {
281
        $this->status = $status;
282
    }
283
284
    public function getTemplateName()
285
    {
286
        return $this->templateName;
287
    }
288
289
    public function setTemplateName($templateName)
290
    {
291
        $this->templateName = $templateName;
292
    }
293
294
    public function getMetadata()
295
    {
296
        return $this->metadata;
297
    }
298
299
    public function getMetadataByKey(string $key)
300
    {
301
        $metadata = $this->getMetadata();
302
303
        if (isset($metadata[$key])) {
304
            return $metadata[$key];
305
        }
306
    }
307
308
    public function setMetadata(array $metadata)
309
    {
310
        $this->metadata = ArrayHelper::sortNestedArrayAssocAlphabeticallyByKey($metadata);
311
    }
312
313
    public function getSubjectType()
314
    {
315
        return 'article';
316
    }
317
318
    public function getLead()
319
    {
320
        return $this->lead;
321
    }
322
323
    public function setLead($lead)
324
    {
325
        $this->lead = $lead;
326
    }
327
328
    public function getCode(): string
329
    {
330
        return $this->code;
331
    }
332
333
    public function setCode(string $code)
334
    {
335
        $this->code = $code;
336
    }
337
338
    public function addSourceReference(ArticleSourceReferenceInterface $source)
339
    {
340
        if (!$this->hasSourceReference($source)) {
341
            $this->sources->add($source);
342
        }
343
    }
344
345
    public function removeSourceReference(ArticleSourceReferenceInterface $source)
346
    {
347
        $this->sources->removeElement($source);
348
    }
349
350
    public function hasSourceReference(ArticleSourceReferenceInterface $source): bool
351
    {
352
        return $this->sources->contains($source);
353
    }
354
355
    public function getSources(): Collection
356
    {
357
        if (0 < $this->sources->count()) {
358
            $sources = new ArrayCollection();
359
            /** @var ArticleSourceReferenceInterface $source */
360
            foreach ($this->sources as $source) {
361
                $sources->add($source->getArticleSource());
362
            }
363
364
            return $sources;
365
        }
366
367
        return $this->sources;
368
    }
369
370
    public function getExtra(): ?array
371
    {
372
        return $this->extra;
373
    }
374
375
    public function setExtra(?array $extra): void
376
    {
377
        $this->extra = $extra;
378
    }
379
380
    public function getSlideshows(): Collection
381
    {
382
        return $this->slideshows;
383
    }
384
385
    public function hasSlideshow(SlideshowInterface $slideshow): bool
386
    {
387
        return $this->slideshows->contains($slideshow);
388
    }
389
390
    public function addSlideshow(SlideshowInterface $slideshow): void
391
    {
392
        if (!$this->hasSlideshow($slideshow)) {
393
            $slideshow->setArticle($this);
394
            $this->slideshows->add($slideshow);
395
        }
396
    }
397
398
    public function removeSlideshow(SlideshowInterface $slideshow): void
399
    {
400
        if ($this->hasSlideshow($slideshow)) {
401
            $slideshow->setArticle(null);
402
            $this->slideshows->removeElement($slideshow);
403
        }
404
    }
405
}
406