Completed
Pull Request — master (#433)
by Paul
11:14 queued 04:32
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\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
     * @deprecated
60
     * @var string
61
     *
62
     * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\MediaBundle\Entity\Media")
63
     * @ORM\JoinColumn(name="image_id", referencedColumnName="id", onDelete="CASCADE")
64
     * @VIC\BusinessProperty("imageable")
65
     */
66
    private $image;
0 ignored issues
show
Unused Code introduced by
The property $image 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...
67
68
    /**
69
     * @ORM\Column(name="status", type="string", nullable=false)
70
     */
71
    protected $status;
72
73
    /**
74
     * Categories of the article.
75
     *
76
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="articles")
77
     * @ORM\JoinColumn(onDelete="SET NULL")
78
     * @VIC\BusinessProperty({"textable", "seoable"})
79
     */
80
    private $category;
81
82
    /**
83
     * @var datetime
84
     *
85
     * @ORM\Column(name="publishedAt", type="datetime", nullable=true)
86
     * @VIC\BusinessProperty({"dateable", "textable"})
87
     */
88
    private $publishedAt;
89
90
    /**
91
     * This relation is dynamically added by ArticleSubscriber
92
     * The property is needed here.
93
     *
94
     * @VIC\BusinessProperty({"textable", "seoable"})
95
     */
96
    private $author;
97
98
    /**
99
     * Tags of the article.
100
     *
101
     * @ORM\ManyToMany(targetEntity="Tag", inversedBy="articles")
102
     * @ORM\JoinTable(name="vic_article_tags")
103
     * @Assert\Valid()
104
     */
105
    private $tags;
106
107
    /**
108
     * @ORM\ManyToOne(targetEntity="\Victoire\Bundle\BlogBundle\Entity\Blog", inversedBy="articles", cascade={"persist"})
109
     * @ORM\JoinColumn(name="blog_id", referencedColumnName="id", onDelete="CASCADE")
110
     */
111
    private $blog;
112
113
    /**
114
     * @var BusinessTemplate
115
     * @ORM\ManyToOne(targetEntity="ArticleTemplate")
116
     * @ORM\JoinColumn(name="template_id", referencedColumnName="id", onDelete="SET NULL")
117
     * @Assert\NotNull()
118
     */
119
    private $template;
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
    /**
325
     * Get businessEntity.
326
     *
327
     * @return Article
328
     */
329
    public function getBusinessEntity()
330
    {
331
        return $this;
332
    }
333
334
    /**
335
     * Set template.
336
     *
337
     * @param ArticleTemplate $template
338
     *
339
     * @return Article
340
     */
341
    public function setTemplate(ArticleTemplate $template)
342
    {
343
        $this->template = $template;
344
345
        return $this;
346
    }
347
348
    /**
349
     * Get template.
350
     *
351
     * @return ArticleTemplate
352
     */
353
    public function getTemplate()
354
    {
355
        return $this->template;
356
    }
357
358
    /**
359
     * Set status.
360
     *
361
     * @param status $status
362
     */
363
    public function setStatus($status)
364
    {
365
        if ($status == PageStatus::PUBLISHED && $this->publishedAt === null) {
366
            $this->setPublishedAt(new \DateTime());
367
        }
368
        $this->status = $status;
369
    }
370
371
    /**
372
     * Get status.
373
     *
374
     * @return status
375
     */
376
    public function getStatus()
377
    {
378
        return $this->status;
379
    }
380
381
    /**
382
     * Get categoryTitle.
383
     *
384
     * @return string
385
     */
386
    public function getCategoryTitle()
387
    {
388
        $this->categoryTitle = $this->category ? $this->category->getTitle() : null;
389
390
        return $this->categoryTitle;
391
    }
392
393
    /**
394
     * Get publishedAtString.
395
     *
396
     * @return string
397
     */
398
    public function getPublishedAtString()
399
    {
400
        setlocale(LC_TIME, 'fr_FR');
401
402
        if ($this->publishedAt) {
403
            return strftime('%d %B %Y', $this->publishedAt->getTimestamp());
404
        } else {
405
            return '';
406
        }
407
    }
408
409
    /**
410
     * Get author.
411
     *
412
     * @return string
413
     */
414
    public function getAuthor()
415
    {
416
        return $this->author;
417
    }
418
419
    /**
420
     * Set author.
421
     *
422
     * @param string $author
423
     *
424
     * @return $this
425
     */
426
    public function setAuthor($author)
427
    {
428
        $this->author = $author;
429
430
        return $this;
431
    }
432
433
    public function getAuthorAvatar()
434
    {
435
        $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...
436
437
        return $this->authorAvatar;
438
    }
439
440
    public function getAuthorFullname()
441
    {
442
        $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...
443
444
        return $this->authorFullName;
445
    }
446
447
    public function setLocale($locale)
448
    {
449
        $this->locale = $locale;
450
    }
451
452
    /**
453
     * @return string
454
     */
455
    public function getLocale()
456
    {
457
        return $this->locale;
458
    }
459
460
    public function getName()
461
    {
462
        return PropertyAccess::createPropertyAccessor()->getValue($this->translate(), 'getName');
463
    }
464
465
    public function setName($name, $locale = null)
466
    {
467
        $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...
468
        $this->mergeNewTranslations();
469
    }
470
471
    public function getSlug()
472
    {
473
        return PropertyAccess::createPropertyAccessor()->getValue($this->translate(), 'getSlug');
474
    }
475
476
    public function setSlug($slug, $locale = null)
477
    {
478
        $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...
479
        $this->mergeNewTranslations();
480
    }
481
482
    public function getDescription()
483
    {
484
        return PropertyAccess::createPropertyAccessor()->getValue($this->translate(), 'getDescription');
485
    }
486
487
    public function setDescription($description, $locale = null)
488
    {
489
        $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...
490
        $this->mergeNewTranslations();
491
    }
492
    public function getImage()
493
    {
494
        return PropertyAccess::createPropertyAccessor()->getValue($this->translate(), 'getImage');
495
    }
496
497
    public function setImage($image, $locale = null)
498
    {
499
        $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...
500
        $this->mergeNewTranslations();
501
    }
502
}
503