Completed
Pull Request — master (#433)
by Paul
11:06 queued 04:27
created

Article::setCategory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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\MediaBundle\Entity\Media;
15
use Victoire\Bundle\PageBundle\Entity\PageStatus;
16
17
/**
18
 * @ORM\Entity(repositoryClass="Victoire\Bundle\BlogBundle\Repository\ArticleRepository"))
19
 * @ORM\Table("vic_article")
20
 * @VIC\BusinessEntity({"Date", "Force", "Redactor", "Listing", "BlogArticles", "Title", "CKEditor", "Text", "UnderlineTitle", "Cover", "Image", "Authorship", "ArticleList", "SliderNav"})
21
 * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
22
 */
23
class Article
24
{
25
    use BusinessEntityTrait;
26
    use TimestampableEntity;
27
    use Translatable;
28
29
    /**
30
     * @VIC\BusinessProperty("businessParameter")
31
     * @ORM\Column(name="id", type="integer")
32
     * @ORM\Id
33
     * @ORM\GeneratedValue(strategy="AUTO")
34
     */
35
    private $id;
36
37
    /**
38
     * @deprecated
39
     * Title is inherited from Page, just add the BusinessProperty annotation.
40
     *
41
     * @ORM\Column(name="name", type="string", length=255, nullable=true)
42
     */
43
    private $name;
44
45
    /**
46
     * @deprecated
47
     * @ORM\Column(name="slug", type="string", length=255, nullable=true)
48
     */
49
    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...
50
51
    /**
52
     * @deprecated
53
     * Description is inherited from Page, just add the BusinessProperty annotation.
54
     *
55
     * @ORM\Column(name="description", type="text", nullable=true)
56
     */
57
    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...
58
59
    /**
60
     * @ORM\Column(name="status", type="string", nullable=false)
61
     */
62
    protected $status;
63
64
    /**
65
     * Categories of the article.
66
     *
67
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="articles")
68
     * @ORM\JoinColumn(onDelete="SET NULL")
69
     * @VIC\BusinessProperty({"textable", "seoable"})
70
     */
71
    private $category;
72
73
    /**
74
     * @var datetime
75
     *
76
     * @ORM\Column(name="publishedAt", type="datetime", nullable=true)
77
     * @VIC\BusinessProperty({"dateable", "textable"})
78
     */
79
    private $publishedAt;
80
81
    /**
82
     * This relation is dynamically added by ArticleSubscriber
83
     * The property is needed here.
84
     *
85
     * @VIC\BusinessProperty({"textable", "seoable"})
86
     */
87
    private $author;
88
89
    /**
90
     * Tags of the article.
91
     *
92
     * @ORM\ManyToMany(targetEntity="Tag", inversedBy="articles")
93
     * @ORM\JoinTable(name="vic_article_tags")
94
     * @Assert\Valid()
95
     */
96
    private $tags;
97
98
    /**
99
     * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\BlogBundle\Entity\Blog", inversedBy="articles", cascade={"persist"})
100
     * @ORM\JoinColumn(name="blog_id", referencedColumnName="id", onDelete="CASCADE")
101
     */
102
    private $blog;
103
104
    /**
105
     * @var BusinessTemplate
106
     * @ORM\ManyToOne(targetEntity="ArticleTemplate")
107
     * @ORM\JoinColumn(name="template_id", referencedColumnName="id", onDelete="SET NULL")
108
     * @Assert\NotNull()
109
     */
110
    private $template;
111
112
    /**
113
     * @var string
114
     *
115
     * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\MediaBundle\Entity\Media")
116
     * @ORM\JoinColumn(name="image_id", referencedColumnName="id", onDelete="CASCADE")
117
     * @VIC\BusinessProperty("imageable")
118
     */
119
    private $image;
120
121
    /**
122
     * @VIC\BusinessProperty("textable")
123
     */
124
    private $categoryTitle;
125
126
    /**
127
     * @VIC\BusinessProperty("textable")
128
     */
129
    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...
130
131
    /**
132
     * @VIC\BusinessProperty("textable")
133
     */
134
    private $authorAvatar;
135
136
    /**
137
     * @VIC\BusinessProperty("textable")
138
     */
139
    private $authorFullName;
140
141
    /**
142
     * @ORM\Column(name="deletedAt", type="datetime", nullable=true)
143
     */
144
    private $deletedAt;
145
146
    /**
147
     * @Gedmo\Locale
148
     */
149
    protected $locale;
150
151
    /**
152
     * to string method.
153
     *
154
     * @return string
155
     */
156
    public function __toString()
157
    {
158
        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...
159
    }
160
161
    /**
162
     * Constructor.
163
     */
164
    public function __construct()
165
    {
166
        $this->status = PageStatus::DRAFT;
167
    }
168
169
    /**
170
     * Get id.
171
     *
172
     * @return int
173
     */
174
    public function getId()
175
    {
176
        return $this->id;
177
    }
178
179
    /**
180
     * Set id.
181
     *
182
     * @param int $id
183
     */
184
    public function setId($id)
185
    {
186
        $this->id = $id;
187
    }
188
189
    /**
190
     * Get category.
191
     *
192
     * @return string
193
     */
194
    public function getCategory()
195
    {
196
        return $this->category;
197
    }
198
199
    /**
200
     * Get the published at property.
201
     *
202
     * @return \DateTime
203
     */
204
    public function getPublishedAt()
205
    {
206
        if ($this->status == PageStatus::PUBLISHED && $this->publishedAt === null) {
207
            $this->setPublishedAt($this->getCreatedAt());
208
        }
209
210
        return $this->publishedAt;
211
    }
212
213
    /**
214
     * Set publishedAt.
215
     *
216
     * @param \DateTime $publishedAt
217
     *
218
     * @return $this
219
     */
220
    public function setPublishedAt($publishedAt)
221
    {
222
        $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...
223
224
        return $this;
225
    }
226
227
    /**
228
     * Get deletedAt.
229
     *
230
     * @return \DateTime
231
     */
232
    public function getDeletedAt()
233
    {
234
        return $this->deletedAt;
235
    }
236
237
    /**
238
     * Set deletedAt.
239
     *
240
     * @param \DateTime $deletedAt
241
     *
242
     * @return $this
243
     */
244
    public function setDeletedAt($deletedAt)
245
    {
246
        $this->deletedAt = $deletedAt;
247
248
        return $this;
249
    }
250
251
    /**
252
     * Get the blog.
253
     *
254
     * @return Blog
255
     */
256
    public function getBlog()
257
    {
258
        return $this->blog;
259
    }
260
261
    /**
262
     * Set the blog.
263
     *
264
     * @param Blog $blog
265
     */
266
    public function setBlog(Blog $blog)
267
    {
268
        $this->blog = $blog;
269
    }
270
271
    /**
272
     * Set tags.
273
     *
274
     * @param string $tags
275
     *
276
     * @return Article
277
     */
278
    public function setTags($tags)
279
    {
280
        $this->tags = $tags;
281
282
        return $this;
283
    }
284
285
    /**
286
     * Add tag.
287
     *
288
     * @param string $tag
289
     *
290
     * @return Article
291
     */
292
    public function addTag($tag)
293
    {
294
        $this->tags[] = $tag;
295
296
        return $this;
297
    }
298
299
    /**
300
     * Remove tag.
301
     *
302
     * @param string $tag
303
     *
304
     * @return Article
305
     */
306
    public function removeTag($tag)
307
    {
308
        $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...
309
310
        return $this;
311
    }
312
313
    /**
314
     * Get tags.
315
     *
316
     * @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...
317
     */
318
    public function getTags()
319
    {
320
        return $this->tags;
321
    }
322
323
    /**
324
     * Set image.
325
     *
326
     * @param Media $image
327
     *
328
     * @return Article
329
     */
330
    public function setImage(Media $image = null)
331
    {
332
        $this->image = $image;
0 ignored issues
show
Documentation Bug introduced by
It seems like $image can also be of type object<Victoire\Bundle\MediaBundle\Entity\Media>. However, the property $image is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
333
334
        return $this;
335
    }
336
337
    /**
338
     * Get image.
339
     *
340
     * @return string
341
     */
342
    public function getImage()
343
    {
344
        return $this->image;
345
    }
346
347
    /**
348
     * Get businessEntity.
349
     *
350
     * @return Article
351
     */
352
    public function getBusinessEntity()
353
    {
354
        return $this;
355
    }
356
357
    /**
358
     * Set template.
359
     *
360
     * @param ArticleTemplate $template
361
     *
362
     * @return Article
363
     */
364
    public function setTemplate(ArticleTemplate $template)
365
    {
366
        $this->template = $template;
367
368
        return $this;
369
    }
370
371
    /**
372
     * Get template.
373
     *
374
     * @return ArticleTemplate
375
     */
376
    public function getTemplate()
377
    {
378
        return $this->template;
379
    }
380
381
    /**
382
     * Set status.
383
     *
384
     * @param status $status
385
     */
386
    public function setStatus($status)
387
    {
388
        if ($status == PageStatus::PUBLISHED && $this->publishedAt === null) {
389
            $this->setPublishedAt(new \DateTime());
390
        }
391
        $this->status = $status;
392
    }
393
394
    /**
395
     * Get status.
396
     *
397
     * @return status
398
     */
399
    public function getStatus()
400
    {
401
        return $this->status;
402
    }
403
404
405
    /**
406
     * Get categoryTitle.
407
     *
408
     * @return string
409
     */
410
    public function getCategoryTitle()
411
    {
412
        $this->categoryTitle = $this->category ? $this->category->getTitle() : null;
413
414
        return $this->categoryTitle;
415
    }
416
417
    /**
418
     * Get publishedAtString.
419
     *
420
     * @return string
421
     */
422
    public function getPublishedAtString()
423
    {
424
        setlocale(LC_TIME, 'fr_FR');
425
426
        if ($this->publishedAt) {
427
            return strftime('%d %B %Y', $this->publishedAt->getTimestamp());
428
        } else {
429
            return '';
430
        }
431
    }
432
433
    /**
434
     * Get author.
435
     *
436
     * @return string
437
     */
438
    public function getAuthor()
439
    {
440
        return $this->author;
441
    }
442
443
    /**
444
     * Set author.
445
     *
446
     * @param string $author
447
     *
448
     * @return $this
449
     */
450
    public function setAuthor($author)
451
    {
452
        $this->author = $author;
453
454
        return $this;
455
    }
456
457
    public function getAuthorAvatar()
458
    {
459
        $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...
460
461
        return $this->authorAvatar;
462
    }
463
464
    public function getAuthorFullname()
465
    {
466
        $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...
467
468
        return $this->authorFullName;
469
    }
470
471
    public function setLocale($locale)
472
    {
473
        $this->locale = $locale;
474
    }
475
476
    /**
477
     * @return string
478
     */
479
    public function getLocale()
480
    {
481
        return $this->locale;
482
    }
483
484
    public function getName()
485
    {
486
        return PropertyAccess::createPropertyAccessor()->getValue($this->translate(), 'getName');
487
    }
488
489
    public function setName($name, $locale = null)
490
    {
491
        $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...
492
        $this->mergeNewTranslations();
493
    }
494
    
495
    public function getSlug()
496
    {
497
        return PropertyAccess::createPropertyAccessor()->getValue($this->translate(), 'getSlug');
498
    }
499
500
    public function setSlug($slug, $locale = null)
501
    {
502
        $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...
503
        $this->mergeNewTranslations();
504
    }
505
506
    public function getDescription()
507
    {
508
        return PropertyAccess::createPropertyAccessor()->getValue($this->translate(), 'getDescription');
509
    }
510
    public function setDescription($description, $locale = null)
511
    {
512
        $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...
513
        $this->mergeNewTranslations();
514
    }
515
}
516