Completed
Branch master (220ce5)
by De Cramer
16:11
created

ChatNotification   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 239
Duplicated Lines 5.86 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 99.1%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 5
dl 14
loc 239
rs 10
c 0
b 0
f 0
ccs 110
cts 111
cp 0.991

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
B onLocalRecordsLoaded() 0 24 4
A onLocalRecordsFirstRecord() 0 4 1
A onLocalRecordsSameScore() 0 4 1
C onLocalRecordsBetterPosition() 7 43 7
B onLocalRecordsSamePosition() 7 32 4
A sendMessage() 0 8 1
A getSecuredBy() 0 18 4
A messageFirstPlaceNew() 0 12 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace eXpansion\Bundle\LocalRecords\Plugins;
4
5
use eXpansion\Framework\Core\Helpers\ChatNotification as ChatNotificationHelper;
6
use eXpansion\Bundle\LocalRecords\DataProviders\Listener\RecordsDataListener;
7
use eXpansion\Bundle\LocalRecords\Entity\Record;
8
use eXpansion\Framework\Core\Helpers\Time;
9
use eXpansion\Framework\Core\Storage\PlayerStorage;
10
11
12
/**
13
 * Class ChatNotificaiton
14
 *
15
 * @package eXpansion\Bundle\LocalRecords\Plugins;
16
 * @author  oliver de Cramer <[email protected]>
17
 */
