Comment::unpublish()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 0
crap 3
1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) MASSIVE ART WebServices 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 Sulu\Component\Persistence\Model\AuditableInterface;
15
use Symfony\Component\Security\Core\User\UserInterface;
16
17
/**
18
 * Minimum implementation for comments.
19
 */
20
class Comment implements CommentInterface, AuditableInterface
21
{
22
    /**
23
     * @var int
24
     */
25
    protected $id;
26
27
    /**
28
     * @var int
29
     */
30
    protected $state = self::STATE_PUBLISHED;
31
32
    /**
33
     * @var string
34
     */
35
    protected $message;
36
37
    /**
38
     * @var ThreadInterface
39
     */
40
    protected $thread;
41
42
    /**
43
     * @var \DateTime
44
     */
45
    protected $created;
46
47
    /**
48
     * @var \DateTime
49
     */
50
    protected $changed;
51
52
    /**
53
     * @var UserInterface
54
     */
55
    protected $changer;
56
57
    /**
58
     * @var UserInterface
59
     */
60
    protected $creator;
61
62
    /**
63
     * @param int $state
64
     * @param ThreadInterface $thread
65
     */
66 10
    public function __construct($state = self::STATE_PUBLISHED, ThreadInterface $thread = null)
67
    {
68 10
        $this->state = $state;
69 10
        $this->thread = $thread;
70
71 10
        if ($this->isPublished()) {
72 8
            $thread->increaseCommentCount();
0 ignored issues
show
Bug introduced by
It seems like $thread is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
73
        }
74 10
    }
75
76
    /**
77
     * Returns id.
78
     *
79
     * @return int
80
     */
81 6
    public function getId()
82
    {
83 6
        return $this->id;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 1
    public function getState()
90
    {
91 1
        return $this->state;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 2
    public function publish()
98
    {
99 2
        if (null !== $this->thread && !$this->isPublished()) {
100 2
            $this->thread->increaseCommentCount();
101
        }
102
103 2
        $this->state = self::STATE_PUBLISHED;
104
105 2
        return $this;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 2
    public function unpublish()
112
    {
113 2
        if (null !== $this->thread && $this->isPublished()) {
114 2
            $this->thread->decreaseCommentCount();
115
        }
116
117 2
        $this->state = self::STATE_UNPUBLISHED;
118
119 2
        return $this;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 15
    public function isPublished()
126
    {
127 15
        return self::STATE_PUBLISHED === $this->state;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 1
    public function getMessage()
134
    {
135 1
        return $this->message;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141 9
    public function setMessage($message)
142
    {
143 9
        $this->message = $message;
144
145 9
        return $this;
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151 5
    public function getThread()
152
    {
153 5
        return $this->thread;
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159 14
    public function setThread(ThreadInterface $thread)
160
    {
161 14
        $this->thread = $thread;
162
163 14
        return $this;
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function getCreated()
170
    {
171
        return $this->created;
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177 9
    public function getChanged()
178
    {
179 9
        return $this->changed;
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185 9
    public function getCreator()
186
    {
187 9
        return $this->creator;
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193 9
    public function getChanger()
194
    {
195 9
        return $this->changer;
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201 9
    public function getCreatorFullName()
202
    {
203 9
        if (!$this->getCreator()) {
204 9
            return '';
205
        }
206
207
        return $this->getCreator()->getFullName();
208
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213 9
    public function getChangerFullName()
214
    {
215 9
        if (!$this->getChanger()) {
216 9
            return '';
217
        }
218
219
        return $this->getChanger()->getFullName();
220
    }
221
}
222