User::getPointsCount()   B
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 2
nop 0
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
1
<?php
2
3
namespace PhpEarth\Stats\Model;
4
5
use PhpEarth\Stats\Collection;
6
use PhpEarth\Stats\Collection\CommentCollection;
7
use PhpEarth\Stats\Collection\ReplyCollection;
8
use PhpEarth\Stats\Points;
9
10
/**
11
 * Class User.
12
 */
13
class User
14
{
15
    /**
16
     * @var int User's id
17
     */
18
    private $id;
19
20
    /**
21
     * @var string
22
     */
23
    private $name;
24
25
    /**
26
     * @var Points
27
     */
28
    private $points;
29
30
    /**
31
     * @var int
32
     */
33
    private $pointsCount = 0;
34
35
    /**
36
     * @var Collection
37
     */
38
    private $topics;
39
40
    /**
41
     * @var Collection
42
     */
43
    private $comments;
44
45
    /**
46
     * @var Collection
47
     */
48
    private $replies;
49
50
    /**
51
     * @var CommentCollection
52
     */
53
    private $feedComments;
54
55
    /**
56
     * @var ReplyCollection
57
     */
58
    private $feedReplies;
59
60
    /**
61
     * User constructor.
62
     *
63
     * @param Points $points
64
     */
65
    public function __construct(Points $points)
66
    {
67
        $this->points = $points;
68
        $this->topics = new Collection();
69
        $this->comments = new CommentCollection();
70
        $this->replies = new Collection();
71
    }
72
73
    /**
74
     * Set entire comments collection.
75
     *
76
     * @param CommentCollection $feedComments
77
     */
78
    public function setFeedComments(CommentCollection $feedComments)
79
    {
80
        $this->feedComments = $feedComments;
81
    }
82
83
    /**
84
     * Set entire replies collection.
85
     *
86
     * @param ReplyCollection $feedReplies
87
     */
88
    public function setFeedReplies(ReplyCollection $feedReplies)
89
    {
90
        $this->feedReplies = $feedReplies;
91
    }
92
93
    /**
94
     * Set user's id.
95
     *
96
     * @param int $id
97
     */
98
    public function setId($id)
99
    {
100
        $this->id = $id;
101
    }
102
103
    /**
104
     * Get user's id.
105
     *
106
     * @return int|null
107
     */
108
    public function getId()
109
    {
110
        return $this->id;
111
    }
112
113
    /**
114
     * Set user's name.
115
     *
116
     * @param string $name
117
     */
118
    public function setName($name)
119
    {
120
        $this->name = $name;
121
    }
122
123
    /**
124
     * Get user's name.
125
     *
126
     * @return string
127
     */
128
    public function getName()
129
    {
130
        return $this->name;
131
    }
132
133
    /**
134
     * Increase number of topics and add points for creating a topic.
135
     *
136
     * @param Topic $topic User's topic
137
     */
138
    public function addTopic(Topic $topic)
139
    {
140
        $this->topics->add($topic, $topic->getId());
141
    }
142
143
    /**
144
     * Increase number of comments and add points.
145
     *
146
     * @param Comment $comment
147
     */
148
    public function addComment(Comment $comment)
149
    {
150
        $this->comments->add($comment, $comment->getId());
151
    }
152
153
    /**
154
     * Increase number of comments and add points.
155
     *
156
     * @param Reply $reply
157
     */
158
    public function addReply(Reply $reply)
159
    {
160
        $this->replies->add($reply, $reply->getId());
161
    }
162
163
    /**
164
     * Get number of user's points.
165
     *
166
     * @return int
167
     */
168
    public function getPointsCount()
169
    {
170
        if ($this->pointsCount === 0) {
171
            $pointsCount = 0;
172
173
            foreach ($this->topics as $topic) {
174
                $pointsCount += $this->points->addPointsForTopic($topic);
175
            }
176
177
            $mergedComments = $this->feedComments->getMergedCommentsByUserId($this->getId());
178
179
            foreach ($mergedComments as $comment) {
180
                $pointsCount += $this->points->addPointsForComment($comment);
181
            }
182
183
            $mergedReplies = $this->feedReplies->getMergedRepliesByUserId($this->getId());
184
            foreach ($mergedReplies as $reply) {
185
                $pointsCount += $this->points->addPointsForReply($reply);
186
            }
187
188
            $this->pointsCount = $pointsCount;
189
        }
190
191
        return $this->pointsCount;
192
    }
193
194
    /**
195
     * Get number of user's topics.
196
     *
197
     * @return int
198
     */
199
    public function getTopicsCount()
200
    {
201
        return $this->topics->count();
202
    }
203
204
    /**
205
     * Get number of user's comments and replies.
206
     *
207
     * @return int
208
     */
209
    public function getCommentsCount()
210
    {
211
        return $this->comments->count() + $this->replies->count();
212
    }
213
}
214