18
class ChatNotification implements RecordsDataListener
19
{
20
    /** @var ChatNotificationHelper */
21
    protected $chatNotification;
22
23
    /** @var  PlayerStorage */
24
    protected $playerStorage;
25
26
    /** @var Time */
27
    protected $timeFormater;
28
29
    /** @var string */
30
    protected $translationPrefix;
31
32
    /** @var int */
33
    protected $positionForPublicMessage;
34
35
    /**
36
     * ChatNotification constructor.
37
     *
38
     * @param ChatNotificationHelper $chatNotification
39
     * @param PlayerStorage $playerStorage
40
     * @param Time $timeFormater
41
     * @param string $translationPrefix
42
     * @param int $positionForPublicMessage
43
     */
44 15
    public function __construct(
45
        ChatNotificationHelper $chatNotification,
46
        PlayerStorage $playerStorage,
47
        Time $timeFormater,
48
        $translationPrefix,
49
        $positionForPublicMessage
50
    ) {
51 15
        $this->chatNotification = $chatNotification;
52 15
        $this->playerStorage = $playerStorage;
53 15
        $this->timeFormater = $timeFormater;
54 15
        $this->translationPrefix = $translationPrefix;
55 15
        $this->positionForPublicMessage = $positionForPublicMessage;
56 15
    }
57
58
    /**
59
     * Called when local records are loaded.
60
     *
61
     * @param Record[] $records
62
     */
63 2
    public function onLocalRecordsLoaded($records)
64
    {
65 2
        if (!empty($records)) {
66 1
            $firstRecord = $records[0];
67
68 1
            $this->sendMessage('loaded.top1', null, [
69 1
                '%nickname%' => $firstRecord->getPlayerLogin(), // TODO get player nickname from database.
70 1
                '%score%' => $this->timeFormater->timeToText($firstRecord->getScore(), true),
71 1
            ]);
72
73
74 1
            $onlinePlayers = $this->playerStorage->getOnline();
75 1
            $count = count($records);
76 1
            for ($i = 1; $i < $count; $i++) {
77 1
                if (isset($onlinePlayers[$records[$i]->getPlayerLogin()])) {
78 1
                    $this->sendMessage('loaded.any', $records[$i]->getPlayerLogin(), [
79 1
                        '%nickname%' => $onlinePlayers[$records[$i]->getPlayerLogin()]->getNickName(),
80 1
                        '%score%' => $this->timeFormater->timeToText($records[$i]->getScore(), true),
81 1
                        '%position%' => $i + 1,
82 1
                    ]);
83 1
                }
84 1
            }
85 1
        }
86 2
    }
87
88
    /**
89
     * Called when a player finishes map for the very first time (basically first record).
90
     *
91
     * @param Record     $record
92
     * @param Record[]   $records
93
     * @param int        $position
94
     */
95 1
    public function onLocalRecordsFirstRecord(Record $record, $records, $position)
96
    {
97 1
        $this->messageFirstPlaceNew($record);
98 1
    }
99
100
    /**
101
     * Called when a player finishes map and does same time as before.
102
     *
103
     * @param Record    $record
104
     * @param Record    $oldRecord
105
     * @param Record[]  $records
106
     *
107
     * @return void
108
     */
109 1
    public function onLocalRecordsSameScore(Record $record, Record $oldRecord, $records)
110
    {
111
        // Nothing.
112 1
    }
113
114
    /**
115
     * Called when a player finishes map with better time and has better position.
116
     *
117
     * @param Record    $record
118
     * @param Record    $oldRecord
119
     * @param Record[]  $records
120
     * @param int       $position
121
     * @param int       $oldPosition
122
     *
123
     * @return void
124
     */
125 6
    public function onLocalRecordsBetterPosition(Record $record, Record $oldRecord, $records, $position, $oldPosition)
126
    {
127 6
        if ($position == 1 && $oldPosition == null) {
128 1
            $this->messageFirstPlaceNew($record);
129 1
            return;
130
        }
131
132
        // Check to who to send.
133 5
        $to = null;
134 5
        if ($position > $this->positionForPublicMessage) {
135 1
            $to = $record->getPlayerLogin();
136 1
        }
137
138
        // Check which message to send.
139 5
        $msg = 'better';
140 5
        if ($oldPosition == null) {
141 1
            $msg = 'new';
142 1
        }
143
144
        // Check for top status
145 5 View Code Duplication
        if ($position == 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
146 1
            $msg .= '.top1';
147 1
        } else {
148 4
            if ($position <= 5) {
149 1
                $msg .= '.top5';
150 1
            } else {
151 3
                $msg .= '.any';
152
            }
153
        }
154
155 5
        $securedBy = $this->getSecuredBy($record, $oldRecord);
156 5
        $this->sendMessage(
157 5
            $msg,
158 5
            $to,
159
            [
160 5
                '%nickname%' => $this->playerStorage->getPlayerInfo($record->getPlayerLogin())->getNickName(),
161 5
                '%score%' => $this->timeFormater->timeToText($record->getScore(), true),
162 5
                '%position%' => $position,
163 5
                '%old_position%' => $oldPosition,
164 5
                '%by%' => $securedBy,
165
            ]
166 5
        );
167 5
    }
168
169
    /**
170
     * Called when a player finishes map with better time but keeps same position.
171
     *
172
     * @param Record $record
173
     * @param Record $oldRecord
174
     * @param Record[] $records
175
     * @param          $position
176
     *
177
     * @return void
178
     */
179 6
    public function onLocalRecordsSamePosition(Record $record, Record $oldRecord, $records, $position)
180
    {
181
        // Check to who to send.
182 6
        $to = null;
183 6
        if ($position > $this->positionForPublicMessage) {
184 3
            $to = $record->getPlayerLogin();
185 3
        }
186
187
        // Check which message to send.
188 6
        $msg = 'secures';
189 6 View Code Duplication
        if ($position == 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
190 1
            $msg .= '.top1';
191 1
        } else {
192 5
            if ($position <= 5) {
193 1
                $msg .= '.top5';
194 1
            } else {
195 4
                $msg .= '.any';
196
            }
197
        }
198
199 6
        $securedBy = $this->getSecuredBy($record, $oldRecord);
200 6
        $this->sendMessage(
201 6
            $msg,
202 6
            $to,
203
            [
204 6
                '%nickname%' => $this->playerStorage->getPlayerInfo($record->getPlayerLogin())->getNickName(),
205 6
                '%score%' => $this->timeFormater->timeToText($record->getScore(), true),
206 6
                '%position%' => $position,
207 6
                '%by%' => $securedBy,
208
            ]
209 6
        );
210 6
    }
211
212 11
    protected function getSecuredBy(Record $record, Record $oldRecord)
213
    {
214 11
        if ($oldRecord->getScore()) {
215 11
            $securedBy = $this->timeFormater->timeToText($oldRecord->getScore() - $record->getScore(), true);
216
217 11
            if (substr($securedBy, 0, 4) === "00:0") {
218 1
                $securedBy = substr($securedBy, 4);
219 1
            } else {
220 10
                if (substr($securedBy, 0, 3) === "00:") {
221 1
                    $securedBy = substr($securedBy, 3);
222 1
                }
223
            }
224
225 11
            return '-' . $securedBy;
226
        }
227
228
        return $securedBy = $this->timeFormater->timeToText(0);
0 ignored issues
show
Unused Code introduced by
$securedBy is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
229
    }
230
231 2
    protected function messageFirstPlaceNew(Record $record)
232
    {
233 2
        $this->sendMessage(
234 2
            'new.top1',
235 2
            null,
236
            [
237 2
                '%nickname%' => $this->playerStorage->getPlayerInfo($record->getPlayerLogin())->getNickName(),
238 2
                '%score%' => $this->timeFormater->timeToText($record->getScore(), true),
239 2
                '%position%' => 1,
240
            ]
241 2
        );
242 2
    }
243
244
    /**
245
     * @param string $message
246
     * @param null|integer $recipe
247
     */
248 14
    protected function sendMessage($message, $recipe, $params)
249
    {
250 14
        $this->chatNotification->sendMessage(
251 14
            $this->translationPrefix.'.'.$message,
252 14
            $recipe,
253
            $params
254 14
        );
255 14
    }
256
}
257