Completed
Pull Request — master (#331)
by
unknown
03:33
created

ChatNotification::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 5
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
    public function onLocalRecordsLoaded($records, BaseRecords $baseRecords)
64
    {
65
        if (!empty($records)) {
66 2
            $firstRecord = $records[0];
67
68 2
            $this->sendMessage('loaded.top1', null, [
69 1
                '%nickname%' => TMString::trimStyles($firstRecord->getPlayer()->getNickname()),
70
                '%score%' => $this->timeFormater->timeToText($firstRecord->getScore(), true),
71 1
            ]);
72 1
73 1
            $online = $this->playerStorage->getOnline();
74
            $count = count($records);
75
            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 1
                    ]);
82 1
                }
83 1
            }
84
        }
85
    }
86
87
    /**
88 2
     * @inheritdoc
89
     */
90
    public function onLocalRecordsFirstRecord(Record $record, $records, $position, BaseRecords $baseRecords)
91
    {
92
        $this->messageFirstPlaceNew($record);
93
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98 1
    public function onLocalRecordsSameScore(Record $record, Record $oldRecord, $records, BaseRecords $baseRecords)
99
    {
100 1
        // Nothing.
101 1
    }
102
103
    /**
104
     * @inheritdoc
105
     */
106
    public function onLocalRecordsBetterPosition(Record $record, Record $oldRecord, $records, $position, $oldPosition, BaseRecords $baseRecords)
107
    {
108
        if ($position == 1 && $oldPosition == null) {
109
            $this->messageFirstPlaceNew($record);
110
111
            return;
112 1
        }
113
114
        // Check to who to send.
115 1
        $to = null;
116
        if ($position > $this->positionForPublicMessage->get()) {
117
            $to = $record->getPlayer()->getLogin();
118
        }
119
120
        // Check which message to send.
121
        $msg = 'better';
122
        if ($oldPosition == null) {
123
            $msg = 'new';
124
        }
125
126
        // Check for top status
127 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
            $msg .= '.top1';
129 6
        } else {
130
            if ($position <= 5) {
131 6
                $msg .= '.top5';
132 1
            } else {
133
                $msg .= '.any';
134 1
            }
135
        }
136
137
        $securedBy = $this->getSecuredBy($record, $oldRecord);
138 5
        $this->sendMessage(
139 5
            $msg,
140 1
            $to,
141
            [
142
                '%nickname%' => TMString::trimStyles($record->getPlayer()->getNickName()),
143
                '%score%' => $this->timeFormater->timeToText($record->getScore(), true),
144 5
                '%position%' => $position,
145 5
                '%old_position%' => $oldPosition,
146 1
                '%by%' => $securedBy,
147
            ]
148
        );
149
    }
150 5
151 1
    /**
152
     * @inheritdoc
153 4
     */
154 1
    public function onLocalRecordsSamePosition(Record $record, Record $oldRecord, $records, $position, BaseRecords $baseRecords)
155
    {
156 3
        // Check to who to send.
157
        $to = null;
158
        if ($position > $this->positionForPublicMessage->get()) {
159
            $to = $record->getPlayer()->getLogin();
160 5
        }
161 5
162 5
        // Check which message to send.
163 5
        $msg = 'secures';
164 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 5
            $msg .= '.top1';
166 5
        } else {
167 5
            if ($position <= 5) {
168 5
                $msg .= '.top5';
169 5
            } else {
170
                $msg .= '.any';
171
            }
172 5
        }
173
174
        $securedBy = $this->getSecuredBy($record, $oldRecord);
175
        $this->sendMessage(
176
            $msg,
177
            $to,
178
            [
179
                '%nickname%' => TMString::trimStyles($record->getPlayer()->getNickName()),
180
                '%score%' => $this->timeFormater->timeToText($record->getScore(), true),
181
                '%position%' => $position,
182
                '%by%' => $securedBy,
183
            ]
184
        );
185 6
    }
186
187
    /**
188 6
     * @param Record $record
189 6
     * @param Record $oldRecord
190 3
     * @return string
191
     */
192
    protected function getSecuredBy(Record $record, Record $oldRecord)
193
    {
194 6
        if ($oldRecord->getScore()) {
195 6
            $securedBy = $this->timeFormater->timeToText($oldRecord->getScore() - $record->getScore(), true);
196 1
197
            if (substr($securedBy, 0, 4) === "00:0") {
198 5
                $securedBy = substr($securedBy, 4);
199 1
            } else {
200
                if (substr($securedBy, 0, 3) === "00:") {
201 4
                    $securedBy = substr($securedBy, 3);
202
                }
203
            }
204
205 6
            return '-'.$securedBy;
206 6
        }
207 6
208 6
        return $this->timeFormater->timeToText(0);
209
    }
210 6
211 6
    /**
212 6
     * @param Record $record
213 6
     * @throws \Propel\Runtime\Exception\PropelException
214
     */
215
    protected function messageFirstPlaceNew(Record $record)
216 6
    {
217
        $this->sendMessage(
218
            'new.top1',
219
            null,
220
            [
221
                '%nickname%' => TMString::trimStyles($record->getPlayer()->getNickname()),
222
                '%score%' => $this->timeFormater->timeToText($record->getScore(), true),
223 11
                '%position%' => 1,
224
            ]
225 11
        );
226 11
    }
227
228 11
    /**
229 1
     * @param string      $message
230
     * @param null|string $recipient
231 10
     * @param             $params
232 1
     */
233
    protected function sendMessage($message, $recipient, $params)
234
    {
235
        $this->chatNotification->sendMessage(
236 11
            $this->translationPrefix.'.'.$message,
237
            $recipient,
238
            $params
239
        );
240
    }
241
}
242