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

Comment::getCreatorFullName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 2
cts 2
cp 1
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
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
19
class Comment implements CommentInterface, AuditableInterface
20
{
21
    use AuditableTrait;
22
23
    /**
24
     * @var int
25
     */
26
    protected $id;
27
28
    /**
29
     * @var int
30
     */
31
    protected $state = self::STATE_PUBLISHED;
32
33
    /**
34
     * @var string
35
     */
36
    protected $message;
37
38
    /**
39
     * @var ThreadInterface|null
40
     */
41
    protected $thread;
42
43
    /**
44
     * @var int
45
     */
46
    protected $lft;
47
48
    /**
49
     * @var int
50
     */
51
    protected $rgt;
52
53
    /**
54
     * @var int
55
     */
56
    protected $depth;
57
58
    /**
59
     * @var CommentInterface|null
60
     */
61
    protected $parent;
62
63
    /**
64
     * @var Collection|CommentInterface[]
65
     */
66 10
    protected $children;
67
68 10
    public function __construct(int $state = self::STATE_PUBLISHED, ThreadInterface $thread = null)
69 10
    {
70
        $this->state = $state;
71 10
        $this->thread = $thread;
72 8
        $this->children = new ArrayCollection();
73
74 10
        if ($this->thread && $this->isPublished()) {
75
            $this->thread->increaseCommentCount();
76
        }
77
    }
78
79
    public function getId(): int
80
    {
81 6
        return $this->id;
82
    }
83 6
84
    public function getState(): int
85
    {
86
        return $this->state;
87
    }
88
89 1
    public function publish(): CommentInterface
90
    {
91 1
        if (null !== $this->thread && !$this->isPublished()) {
92
            $this->thread->increaseCommentCount();
93
        }
94
95
        $this->state = self::STATE_PUBLISHED;
96
97 2
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Sulu\Bundle\CommentBundle\Entity\Comment) is incompatible with the return type declared by the interface Sulu\Bundle\CommentBundl...mmentInterface::publish 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...
98
    }
99 2
100 2
    public function unpublish(): CommentInterface
101
    {
102
        if (null !== $this->thread && $this->isPublished()) {
103 2
            $this->thread->decreaseCommentCount();
104
        }
105 2
106
        $this->state = self::STATE_UNPUBLISHED;
107
108
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Sulu\Bundle\CommentBundle\Entity\Comment) is incompatible with the return type declared by the interface Sulu\Bundle\CommentBundl...entInterface::unpublish 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...
109
    }
110
111 2
    public function isPublished(): bool
112
    {
113 2
        return self::STATE_PUBLISHED === $this->state;
114 2
    }
115
116
    public function getMessage(): string
117 2
    {
118
        return $this->message ?? '';
119 2
    }
120
121
    public function setMessage(string $message): CommentInterface
122
    {
123
        $this->message = $message;
124
125 15
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Sulu\Bundle\CommentBundle\Entity\Comment) is incompatible with the return type declared by the interface Sulu\Bundle\CommentBundl...ntInterface::setMessage 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
    }
127 15
128
    public function getThread(): ThreadInterface
129
    {
130
        if (!$this->thread) {
131
            throw new \RuntimeException('No thread assigned.');
132
        }
133 1
134
        return $this->thread;
135 1
    }
136
137
    public function setThread(ThreadInterface $thread): CommentInterface
138
    {
139
        $this->thread = $thread;
140
141 9
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Sulu\Bundle\CommentBundle\Entity\Comment) is incompatible with the return type declared by the interface Sulu\Bundle\CommentBundl...entInterface::setThread 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...
142
    }
143 9
144
    public function getParent(): ?CommentInterface
145 9
    {
146
        return $this->parent;
147
    }
148
149
    public function setParent(?CommentInterface $parent = null): CommentInterface
150
    {
151 5
        $this->parent = $parent;
152
153 5
        return $this;
154
    }
155
156
    public function getDepth(): int
157
    {
158
        return $this->depth;
159 14
    }
160
161 14
    public function getChildren(): Collection
162
    {
163 14
        return $this->children;
164
    }
165
166
    public function getPublishedChildren(): Collection
167
    {
168
        return $this->children->filter(
169
            function(CommentInterface $comment) {
170
                return $comment->isPublished();
171
            }
172
        );
173
    }
174
175
    public function getCreatorFullName(): string
176
    {
177 9
        $creator = $this->getCreator();
178
        if (!$creator) {
179 9
            return '';
180
        }
181
182
        return $creator->getFullName();
183
    }
184
185 9
    public function getChangerFullName(): string
186
    {
187 9
        $changer = $this->getChanger();
188
        if (!$changer) {
189
            return '';
190
        }
191
192
        return $changer->getFullName();
193 9
    }
194
}
195