Completed
Push — 1.5 ( 6568c9...51c4c2 )
by Rafał
11:32
created

Article::setIsPublishable()   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\Model\DateTime;
25
use SWP\Component\Common\Model\SoftDeletableTrait;
26
use SWP\Component\Common\Model\TimestampableTrait;
27
use SWP\Component\Common\Model\TranslatableTrait;
28
29
/**
30
 * Class Article.
31
 */
32
class Article implements ArticleInterface
33
{
34
    use TranslatableTrait;
35
    use SoftDeletableTrait;
36
    use TimestampableTrait;
37
    use AuthorsAwareTrait;
38
    use KeywordsAwareTrait;
39
    use RelatedArticlesAwareTrait;
40
    use TimestampableCancelTrait;
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
    /**
98
     * @var array
99
     */
100
    protected $metadata = [];
101
102
    /**
103
     * @var Collection
104
     */
105
    protected $media;
106
107
    /**
108
     * @var ArticleMediaInterface
109
     */
110
    protected $featureMedia;
111
112
    /**
113
     * @var string
114
     */
115
    protected $lead;
116
117
    /**
118
     * @var string
119
     */
120
    protected $code;
121
122
    /**
123
     * @var Collection|ArticleSourceInterface[]
124
     */
125
    protected $sources;
126
127
    /**
128
     * @var array|null
129
     */
130
    protected $extra;
131
132
    /**
133
     * @var Collection|SlideshowInterface[]
134
     */
135
    protected $slideshows;
136
137
    public function __construct()
138
    {
139
        $this->createdAt = DateTime::getCurrentDateTime();
140
        $this->setPublishable(false);
141
        $this->setMedia(new ArrayCollection());
142
        $this->sources = new ArrayCollection();
143
        $this->authors = new ArrayCollection();
144
        $this->keywords = new ArrayCollection();
145
        $this->slideshows = new ArrayCollection();
146
        $this->relatedArticles = new ArrayCollection();
147
    }
148
149
    public function setPublishStartDate(\DateTime $startDate = null)
150
    {
151
        $this->publishStartDate = $startDate;
152
    }
153
154
    public function getPublishStartDate()
155
    {
156
        return $this->publishStartDate;
157
    }
158
159
    public function setPublishEndDate(\DateTime $endDate = null)
160
    {
161
        $this->publishEndDate = $endDate;
162
    }
163
164
    public function getPublishEndDate()
165
    {
166
        return $this->publishEndDate;
167
    }
168
169
    public function isPublishable()
170
    {
171
        return $this->isPublishable;
172
    }
173
174
    public function setPublishable($boolean)
175
    {
176
        $this->isPublishable = $boolean;
177
    }
178
179
    public function setIsPublishable(bool $boolean): void
180
    {
181
        $this->setPublishable($boolean);
182
    }
183
184
    public function isPublished()
185
    {
186
        return ArticleInterface::STATUS_PUBLISHED === $this->getStatus();
187
    }
188
189
    public function setRoute(RouteInterface $route = null)
190
    {
191
        $this->route = $route;
192
    }
193
194
    public function getRoute()
195
    {
196
        return $this->route;
197
    }
198
199
    public function getId()
200
    {
201
        return $this->id;
202
    }
203
204
    public function getBody()
205
    {
206
        return $this->body;
207
    }
208
209
    public function setBody($body)
210
    {
211
        $this->body = \trim($body);
212
    }
213
214
    public function getMedia()
215
    {
216
        return $this->media;
217
    }
218
219
    public function setMedia(Collection $media)
220
    {
221
        $this->media = $media;
222
    }
223
224
    public function getTitle()
225
    {
226
        return $this->title;
227
    }
228
229
    public function setTitle($title)
230
    {
231
        $this->title = $title;
232
233
        if (null !== $this->slug) {
234
            $this->setSlug($this->slug);
235
236
            return;
237
        }
238
239
        $this->setSlug($this->title);
240
    }
241
242
    public function getSlug()
243
    {
244
        return $this->slug;
245
    }
246
247
    public function setSlug($slug)
248
    {
249
        $urlizedSlug = Transliterator::urlize($slug);
250
251
        if ('' === $urlizedSlug) {
252
            $slug = str_replace('\'', '-', $slug);
253
            $this->slug = Transliterator::transliterate($slug);
254
255
            return;
256
        }
257
258
        $this->slug = $urlizedSlug;
259
    }
260
261
    public function getPublishedAt()
262
    {
263
        return $this->publishedAt;
264
    }
265
266
    public function setPublishedAt(\DateTime $publishedAt)
267
    {
268
        $this->publishedAt = $publishedAt;
269
    }
270
271
    public function getStatus()
272
    {
273
        return $this->status;
274
    }
275
276
    public function setStatus($status)
277
    {
278
        $this->status = $status;
279
    }
280
281
    public function getTemplateName()
282
    {
283
        return $this->templateName;
284
    }
285
286
    public function setTemplateName($templateName)
287
    {
288
        $this->templateName = $templateName;
289
    }
290
291
    public function getMetadata()
292
    {
293
        return $this->metadata;
294
    }
295
296
    public function getMetadataByKey(string $key)
297
    {
298
        $metadata = $this->getMetadata();
299
300
        if (isset($metadata[$key])) {
301
            return $metadata[$key];
302
        }
303
    }
304
305
    public function setMetadata(array $metadata)
306
    {
307
        $this->metadata = $metadata;
308
    }
309
310
    public function getSubjectType()
311
    {
312
        return 'article';
313
    }
314
315
    public function getLead()
316
    {
317
        return $this->lead;
318
    }
319
320
    public function setLead($lead)
321
    {
322
        $this->lead = $lead;
323
    }
324
325
    public function getFeatureMedia()
326
    {
327
        return $this->featureMedia;
328
    }
329
330
    public function setFeatureMedia(ArticleMediaInterface $featureMedia = null)
331
    {
332
        $this->featureMedia = $featureMedia;
333
    }
334
335
    public function getCode(): string
336
    {
337
        return $this->code;
338
    }
339
340
    public function setCode(string $code)
341
    {
342
        $this->code = $code;
343
    }
344
345
    public function addSourceReference(ArticleSourceReferenceInterface $source)
346
    {
347
        if (!$this->hasSourceReference($source)) {
348
            $this->sources->add($source);
349
        }
350
    }
351
352
    public function removeSourceReference(ArticleSourceReferenceInterface $source)
353
    {
354
        $this->sources->removeElement($source);
355
    }
356
357
    public function hasSourceReference(ArticleSourceReferenceInterface $source): bool
358
    {
359
        return $this->sources->contains($source);
360
    }
361
362
    public function getSources(): Collection
363
    {
364
        if (0 < $this->sources->count()) {
365
            $sources = new ArrayCollection();
366
            /** @var ArticleSourceReferenceInterface $source */
367
            foreach ($this->sources as $source) {
368
                $sources->add($source->getArticleSource());
369
            }
370
371
            return $sources;
372
        }
373
374
        return $this->sources;
375
    }
376
377
    public function getExtra(): ?array
378
    {
379
        return $this->extra;
380
    }
381
382
    public function setExtra(?array $extra): void
383
    {
384
        $this->extra = $extra;
385
    }
386
387
    public function getSlideshows(): Collection
388
    {
389
        return $this->slideshows;
390
    }
391
392
    public function hasSlideshow(SlideshowInterface $slideshow): bool
393
    {
394
        return $this->slideshows->contains($slideshow);
395
    }
396
397
    public function addSlideshow(SlideshowInterface $slideshow): void
398
    {
399
        if (!$this->hasSlideshow($slideshow)) {
400
            $slideshow->setArticle($this);
401
            $this->slideshows->add($slideshow);
402
        }
403
    }
404
405
    public function removeSlideshow(SlideshowInterface $slideshow): void
406
    {
407
        if ($this->hasSlideshow($slideshow)) {
408
            $slideshow->setArticle(null);
409
            $this->slideshows->removeElement($slideshow);
410
        }
411
    }
412
}
413