Points::addPointsForLinks()   B
last analyzed

Complexity

Conditions 8
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 8
nc 2
nop 1
dl 0
loc 15
rs 7.7777
c 0
b 0
f 0
1
<?php
2
3
namespace PhpEarth\Stats;
4
5
use PhpEarth\Stats\Model\Topic;
6
use PhpEarth\Stats\Model\Comment;
7
use PhpEarth\Stats\Model\Reply;
8
use PhpEarth\Stats\Util\CodeDetector;
9
10
/**
11
 * Class Points.
12
 */
13
class Points
14
{
15
    /**
16
     * @var ExpressionLanguage
17
     */
18
    private $language;
19
20
    /**
21
     * @var CodeDetector
22
     */
23
    private $codeDetector;
24
25
    /**
26
     * @var array
27
     */
28
    private $points;
29
30
    /**
31
     * @var array
32
     */
33
    private $admins;
34
35
    /**
36
     * @var array
37
     */
38
    private $offensiveWords;
39
40
    /**
41
     * Points constructor.
42
     */
43
    public function __construct()
44
    {
45
        $this->language = new ExpressionLanguage();
46
        $this->codeDetector = new CodeDetector();
47
    }
48
49
    /**
50
     * Set points configuration values.
51
     *
52
     * @param array $points
53
     */
54
    public function setPoints($points)
55
    {
56
        $this->points = $points;
57
        $this->codeDetector->setPatterns($points['code_regex']);
58
        $this->codeDetector->setMinCodeLines($points['min_code_lines']);
59
    }
60
61
    /**
62
     * Set admins from configuration values.
63
     *
64
     * @param array $admins
65
     */
66
    public function setAdmins($admins)
67
    {
68
        $this->admins = $admins;
69
    }
70
71
    /**
72
     * Set offensive words.
73
     *
74
     * @param array $offensiveWords
75
     */
76
    public function setOffensiveWords($offensiveWords)
77
    {
78
        $this->offensiveWords = $offensiveWords;
79
    }
80
81
    /**
82
     * Add points based on creating a topic.
83
     *
84
     * @param Topic $topic
85
     *
86
     * @return int
87
     */
88
    public function addPointsForTopic(Topic $topic)
89
    {
90
        // Add points for creating a topic
91
        $points = $this->points['topic'];
92
93
        // Add points based on the topic reactions
94
        $points += $this->language->evaluate($this->points['topic_reactions'], ['topic' => $topic]);
95
96
        // Add points for using recommended links
97
        $points += $this->addPointsForLinks($topic->getMessage());
98
99
        // Set points for only photo shares
100
        $points = $this->language->evaluate($this->points['image'], ['topic' => $topic, 'points' => $points]);
101
102
        // Set points if topic is closed for comments
103
        $points = $this->language->evaluate($this->points['closed_topic'], ['topic' => $topic, 'points' => $points]);
104
105
        // Set points if message contains code
106
        $points = $this->language->evaluate($this->points['code'], ['codeDetector' => $this->codeDetector, 'message' => $topic->getMessage(), 'points' => $points]);
107
108
        // Remove points for using offensive words
109
        $points += $this->getOffensivePoints($topic->getMessage());
110
111
        // Add points for special admin topics
112
        $points += $this->language->evaluate($this->points['admin_topic'], ['topic' => $topic, 'admins' => $this->admins]);
113
114
        return $points;
115
    }
116
117
    /**
118
     * Add points based on comment.
119
     *
120
     * @param Comment $comment
121
     *
122
     * @return int
123
     */
124 View Code Duplication
    public function addPointsForComment(Comment $comment)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
    {
126
        $points = $this->points['comment'];
127
        $points += $this->language->evaluate($this->points['comment_reactions'], ['comment' => $comment]);
128
        $points += $this->getPointsForMessageLength($comment->getMessage());
129
130
        // Add points for using recommended links
131
        $points += $this->addPointsForLinks($comment->getMessage());
132
133
        // Set points if message contains code
134
        $points = $this->language->evaluate($this->points['code'], ['codeDetector' => $this->codeDetector, 'message' => $comment->getMessage(), 'points' => $points]);
135
136
        // Remove points for using offensive words
137
        $points += $this->getOffensivePoints($comment->getMessage());
138
139
        return $points;
140
    }
141
142
    /**
143
     * Add points based on reply.
144
     *
145
     * @param Reply $reply
146
     *
147
     * @return int
148
     */
149 View Code Duplication
    public function addPointsForReply(Reply $reply)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
    {
151
        $points = $this->points['reply'];
152
        $points += $this->language->evaluate($this->points['comment_reactions'], ['comment' => $reply]);
153
        $points += $this->getPointsForMessageLength($reply->getMessage());
154
155
        // Add points for using recommended links
156
        $points += $this->addPointsForLinks($reply->getMessage());
157
158
        // Set points if message contains code
159
        $points = $this->language->evaluate($this->points['code'], ['codeDetector' => $this->codeDetector, 'message' => $reply->getMessage(), 'points' => $points]);
160
161
        // Remove points for using offensive words
162
        $points += $this->getOffensivePoints($reply->getMessage());
163
164
        return $points;
165
    }
166
167
    /**
168
     * Add points for attaching recommended links.
169
     *
170
     * @param string $message
171
     *
172
     * @return int
173
     */
174
    private function addPointsForLinks($message)
175
    {
176
        $positivePoints = 0;
177
        $negativePoints = 0;
178
179
        if (isset($message)) {
180
            foreach ($this->points['urls'] as $url) {
181
                if (false !== stripos($message, $url[0])) {
182
                    $positivePoints = ($url[1] > 0 && $positivePoints < $url[1]) ? $positivePoints : $url[1];
183
                    $negativePoints = ($url[1] <= 0 && $negativePoints > $url[1]) ? $negativePoints : $url[1];
184
                }
185
            }
186
        }
187
188
        return $positivePoints + $negativePoints;
189
    }
190
191
    /**
192
     * Remove points for using offensive and inappropriate keywords.
193
     *
194
     * @param string $message
195
     *
196
     * @return int
197
     */
198
    private function getOffensivePoints($message)
199
    {
200
        $points = 0;
201
        if (isset($message)) {
202
            foreach ($this->offensiveWords as $keyword) {
203
                if (false !== stripos($message, str_rot13($keyword[0]))) {
204
                    $points = ($points < $keyword[1]) ? $points : $keyword[1];
205
                }
206
            }
207
        }
208
209
        return $points;
210
    }
211
212
    /**
213
     * Get points based on message length
214
     *
215
     * @param $message
216
     *
217
     * @return int
218
     */
219
    private function getPointsForMessageLength($message)
220
    {
221
        return $this->language->evaluate($this->points['message_length'], ['message' => $message]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->language->...'message' => $message)) returns the type string which is incompatible with the documented return type integer.
Loading history...
222
    }
223
}
224