Completed
Push — master ( 3b350f...052401 )
by De Cramer
17s
created

ChatNotification   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 223
Duplicated Lines 6.28 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 5
dl 14
loc 223
ccs 85
cts 90
cp 0.9444
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A onLocalRecordsFirstRecord() 0 4 1
A onLocalRecordsSameScore() 0 4 1
B onLocalRecordsLoaded() 0 23 4
C onLocalRecordsBetterPosition() 7 40 7
B onLocalRecordsSamePosition() 7 30 4
A getSecuredBy() 0 14 3
A messageFirstPlaceNew() 0 12 1
A sendMessage() 0 8 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->milisecondsToTrackmania($firstRecord->getScore(), true)
71
            ]);
72
73
74 1
            $onlinePlayers = $this->playerStorage->getOnline();
75 1
            for ($i = 1; $i < count($records); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
76 1
                if (isset($onlinePlayers[$records[$i]->getPlayerLogin()])) {
77 1
                    $this->sendMessage('loaded.any', $records[$i]->getPlayerLogin(), [
78 1
                        '%nickname%' => $onlinePlayers[$records[$i]->getPlayerLogin()]->getNickName(),
79 1
                        '%score%' => $this->timeFormater->milisecondsToTrackmania($records[$i]->getScore(), true),
80 1
                        '%position%' => $i+1,
81
                    ]);
82
                }
83
            }
84
        }
85 2
    }
86
87
    /**
88
     * Called when a player finishes map for the very first time (basically first record).
89
     *
90
     * @param Record   $record
91
     * @param Record[] $records
92
     * @param          $position
93
     */
94 1
    public function onLocalRecordsFirstRecord(Record $record, $records, $position)
95
    {
96 1
        $this->messageFirstPlaceNew($record);
97 1
    }
98
99
    /**
100
     * Called when a player finishes map and does same time as before.
101
     *
102
     * @param Record   $record
103
     * @param Record   $oldRecord
104
     * @param Record[] $records
105
     *
106
     * @return mixed
107
     */
108 1
    public function onLocalRecordsSameScore(Record $record, Record $oldRecord, $records)
109
    {
110
        // Nothing.
111 1
    }
112
113
    /**
114
     * Called when a player finishes map with better time and has better position.
115
     *
116
     * @param Record   $record
117
     * @param Record   $oldRecord
118
     * @param Record[] $records
119
     * @param int      $position
120
     * @param int      $oldPosition
121
     */
122 6
    public function onLocalRecordsBetterPosition(Record $record, Record $oldRecord, $records, $position, $oldPosition)
123
    {
124 6
        if ($position == 1 && $oldPosition == null) {
125 1
            return $this->messageFirstPlaceNew($record);
126
        }
127
128
        // Check to who to send.
129 5
        $to = null;
130 5
        if ($position > $this->positionForPublicMessage) {
131 1
            $to = $record->getPlayerLogin();
132
        }
133
134
        // Check which message to send.
135 5
        $msg = 'better';
136 5
        if ($oldPosition == null) {
137 1
            $msg = 'new';
138
        }
139
140
        // Check for top status
141 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...
142 1
            $msg .= '.top1';
143 4
        } else if ($position <= 5) {
144 1
            $msg .= '.top5';
145
        } else {
146 3
            $msg .= '.any';
147
        }
148
149 5
        $securedBy = $this->getSecuredBy($record, $oldRecord);
150 5
        $this->sendMessage(
151
            $msg,
152
            $to,
153
            [
154 5
                '%nickname%' => $this->playerStorage->getPlayerInfo($record->getPlayerLogin())->getNickName(),
155 5
                '%score%' => $this->timeFormater->milisecondsToTrackmania($record->getScore(), true),
156 5
                '%position%' => $position,
157 5
                '%old_position%' => $oldPosition,
158 5
                '%by%' => $securedBy
159
            ]
160
        );
161 5
    }
162
163
    /**
164
     * Called when a player finishes map with better time but keeps same position.
165
     *
166
     * @param Record   $record
167
     * @param Record   $oldRecord
168
     * @param Record[] $records
169
     * @param          $position
170
     *
171
     * @return mixed
172
     */
173 6
    public function onLocalRecordsSamePosition(Record $record, Record $oldRecord, $records, $position)
174
    {
175
        // Check to who to send.
176 6
        $to = null;
177 6
        if ($position > $this->positionForPublicMessage) {
178 3
            $to = $record->getPlayerLogin();
179
        }
180
181
        // Check which message to send.
182 6
        $msg = 'secures';
183 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...
184 1
            $msg .= '.top1';
185 5
        } else if ($position <= 5) {
186 1
            $msg .= '.top5';
187
        } else {
188 4
            $msg .= '.any';
189
        }
190
191 6
        $securedBy = $this->getSecuredBy($record, $oldRecord);
192 6
        $this->sendMessage(
193
            $msg,
194
            $to,
195
            [
196 6
                '%nickname%' => $this->playerStorage->getPlayerInfo($record->getPlayerLogin())->getNickName(),
197 6
                '%score%' => $this->timeFormater->milisecondsToTrackmania($record->getScore(), true),
198 6
                '%position%' => $position,
199 6
                '%by%' => $securedBy
200
            ]
201
        );
202 6
    }
203
204 11
    protected function getSecuredBy(Record $record, Record $oldRecord)
205
    {
206 11
        $securedBy =$this->timeFormater->milisecondsToTrackmania($oldRecord->getScore() - $record->getScore(), true);
207
208 11
        if (substr($securedBy, 0, 4) === "00:0") {
209 1
            $securedBy = substr($securedBy, 4);
210
        } else {
211 10
            if (substr($securedBy, 0, 3) === "00:") {
212 1
                $securedBy = substr($securedBy, 3);
213
            }
214
        }
215
216 11
        return '-' . $securedBy;
217
    }
218
219 2
    protected function messageFirstPlaceNew(Record $record)
220
    {
221 2
        $this->sendMessage(
222 2
            'new.top1',
223 2
            null,
224
            [
225 2
                '%nickname%' => $this->playerStorage->getPlayerInfo($record->getPlayerLogin())->getNickName(),
226 2
                '%score%' => $this->timeFormater->milisecondsToTrackmania($record->getScore(), true),
227 2
                '%position%' => 1
228
            ]
229
        );
230 2
    }
231
232 14
    protected function sendMessage($message, $recipe, $params)
233
    {
234 14
        $this->chatNotification->sendMessage(
235 14
            $this->translationPrefix . '.' . $message,
236
            $recipe,
237 14
            $params
238
        );
239 14
    }
240
}
241