Completed
Pull Request — master (#32)
by
unknown
29:38
created

Thread::getCreated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) Sulu GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Bundle\CommentBundle\Entity;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\Common\Collections\Collection;
16
use Sulu\Component\Persistence\Model\AuditableInterface;
17
use Sulu\Component\Persistence\Model\AuditableTrait;
18
use Sulu\Component\Security\Authentication\UserInterface;
19
20
class Thread implements ThreadInterface, AuditableInterface
21
{
22
    use AuditableTrait;
23
24
    /**
25
     * @var int
26
     */
27
    protected $id;
28
29
    /**
30
     * @var string
31
     */
32
    protected $type;
33
34
    /**
35
     * @var string
36
     */
37
    protected $entityId;
38
39
    /**
40
     * @var string
41
     */
42
    protected $title;
43
44
    /**
45
     * @var int
46
     */
47
    protected $commentCount = 0;
48
49
    /**
50
     * @var Collection
51
     */
52
    protected $comments;
53
54
    /**
55
     * @var \DateTime
56
     */
57
    protected $created;
58
59
    /**
60
     * @var \DateTime
61
     */
62
    protected $changed;
63
64
    /**
65
     * @var UserInterface
66
     */
67
    protected $changer;
68
69
    /**
70
     * @var UserInterface
71
     */
72
    protected $creator;
73
74
    public function __construct(string $type, string $entityId, Collection $comments = null, int $commentCount = 0)
75
    {
76
        $this->type = $type;
77
        $this->entityId = $entityId;
78
        $this->comments = $comments ?: new ArrayCollection();
79
        $this->commentCount = $commentCount;
80 27
    }
81
82 27
    public function getId(): int
83 27
    {
84 27
        return $this->id;
85 27
    }
86 27
87
    public function getType(): string
88
    {
89
        return $this->type;
90
    }
91 8
92
    public function getEntityId(): string
93 8
    {
94
        return $this->entityId;
95
    }
96
97
    public function getTitle(): string
98
    {
99 10
        return $this->title;
100
    }
101 10
102
    public function setTitle(string $title): ThreadInterface
103
    {
104
        $this->title = $title;
105
106
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Sulu\Bundle\CommentBundle\Entity\Thread) is incompatible with the return type declared by the interface Sulu\Bundle\CommentBundl...readInterface::setTitle of type self.

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...
107 10
    }
108
109 10
    public function getCommentCount(): int
110
    {
111
        return $this->commentCount;
112
    }
113
114
    public function increaseCommentCount(): ThreadInterface
115 3
    {
116
        ++$this->commentCount;
117 3
118
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Sulu\Bundle\CommentBundle\Entity\Thread) is incompatible with the return type declared by the interface Sulu\Bundle\CommentBundl...e::increaseCommentCount of type self.

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...
119
    }
120
121
    public function decreaseCommentCount(): ThreadInterface
122
    {
123 21
        --$this->commentCount;
124
125 21
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Sulu\Bundle\CommentBundle\Entity\Thread) is incompatible with the return type declared by the interface Sulu\Bundle\CommentBundl...e::decreaseCommentCount of type self.

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...
126 21
    }
127
128
    public function setCommentCount(int $commentCount): ThreadInterface
129
    {
130
        $this->commentCount = $commentCount;
131 11
132
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Sulu\Bundle\CommentBundle\Entity\Thread) is incompatible with the return type declared by the interface Sulu\Bundle\CommentBundl...erface::setCommentCount of type self.

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...
133 11
    }
134
135
    public function getComments(): Collection
136
    {
137
        return $this->comments;
138
    }
139 17
140
    public function addComment(CommentInterface $comment): ThreadInterface
141 17
    {
142
        $this->comments->add($comment);
143 17
        $comment->setThread($this);
144
145
        if ($comment->isPublished()) {
146
            $this->increaseCommentCount();
147
        }
148
149 6
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Sulu\Bundle\CommentBundle\Entity\Thread) is incompatible with the return type declared by the interface Sulu\Bundle\CommentBundl...adInterface::addComment of type self.

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...
150
    }
151 6
152
    public function removeComment(CommentInterface $comment): ThreadInterface
153 6
    {
154
        $this->comments->removeElement($comment);
155
156
        if ($comment->isPublished()) {
157
            $this->decreaseCommentCount();
158
        }
159 1
160
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Sulu\Bundle\CommentBundle\Entity\Thread) is incompatible with the return type declared by the interface Sulu\Bundle\CommentBundl...nterface::removeComment of type self.

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...
161 1
    }
162
163 1
    public function getCreatorFullName(): string
164
    {
165
        $creator = $this->getCreator();
166
        if (!$creator) {
167
            return '';
168
        }
169 9
170
        return $creator->getFullName();
171 9
    }
172
173
    public function getChangerFullName(): string
174
    {
175
        $changer = $this->getChanger();
176
        if (!$changer) {
177 16
            return '';
178
        }
179 16
180 16
        return $changer->getFullName();
181
    }
182
}
183