Completed
Push — 3.x ( 733455...aa9981 )
by Grégoire
02:00
created

Post::isCommentable()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.5555
c 0
b 0
f 0
cc 5
nc 4
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\NewsBundle\Model;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\Common\Collections\Collection;
16
use FOS\UserBundle\Model\UserInterface;
17
use Sonata\ClassificationBundle\Model\CollectionInterface;
18
use Sonata\ClassificationBundle\Model\Tag;
19
use Sonata\ClassificationBundle\Model\TagInterface;
20
use Sonata\MediaBundle\Model\MediaInterface;
21
22
abstract class Post implements PostInterface
23
{
24
    /**
25
     * @var string
26
     */
27
    protected $title;
28
29
    /**
30
     * @var string
31
     */
32
    protected $slug;
33
34
    /**
35
     * @var string
36
     */
37
    protected $abstract;
38
39
    /**
40
     * @var string
41
     */
42
    protected $content;
43
44
    /**
45
     * @var string
46
     */
47
    protected $rawContent;
48
49
    /**
50
     * @var string
51
     */
52
    protected $contentFormatter;
53
54
    /**
55
     * @var Collection|TagInterface[]
56
     */
57
    protected $tags;
58
59
    /**
60
     * @var Collection|CommentInterface[]
61
     */
62
    protected $comments;
63
64
    /**
65
     * @var bool
66
     */
67
    protected $enabled;
68
69
    /**
70
     * @var \DateTime
71
     */
72
    protected $publicationDateStart;
73
74
    /**
75
     * @var \DateTime
76
     */
77
    protected $createdAt;
78
79
    /**
80
     * @var \DateTime
81
     */
82
    protected $updatedAt;
83
84
    /**
85
     * @var bool
86
     */
87
    protected $commentsEnabled = true;
88
89
    /**
90
     * @var \DateTime
91
     */
92
    protected $commentsCloseAt;
93
94
    /**
95
     * @var int
96
     */
97
    protected $commentsDefaultStatus;
98
99
    /**
100
     * @var int
101
     */
102
    protected $commentsCount = 0;
103
104
    /**
105
     * @var UserInterface
106
     */
107
    protected $author;
108
109
    /**
110
     * @var MediaInterface
111
     */
112
    protected $image;
113
114
    /**
115
     * @var Collection|CollectionInterface[]
116
     */
117
    protected $collection;
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function __construct()
123
    {
124
        $this->setPublicationDateStart(new \DateTime());
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function __toString()
131
    {
132
        return $this->getTitle() ?: 'n/a';
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function setTitle($title)
139
    {
140
        $this->title = $title;
141
142
        $this->setSlug(Tag::slugify($title));
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function getTitle()
149
    {
150
        return $this->title;
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function setAbstract($abstract)
157
    {
158
        $this->abstract = $abstract;
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164
    public function getAbstract()
165
    {
166
        return $this->abstract;
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    public function setContent($content)
173
    {
174
        $this->content = $content;
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function getContent()
181
    {
182
        return $this->content;
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188
    public function setEnabled($enabled)
189
    {
190
        $this->enabled = $enabled;
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196
    public function getEnabled()
197
    {
198
        return $this->enabled;
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204
    public function setSlug($slug)
205
    {
206
        $this->slug = $slug;
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212
    public function getSlug()
213
    {
214
        return $this->slug;
215
    }
216
217
    /**
218
     * {@inheritdoc}
219
     */
220
    public function setPublicationDateStart(\DateTime $publicationDateStart = null)
221
    {
222
        $this->publicationDateStart = $publicationDateStart;
223
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228
    public function getPublicationDateStart()
229
    {
230
        return $this->publicationDateStart;
231
    }
232
233
    /**
234
     * {@inheritdoc}
235
     */
236
    public function setCreatedAt(\DateTime $createdAt = null)
237
    {
238
        $this->createdAt = $createdAt;
239
    }
240
241
    /**
242
     * {@inheritdoc}
243
     */
244
    public function getCreatedAt()
245
    {
246
        return $this->createdAt;
247
    }
248
249
    /**
250
     * {@inheritdoc}
251
     */
252
    public function setUpdatedAt(\DateTime $updatedAt = null)
253
    {
254
        $this->updatedAt = $updatedAt;
255
    }
256
257
    /**
258
     * {@inheritdoc}
259
     */
260
    public function getUpdatedAt()
261
    {
262
        return $this->updatedAt;
263
    }
264
265
    /**
266
     * {@inheritdoc}
267
     */
268
    public function addComments(CommentInterface $comment)
269
    {
270
        $this->comments[] = $comment;
271
        $comment->setPost($this);
272
    }
273
274
    /**
275
     * {@inheritdoc}
276
     */
277
    public function setComments($comments)
278
    {
279
        $this->comments = new ArrayCollection();
280
281
        foreach ($this->comments as $comment) {
282
            $this->addComments($comment);
283
        }
284
    }
285
286
    /**
287
     * {@inheritdoc}
288
     */
289
    public function getComments()
290
    {
291
        return $this->comments;
292
    }
293
294
    /**
295
     * {@inheritdoc}
296
     */
297
    public function addTags(TagInterface $tags)
298
    {
299
        $this->tags[] = $tags;
300
    }
301
302
    /**
303
     * {@inheritdoc}
304
     */
305
    public function getTags()
306
    {
307
        return $this->tags;
308
    }
309
310
    /**
311
     * {@inheritdoc}
312
     */
313
    public function setTags($tags)
314
    {
315
        $this->tags = $tags;
316
    }
317
318
    public function prePersist()
319
    {
320
        if (!$this->getPublicationDateStart()) {
321
            $this->setPublicationDateStart(new \DateTime());
322
        }
323
324
        $this->setCreatedAt(new \DateTime());
325
        $this->setUpdatedAt(new \DateTime());
326
    }
327
328
    public function preUpdate()
329
    {
330
        if (!$this->getPublicationDateStart()) {
331
            $this->setPublicationDateStart(new \DateTime());
332
        }
333
334
        $this->setUpdatedAt(new \DateTime());
335
    }
336
337
    /**
338
     * {@inheritdoc}
339
     */
340
    public function getYear()
341
    {
342
        return $this->getPublicationDateStart()->format('Y');
343
    }
344
345
    /**
346
     * {@inheritdoc}
347
     */
348
    public function getMonth()
349
    {
350
        return $this->getPublicationDateStart()->format('m');
351
    }
352
353
    /**
354
     * {@inheritdoc}
355
     */
356
    public function getDay()
357
    {
358
        return $this->getPublicationDateStart()->format('d');
359
    }
360
361
    /**
362
     * {@inheritdoc}
363
     */
364
    public function setCommentsEnabled($commentsEnabled)
365
    {
366
        $this->commentsEnabled = $commentsEnabled;
367
    }
368
369
    /**
370
     * {@inheritdoc}
371
     */
372
    public function getCommentsEnabled()
373
    {
374
        return $this->commentsEnabled;
375
    }
376
377
    /**
378
     * {@inheritdoc}
379
     */
380
    public function setCommentsCloseAt(\DateTime $commentsCloseAt = null)
381
    {
382
        $this->commentsCloseAt = $commentsCloseAt;
383
    }
384
385
    /**
386
     * {@inheritdoc}
387
     */
388
    public function getCommentsCloseAt()
389
    {
390
        return $this->commentsCloseAt;
391
    }
392
393
    /**
394
     * {@inheritdoc}
395
     */
396
    public function setCommentsDefaultStatus($commentsDefaultStatus)
397
    {
398
        $this->commentsDefaultStatus = $commentsDefaultStatus;
399
    }
400
401
    /**
402
     * {@inheritdoc}
403
     */
404
    public function getCommentsDefaultStatus()
405
    {
406
        return $this->commentsDefaultStatus;
407
    }
408
409
    /**
410
     * {@inheritdoc}
411
     */
412
    public function setCommentsCount($commentsCount)
413
    {
414
        $this->commentsCount = $commentsCount;
415
    }
416
417
    /**
418
     * {@inheritdoc}
419
     */
420
    public function getCommentsCount()
421
    {
422
        return $this->commentsCount;
423
    }
424
425
    /**
426
     * {@inheritdoc}
427
     */
428
    public function isCommentable()
429
    {
430
        if (!$this->getCommentsEnabled() || !$this->getEnabled()) {
431
            return false;
432
        }
433
434
        if ($this->getCommentsCloseAt() instanceof \DateTime) {
435
            return 1 == $this->getCommentsCloseAt()->diff(new \DateTime())->invert ? true : false;
436
        }
437
438
        return true;
439
    }
440
441
    /**
442
     * {@inheritdoc}
443
     */
444
    public function isPublic()
445
    {
446
        if (!$this->getEnabled()) {
447
            return false;
448
        }
449
450
        return 0 == $this->getPublicationDateStart()->diff(new \DateTime())->invert ? true : false;
451
    }
452
453
    /**
454
     * {@inheritdoc}
455
     */
456
    public function setAuthor($author)
457
    {
458
        $this->author = $author;
459
    }
460
461
    /**
462
     * {@inheritdoc}
463
     */
464
    public function getAuthor()
465
    {
466
        return $this->author;
467
    }
468
469
    /**
470
     * {@inheritdoc}
471
     */
472
    public function setImage($image)
473
    {
474
        $this->image = $image;
475
    }
476
477
    /**
478
     * {@inheritdoc}
479
     */
480
    public function getImage()
481
    {
482
        return $this->image;
483
    }
484
485
    /**
486
     * {@inheritdoc}
487
     */
488
    public function setCollection(CollectionInterface $collection = null)
489
    {
490
        $this->collection = $collection;
0 ignored issues
show
Documentation Bug introduced by
It seems like $collection can also be of type object<Sonata\Classifica...el\CollectionInterface>. However, the property $collection is declared as type object<Doctrine\Common\C...l\CollectionInterface>>. 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...
491
    }
492
493
    /**
494
     * {@inheritdoc}
495
     */
496
    public function getCollection()
497
    {
498
        return $this->collection;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->collection; (Doctrine\Common\Collecti...l\CollectionInterface[]) is incompatible with the return type declared by the interface Sonata\NewsBundle\Model\...nterface::getCollection of type Sonata\ClassificationBun...ollectionInterface|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
499
    }
500
501
    /**
502
     * @param $contentFormatter
503
     */
504
    public function setContentFormatter($contentFormatter)
505
    {
506
        $this->contentFormatter = $contentFormatter;
507
    }
508
509
    /**
510
     * {@inheritdoc}
511
     */
512
    public function getContentFormatter()
513
    {
514
        return $this->contentFormatter;
515
    }
516
517
    /**
518
     * {@inheritdoc}
519
     */
520
    public function setRawContent($rawContent)
521
    {
522
        $this->rawContent = $rawContent;
523
    }
524
525
    /**
526
     * {@inheritdoc}
527
     */
528
    public function getRawContent()
529
    {
530
        return $this->rawContent;
531
    }
532
}
533