Completed
Pull Request — master (#393)
by De Cramer
02:39
created

ChatNotification::sendMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace eXpansion\Bundle\LocalRecords\Plugins;
4
5
use eXpansion\Bundle\LocalRecords\DataProviders\Listener\RecordsDataListener;
6
use eXpansion\Bundle\LocalRecords\Model\Record;
7
use eXpansion\Framework\Config\Model\ConfigInterface;
8
use eXpansion\Framework\Core\Helpers\ChatNotification as ChatNotificationHelper;
9
use eXpansion\Framework\Core\Helpers\Time;
10
use eXpansion\Framework\Core\Helpers\TMString;
11
use eXpansion\Framework\Core\Storage\PlayerStorage;
12
13
14
/**
15
 * Class ChatNotificaiton
16
 *
17
 * @package eXpansion\Bundle\LocalRecords\Plugins;
18
 * @author  oliver de Cramer <[email protected]>
19
 */
20
class ChatNotification implements RecordsDataListener
21
{
22
    /** @var ChatNotificationHelper */
23
    protected $chatNotification;
24
25
    /** @var Time */
26
    protected $timeFormater;
27
28
    /** @var PlayerStorage */
29
    protected $playerStorage;
30
31
    /** @var string */
32
    protected $translationPrefix;
33
34
    /** @var ConfigInterface */
35
    protected $positionForPublicMessage;
36
37
    /**
38
     * ChatNotification constructor.
39
     *
40
     * @param ChatNotificationHelper $chatNotification
41
     * @param Time                   $timeFormater
42
     * @param PlayerStorage          $playerStorage
43
     * @param string                 $translationPrefix
44
     * @param ConfigInterface        $positionForPublicMessage
45
     */
46 15
    public function __construct(
47
        ChatNotificationHelper $chatNotification,
48
        Time $timeFormater,
49
        PlayerStorage $playerStorage,
50
        $translationPrefix,
51
        ConfigInterface $positionForPublicMessage
52
    ) {
53 15
        $this->chatNotification = $chatNotification;
54 15
        $this->timeFormater = $timeFormater;
55 15
        $this->playerStorage = $playerStorage;
56 15
        $this->translationPrefix = $translationPrefix;
57 15
        $this->positionForPublicMessage = $positionForPublicMessage;
58 15
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63 2
    public function onLocalRecordsLoaded($records, BaseRecords $baseRecords)
64
    {
65 2
        if (!empty($records)) {
66 1
            $firstRecord = $records[0];
67
68 1
            $this->sendMessage('loaded.top1', null, [
69 1
                '%nickname%' => TMString::trimStyles($firstRecord->getPlayer()->getNickname()),
70 1
                '%score%' => $this->timeFormater->timeToText($firstRecord->getScore(), true),
71
            ]);
72
73 1
            $online = $this->playerStorage->getOnline();
74 1
            $count = count($records);
75 1
            for ($i = 1; $i < $count; $i++) {
76 1
                if (isset($online[$records[$i]->getPlayer()->getLogin()])) {
77 1
                    $this->sendMessage('loaded.any', $records[$i]->getPlayer()->getLogin(), [
78 1
                        '%nickname%' => TMString::trimStyles($records[$i]->getPlayer()->getNickname()),
79 1
                        '%score%' => $this->timeFormater->timeToText($records[$i]->getScore(), true),
80 1
                        '%position%' => $i + 1,
81
                    ]);
82
                }
83
            }
84
        }
85 2
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90 1
    public function onLocalRecordsFirstRecord(Record $record, $records, $position, BaseRecords $baseRecords)
91
    {
92 1
        $this->messageFirstPlaceNew($record);
93 1
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98 1
    public function onLocalRecordsSameScore(Record $record, Record $oldRecord, $records, BaseRecords $baseRecords)
99
    {
100
        // Nothing.
101 1
    }
102
103
    /**
104
     * @inheritdoc
105
     */
106 6
    public function onLocalRecordsBetterPosition(Record $record, Record $oldRecord, $records, $position, $oldPosition, BaseRecords $baseRecords)
107
    {
108 6
        if ($position == 1 && $oldPosition == null) {
109 1
            $this->messageFirstPlaceNew($record);
110
111 1
            return;
112
        }
113
114
        // Check to who to send.
115 5
        $to = null;
116 5
        if ($position > $this->positionForPublicMessage->get()) {
117 1
            $to = $record->getPlayer()->getLogin();
118
        }
119
120
        // Check which message to send.
121 5
        $msg = 'better';
122 5
        if ($oldPosition == null) {
123 1
            $msg = 'new';
124
        }
125
126
        // Check for top status
127 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...
128 1
            $msg .= '.top1';
129
        } else {
130 4
            if ($position <= 5) {
131 1
                $msg .= '.top5';
132
            } else {
133 3
                $msg .= '.any';
134
            }
135
        }
136
137 5
        $securedBy = $this->getSecuredBy($record, $oldRecord);
138 5
        $this->sendMessage(
139 5
            $msg,
140
            $to,
141
            [
142 5
                '%nickname%' => TMString::trimStyles($record->getPlayer()->getNickName()),
143 5
                '%score%' => $this->timeFormater->timeToText($record->getScore(), true),
144 5
                '%position%' => $position,
145 5
                '%old_position%' => $oldPosition,
146 5
                '%by%' => $securedBy,
147
            ]
148
        );
149 5
    }
150
151
    /**
152
     * @inheritdoc
153
     */
154 6
    public function onLocalRecordsSamePosition(Record $record, Record $oldRecord, $records, $position, BaseRecords $baseRecords)
155
    {
156
        // Check to who to send.
157 6
        $to = null;
158 6
        if ($position > $this->positionForPublicMessage->get()) {
159 3
            $to = $record->getPlayer()->getLogin();
160
        }
161
162
        // Check which message to send.
163 6
        $msg = 'secures';
164 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...
165 1
            $msg .= '.top1';
166
        } else {
167 5
            if ($position <= 5) {
168 1
                $msg .= '.top5';
169
            } else {
170 4
                $msg .= '.any';
171
            }
172
        }
173
174 6
        $securedBy = $this->getSecuredBy($record, $oldRecord);
175 6
        $this->sendMessage(
176 6
            $msg,
177
            $to,
178
            [
179 6
                '%nickname%' => TMString::trimStyles($record->getPlayer()->getNickName()),
180 6
                '%score%' => $this->timeFormater->timeToText($record->getScore(), true),
181 6
                '%position%' => $position,
182 6
                '%by%' => $securedBy,
183
            ]
184
        );
185 6
    }
186
187
    /**
188
     * @param Record $record
189
     * @param Record $oldRecord
190
     * @return string
191
     */
192 11
    protected function getSecuredBy(Record $record, Record $oldRecord)
193
    {
194 11
        if ($oldRecord->getScore()) {
195 11
            $securedBy = $this->timeFormater->timeToText($oldRecord->getScore() - $record->getScore(), true);
196
197 11
            if (substr($securedBy, 0, 4) === "00:0") {
198 1
                $securedBy = substr($securedBy, 4);
199
            } else {
200 10
                if (substr($securedBy, 0, 3) === "00:") {
201 1
                    $securedBy = substr($securedBy, 3);
202
                }
203
            }
204
205 11
            return '-'.$securedBy;
206
        }
207
208
        return $this->timeFormater->timeToText(0);
209
    }
210
211
    /**
212
     * @param Record $record
213
     * @throws \Propel\Runtime\Exception\PropelException
214
     */
215 2
    protected function messageFirstPlaceNew(Record $record)
216
    {
217 2
        $this->sendMessage(
218 2
            'new.top1',
219 2
            null,
220
            [
221 2
                '%nickname%' => TMString::trimStyles($record->getPlayer()->getNickname()),
222 2
                '%score%' => $this->timeFormater->timeToText($record->getScore(), true),
223 2
                '%position%' => 1,
224
            ]
225
        );
226 2
    }
227
228
    /**
229
     * @param string      $message
230
     * @param null|string $recipient
231
     * @param             $params
232
     */
233 14
    protected function sendMessage($message, $recipient, $params)
234
    {
235 14
        $this->chatNotification->sendMessage(
236 14
            $this->translationPrefix.'.'.$message,
237
            $recipient,
238
            $params
239
        );
240 14
    }
241
}
242