Completed
Push — 2.0 ( 6f195e...6903c9 )
by Paweł
11:57
created

Article   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 363
Duplicated Lines 0 %

Coupling/Cohesion

Components 6
Dependencies 13

Importance

Changes 0
Metric Value
wmc 53
lcom 6
cbo 13
dl 0
loc 363
rs 6.96
c 0
b 0
f 0

43 Methods

Rating   Name   Duplication   Size   Complexity  
A setTitle() 0 12 2
A getSlug() 0 4 1
A setSlug() 0 13 2
A getPublishedAt() 0 4 1
A setPublishedAt() 0 4 1
A getStatus() 0 4 1
A setStatus() 0 4 1
A getTemplateName() 0 4 1
A setTemplateName() 0 4 1
A getMetadata() 0 4 1
A getMetadataByKey() 0 8 2
A setMetadata() 0 4 1
A getSubjectType() 0 4 1
A getLead() 0 4 1
A setLead() 0 4 1
A getCode() 0 4 1
A setCode() 0 4 1
A addSourceReference() 0 6 2
A removeSourceReference() 0 4 1
A hasSourceReference() 0 4 1
A getSources() 0 14 3
A getExtra() 0 4 1
A setExtra() 0 4 1
A getSlideshows() 0 4 1
A hasSlideshow() 0 4 1
A addSlideshow() 0 7 2
A removeSlideshow() 0 7 2
A __construct() 0 11 1
A setPublishStartDate() 0 4 1
A getPublishStartDate() 0 4 1
A setPublishEndDate() 0 4 1
A getPublishEndDate() 0 4 1
A isPublishable() 0 4 1
A setPublishable() 0 4 1
A setIsPublishable() 0 4 1
A isPublished() 0 4 1
A setRoute() 0 4 1
A getRoute() 0 4 1
A getId() 0 4 1
A getBody() 0 4 1
A setBody() 0 4 1
A getTitle() 0 4 1
A getPlace() 0 9 3

How to fix   Complexity   

Complex Class

Complex classes like Article often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Article, and based on these observations, apply Extract Interface, too.

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