Completed
Pull Request — master (#433)
by Paul
06:40
created

Article   B

Complexity

Total Complexity 42

Size/Duplication

Total Lines 471
Duplicated Lines 0 %

Coupling/Cohesion

Components 5
Dependencies 5

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 42
c 5
b 1
f 1
lcom 5
cbo 5
dl 0
loc 471
rs 8.295

36 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 4 1
A __construct() 0 4 1
A getId() 0 4 1
A setId() 0 4 1
A getCategory() 0 4 1
A getPublishedAt() 0 8 3
A setPublishedAt() 0 6 1
A getDeletedAt() 0 4 1
A setDeletedAt() 0 6 1
A getBlog() 0 4 1
A setBlog() 0 4 1
A setTags() 0 6 1
A addTag() 0 6 1
A removeTag() 0 6 1
A getTags() 0 4 1
A getBusinessEntity() 0 4 1
A setTemplate() 0 6 1
A getTemplate() 0 4 1
A setStatus() 0 7 3
A getStatus() 0 4 1
A getCategoryTitle() 0 6 2
A getPublishedAtString() 0 10 2
A getAuthor() 0 4 1
A setAuthor() 0 6 1
A getAuthorAvatar() 0 6 1
A getAuthorFullname() 0 6 1
A setLocale() 0 4 1
A getLocale() 0 4 1
A getName() 0 4 1
A setName() 0 5 1
A getSlug() 0 4 1
A setSlug() 0 5 1
A getDescription() 0 4 1
A setDescription() 0 5 1
A getImage() 0 4 1
A setImage() 0 5 1

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
namespace Victoire\Bundle\BlogBundle\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Gedmo\Mapping\Annotation as Gedmo;
7
use Gedmo\Timestampable\Traits\TimestampableEntity;
8
use Knp\DoctrineBehaviors\Model\Translatable\Translatable;
9
use Symfony\Component\PropertyAccess\PropertyAccess;
10
use Symfony\Component\Validator\Constraints as Assert;
11
use Victoire\Bundle\BusinessEntityBundle\Entity\Traits\BusinessEntityTrait;
12
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessTemplate;
13
use Victoire\Bundle\CoreBundle\Annotations as VIC;
14
use Victoire\Bundle\PageBundle\Entity\PageStatus;
15
16
/**
17
 * @ORM\Entity(repositoryClass="Victoire\Bundle\BlogBundle\Repository\ArticleRepository"))
18
 * @ORM\Table("vic_article")
19
 * @VIC\BusinessEntity({"Date", "Force", "Redactor", "Listing", "BlogArticles", "Title", "CKEditor", "Text", "UnderlineTitle", "Cover", "Image", "Authorship", "ArticleList", "SliderNav"})
20
 * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
21
 */
22
class Article
23
{
24
    use BusinessEntityTrait;
25
    use TimestampableEntity;
26
    use Translatable;
27
28
    /**
29
     * @VIC\BusinessProperty("businessParameter")
30
     * @ORM\Column(name="id", type="integer")
31
     * @ORM\Id
32
     * @ORM\GeneratedValue(strategy="AUTO")
33
     */
34
    private $id;
35
36
    /**
37
     * @deprecated
38
     * Title is inherited from Page, just add the BusinessProperty annotation.
39
     *
40
     * @ORM\Column(name="name", type="string", length=255, nullable=true)
41
     */
42
    private $name;
43
44
    /**
45
     * @deprecated
46
     * @ORM\Column(name="slug", type="string", length=255, nullable=true)
47
     */
48
    private $slug;
0 ignored issues
show
Unused Code introduced by
The property $slug is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
49
50
    /**
51
     * @deprecated
52
     * Description is inherited from Page, just add the BusinessProperty annotation.
53
     *
54
     * @ORM\Column(name="description", type="text", nullable=true)
55
     */
56
    private $description;
0 ignored issues
show
Unused Code introduced by
The property $description is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
57
58
    /**
59
     * @ORM\Column(name="status", type="string", nullable=false)
60
     */
61
    protected $status;
62
63
    /**
64
     * Categories of the article.
65
     *
66
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="articles")
67
     * @ORM\JoinColumn(onDelete="SET NULL")
68
     * @VIC\BusinessProperty({"textable", "seoable"})
69
     */
70
    private $category;
71
72
    /**
73
     * @var datetime
74
     *
75
     * @ORM\Column(name="publishedAt", type="datetime", nullable=true)
76
     * @VIC\BusinessProperty({"dateable", "textable"})
77
     */
78
    private $publishedAt;
79
80
    /**
81
     * This relation is dynamically added by ArticleSubscriber
82
     * The property is needed here.
83
     *
84
     * @VIC\BusinessProperty({"textable", "seoable"})
85
     */
86
    private $author;
87
88
    /**
89
     * Tags of the article.
90
     *
91
     * @ORM\ManyToMany(targetEntity="Tag", inversedBy="articles")
92
     * @ORM\JoinTable(name="vic_article_tags")
93
     * @Assert\Valid()
94
     */
95
    private $tags;
96
97
    /**
98
     * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\BlogBundle\Entity\Blog", inversedBy="articles", cascade={"persist"})
99
     * @ORM\JoinColumn(name="blog_id", referencedColumnName="id", onDelete="CASCADE")
100
     */
101
    private $blog;
102
103
    /**
104
     * @var BusinessTemplate
105
     * @ORM\ManyToOne(targetEntity="ArticleTemplate")
106
     * @ORM\JoinColumn(name="template_id", referencedColumnName="id", onDelete="SET NULL")
107
     * @Assert\NotNull()
108
     */
109
    private $template;
110
111
    /**
112
     * @VIC\BusinessProperty("textable")
113
     */
114
    private $categoryTitle;
115
116
    /**
117
     * @VIC\BusinessProperty("textable")
118
     */
119
    private $publishedAtString;
0 ignored issues
show
Unused Code introduced by
The property $publishedAtString is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
120
121
    /**
122
     * @VIC\BusinessProperty("textable")
123
     */
124
    private $authorAvatar;
125
126
    /**
127
     * @VIC\BusinessProperty("textable")
128
     */
129
    private $authorFullName;
130
131
    /**
132
     * @ORM\Column(name="deletedAt", type="datetime", nullable=true)
133
     */
134
    private $deletedAt;
135
136
    /**
137
     * @Gedmo\Locale
138
     */
139
    protected $locale;
140
141
    /**
142
     * to string method.
143
     *
144
     * @return string
145
     */
146
    public function __toString()
147
    {
148
        return $this->name;
0 ignored issues
show
Deprecated Code introduced by
The property Victoire\Bundle\BlogBundle\Entity\Article::$name has been deprecated with message: Title is inherited from Page, just add the BusinessProperty annotation.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
149
    }
150
151
    /**
152
     * Constructor.
153
     */
154
    public function __construct()
155
    {
156
        $this->status = PageStatus::DRAFT;
157
    }
158
159
    /**
160
     * Get id.
161
     *
162
     * @return int
163
     */
164
    public function getId()
165
    {
166
        return $this->id;
167
    }
168
169
    /**
170
     * Set id.
171
     *
172
     * @param int $id
173
     */
174
    public function setId($id)
175
    {
176
        $this->id = $id;
177
    }
178
179
    /**
180
     * Get category.
181
     *
182
     * @return string
183
     */
184
    public function getCategory()
185
    {
186
        return $this->category;
187
    }
188
189
    /**
190
     * Get the published at property.
191
     *
192
     * @return \DateTime
193
     */
194
    public function getPublishedAt()
195
    {
196
        if ($this->status == PageStatus::PUBLISHED && $this->publishedAt === null) {
197
            $this->setPublishedAt($this->getCreatedAt());
198
        }
199
200
        return $this->publishedAt;
201
    }
202
203
    /**
204
     * Set publishedAt.
205
     *
206
     * @param \DateTime $publishedAt
207
     *
208
     * @return $this
209
     */
210
    public function setPublishedAt($publishedAt)
211
    {
212
        $this->publishedAt = $publishedAt;
0 ignored issues
show
Documentation Bug introduced by
It seems like $publishedAt of type object<DateTime> is incompatible with the declared type object<Victoire\Bundle\B...Bundle\Entity\datetime> of property $publishedAt.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
213
214
        return $this;
215
    }
216
217
    /**
218
     * Get deletedAt.
219
     *
220
     * @return \DateTime
221
     */
222
    public function getDeletedAt()
223
    {
224
        return $this->deletedAt;
225
    }
226
227
    /**
228
     * Set deletedAt.
229
     *
230
     * @param \DateTime $deletedAt
231
     *
232
     * @return $this
233
     */
234
    public function setDeletedAt($deletedAt)
235
    {
236
        $this->deletedAt = $deletedAt;
237
238
        return $this;
239
    }
240
241
    /**
242
     * Get the blog.
243
     *
244
     * @return Blog
245
     */
246
    public function getBlog()
247
    {
248
        return $this->blog;
249
    }
250
251
    /**
252
     * Set the blog.
253
     *
254
     * @param Blog $blog
255
     */
256
    public function setBlog(Blog $blog)
257
    {
258
        $this->blog = $blog;
259
    }
260
261
    /**
262
     * Set tags.
263
     *
264
     * @param string $tags
265
     *
266
     * @return Article
267
     */
268
    public function setTags($tags)
269
    {
270
        $this->tags = $tags;
271
272
        return $this;
273
    }
274
275
    /**
276
     * Add tag.
277
     *
278
     * @param string $tag
279
     *
280
     * @return Article
281
     */
282
    public function addTag($tag)
283
    {
284
        $this->tags[] = $tag;
285
286
        return $this;
287
    }
288
289
    /**
290
     * Remove tag.
291
     *
292
     * @param string $tag
293
     *
294
     * @return Article
295
     */
296
    public function removeTag($tag)
297
    {
298
        $this->tags->removeElement($tag);
0 ignored issues
show
Bug introduced by
The method removeElement cannot be called on $this->tags (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
299
300
        return $this;
301
    }
302
303
    /**
304
     * Get tags.
305
     *
306
     * @return [Tag]
0 ignored issues
show
Documentation introduced by
The doc-type [Tag] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
307
     */
308
    public function getTags()
309
    {
310
        return $this->tags;
311
    }
312
313
314
    /**
315
     * Get businessEntity.
316
     *
317
     * @return Article
318
     */
319
    public function getBusinessEntity()
320
    {
321
        return $this;
322
    }
323
324
    /**
325
     * Set template.
326
     *
327
     * @param ArticleTemplate $template
328
     *
329
     * @return Article
330
     */
331
    public function setTemplate(ArticleTemplate $template)
332
    {
333
        $this->template = $template;
334
335
        return $this;
336
    }
337
338
    /**
339
     * Get template.
340
     *
341
     * @return ArticleTemplate
342
     */
343
    public function getTemplate()
344
    {
345
        return $this->template;
346
    }
347
348
    /**
349
     * Set status.
350
     *
351
     * @param status $status
352
     */
353
    public function setStatus($status)
354
    {
355
        if ($status == PageStatus::PUBLISHED && $this->publishedAt === null) {
356
            $this->setPublishedAt(new \DateTime());
357
        }
358
        $this->status = $status;
359
    }
360
361
    /**
362
     * Get status.
363
     *
364
     * @return status
365
     */
366
    public function getStatus()
367
    {
368
        return $this->status;
369
    }
370
371
    /**
372
     * Get categoryTitle.
373
     *
374
     * @return string
375
     */
376
    public function getCategoryTitle()
377
    {
378
        $this->categoryTitle = $this->category ? $this->category->getTitle() : null;
379
380
        return $this->categoryTitle;
381
    }
382
383
    /**
384
     * Get publishedAtString.
385
     *
386
     * @return string
387
     */
388
    public function getPublishedAtString()
389
    {
390
        setlocale(LC_TIME, 'fr_FR');
391
392
        if ($this->publishedAt) {
393
            return strftime('%d %B %Y', $this->publishedAt->getTimestamp());
394
        } else {
395
            return '';
396
        }
397
    }
398
399
    /**
400
     * Get author.
401
     *
402
     * @return string
403
     */
404
    public function getAuthor()
405
    {
406
        return $this->author;
407
    }
408
409
    /**
410
     * Set author.
411
     *
412
     * @param string $author
413
     *
414
     * @return $this
415
     */
416
    public function setAuthor($author)
417
    {
418
        $this->author = $author;
419
420
        return $this;
421
    }
422
423
    public function getAuthorAvatar()
424
    {
425
        $this->authorAvatar = 'http://www.gravatar.com/avatar/'.md5($this->author->getEmail()).'?s=70';
0 ignored issues
show
Bug introduced by
The method getEmail cannot be called on $this->author (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
426
427
        return $this->authorAvatar;
428
    }
429
430
    public function getAuthorFullname()
431
    {
432
        $this->authorFullName = $this->author->getFullname();
0 ignored issues
show
Bug introduced by
The method getFullname cannot be called on $this->author (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
433
434
        return $this->authorFullName;
435
    }
436
437
    public function setLocale($locale)
438
    {
439
        $this->locale = $locale;
440
    }
441
442
    /**
443
     * @return string
444
     */
445
    public function getLocale()
446
    {
447
        return $this->locale;
448
    }
449
450
    public function getName()
451
    {
452
        return PropertyAccess::createPropertyAccessor()->getValue($this->translate(), 'getName');
453
    }
454
455
    public function setName($name, $locale = null)
456
    {
457
        $this->translate($locale, false)->setName($name);
0 ignored issues
show
Bug introduced by
It seems like setName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
458
        $this->mergeNewTranslations();
459
    }
460
461
    public function getSlug()
462
    {
463
        return PropertyAccess::createPropertyAccessor()->getValue($this->translate(), 'getSlug');
464
    }
465
466
    public function setSlug($slug, $locale = null)
467
    {
468
        $this->translate($locale, false)->setSlug($slug);
0 ignored issues
show
Bug introduced by
It seems like setSlug() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
469
        $this->mergeNewTranslations();
470
    }
471
472
    public function getDescription()
473
    {
474
        return PropertyAccess::createPropertyAccessor()->getValue($this->translate(), 'getDescription');
475
    }
476
477
    public function setDescription($description, $locale = null)
478
    {
479
        $this->translate($locale, false)->setDescription($description);
0 ignored issues
show
Bug introduced by
It seems like setDescription() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
480
        $this->mergeNewTranslations();
481
    }
482
    public function getImage()
483
    {
484
        return PropertyAccess::createPropertyAccessor()->getValue($this->translate(), 'getImage');
485
    }
486
487
    public function setImage($image, $locale = null)
488
    {
489
        $this->translate($locale, false)->setImage($image);
0 ignored issues
show
Bug introduced by
It seems like setImage() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
490
        $this->mergeNewTranslations();
491
    }
492
}
493