Completed
Push — feature/pixie-port ( b3acf3...e69309 )
by Vladimir
03:27
created

Invitation::getStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * This file contains functionality relating to the invitation of players to join teams
4
 *
5
 * @package    BZiON\Models
6
 * @license    https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3
7
 */
8
9
/**
10
 * An invitation sent to a player asking them to join a team
11
 * @package    BZiON\Models
12
 */
13
class Invitation extends UrlModel
14
{
15
    /**
16
     * The ID of the player receiving the invite
17
     * @var int
18
     */
19
    protected $invited_player;
20
21
    /**
22
     * The ID of the sender of the invite
23
     * @var int
24
     */
25
    protected $sent_by;
26
27
    /**
28
     * The ID of the team a player was invited to
29
     * @var int
30
     */
31
    protected $team_id;
32
33
    /**
34
     * The time the invitation was sent
35
     * @var TimeDate
36
     */
37
    protected $sent;
38
39
    /**
40
     * The time the invitation will expire
41
     * @var TimeDate
42
     */
43
    protected $expiration;
44
45
    /**
46
     * The optional message sent to a player to join a team
47
     * @var string
48
     */
49
    protected $message;
50
51
    /**
52
     * @var int
53
     */
54
    protected $status;
55
56
    /**
57
     * An array of valid statuses an Invitation can be in.
58
     *
59
     * @var int[]
60
     */
61
    protected static $validStatuses = [self::STATUS_PENDING, self::STATUS_ACCEPTED, self::STATUS_DENIED];
62
63
    const DELETED_COLUMN = 'is_deleted';
64
65
    const STATUS_PENDING = 0;
66
    const STATUS_ACCEPTED = 1;
67
    const STATUS_DENIED = 2;
68
69
    const TABLE = 'invitations';
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    protected function assignResult($invitation)
75
    {
76
        $this->invited_player = $invitation['invited_player'];
77
        $this->sent_by    = $invitation['sent_by'];
78
        $this->team_id    = $invitation['team'];
79
        $this->sent       = TimeDate::fromMysql($invitation['sent']);
80
        $this->expiration = TimeDate::fromMysql($invitation['expiration']);
81
        $this->status     = self::castStatus($invitation['status']);
82
        $this->is_deleted = $invitation['is_deleted'];
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    protected function assignLazyResult($invitation)
89
    {
90
        $this->message = $invitation['text'];
91
    }
92
93
    /**
94
     * Get the player receiving the invite
95
     *
96
     * @return Player
97
     */
98
    public function getInvitedPlayer()
99
    {
100
        return Player::get($this->invited_player);
101
    }
102
103
    /**
104
     * Get the sender of the invite
105
     *
106
     * @return Player
107
     */
108
    public function getSentBy()
109
    {
110
        return Player::get($this->sent_by);
111
    }
112
113
    /**
114
     * Get the team a player was invited to
115
     *
116
     * @return Team
117
     */
118
    public function getTeam()
119
    {
120
        return Team::get($this->team_id);
121
    }
122
123
    /**
124
     * Get the timestamp of when the invitation was sent.
125
     *
126
     * @return TimeDate
127
     */
128
    public function getSendTimestamp()
129
    {
130
        return $this->sent;
131
    }
132
133
    /**
134
     * Get the time when the invitation will expire
135
     *
136
     * @return TimeDate
137
     */
138
    public function getExpiration()
139
    {
140
        return $this->expiration->copy();
141
    }
142
143
    /**
144
     * Get the optional message sent to a player to join a team
145
     *
146
     * @return string
147
     */
148
    public function getMessage()
149
    {
150
        $this->lazyLoad();
151
152
        return $this->message;
153
    }
154
155
    /**
156
     * Get the current status of the Invitation.
157
     *
158
     * @see Invitation::STATUS_PENDING
159
     * @see Invitation::STATUS_ACCEPTED
160
     * @see Invitation::STATUS_DENIED
161
     *
162
     * @since 0.11.0
163
     *
164
     * @return int
165
     */
166
    public function getStatus()
167
    {
168
        return $this->status;
169
    }
170
171
    /**
172
     * Whether or not an invitation has expired
173
     *
174
     * @return bool
175
     */
176
    public function isExpired()
177
    {
178
        return $this->expiration->lt(TimeDate::now());
179
    }
180
181
    /**
182
     * Mark the invitation as having expired
183
     *
184
     * @return self
185
     */
186
    public function setExpired()
187
    {
188
        return $this->updateProperty($this->expiration, 'expiration', TimeDate::now());
189
    }
190
191
    /**
192
     * Update the status for this Invitation
193
     *
194
     * @param int $statusValue
195
     *
196
     * @see Invitation::STATUS_PENDING
197
     * @see Invitation::STATUS_ACCEPTED
198
     * @see Invitation::STATUS_DENIED
199
     *
200
     * @since 0.11.0
201
     *
202
     * @throws InvalidArgumentException When an invalid status is given as an argument
203
     *
204
     * @return static
205
     */
206
    public function setStatus($statusValue)
207
    {
208
        if (!in_array($statusValue, self::$validStatuses)) {
209
            throw new InvalidArgumentException('Invalid value was used; see Invitation::$validStatuses for valid values.');
210
        }
211
212
        return $this->updateProperty($this->status, 'status', $statusValue);
213
    }
214
215
    /**
216
     * Send an invitation to join a team
217
     *
218
     * @param  int        $playerID The ID of the player who will receive the invitation
219
     * @param  int        $teamID   The team ID to which a player has been invited to
220
     * @param  int|null   $from     The ID of the player who sent it
221
     * @param  string     $message  (Optional) The message that will be displayed to the person receiving the invitation
222
     * @param  string|TimeDate|null $expiration The expiration time of the invitation (defaults to 1 week from now)
223
     *
224
     * @return Invitation The object of the invitation just sent
225
     */
226
    public static function sendInvite($playerID, $teamID, $from = null, $message = '', $expiration = null)
227
    {
228
        if ($expiration === null) {
229
            $expiration = TimeDate::now()->addWeek();
230
        } else {
231
            $expiration = Timedate::from($expiration);
232
        }
233
234
        $invitation = self::create([
235
            'invited_player' => $playerID,
236
            'sent_by'        => $from,
237
            'team'           => $teamID,
238
            'sent'           => TimeDate::now()->toMysql(),
239
            'expiration'     => $expiration->toMysql(),
240
            'message'        => $message,
241
            'status'         => self::STATUS_PENDING,
242
        ]);
243
244
        return $invitation;
245
    }
246
247
    /**
248
     * {@inheritdoc}
249
     */
250
    public static function getQueryBuilder()
251
    {
252
        return QueryBuilderFlex::createForModel(Invitation::class);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
253
    }
254
255
    /**
256
     * {@inheritdoc}
257
     */
258
    public static function getEagerColumnsList()
259
    {
260
        return [
261
            'id',
262
            'invited_player',
263
            'sent_by',
264
            'team',
265
            'sent',
266
            'expiration',
267
            'status',
268
            'is_deleted',
269
        ];
270
    }
271
272
    /**
273
     * {@inheritdoc}
274
     */
275
    public static function getLazyColumnsList()
276
    {
277
        return [
278
            'message',
279
        ];
280
    }
281
282
    /**
283
     * Find whether there are unexpired invitations for a player and a team
284
     *
285
     * @param  Player|int $player
286
     * @param  Team|int $team
287
     *
288
     * @return int
289
     */
290
    public static function playerHasInvitationToTeam($player, $team)
291
    {
292
        return (bool)self::getQueryBuilder()
293
            ->where('invited_player', '=', $player)
294
            ->where('team', '=', $team)
295
            ->where('expiration', '<', 'UTC_TIMESTAMP()')
296
            ->count()
297
        ;
298
    }
299
300
    /**
301
     * Cast a value to a valid Invitation status.
302
     *
303
     * @param int $status
304
     *
305
     * @see Invitation::STATUS_PENDING
306
     * @see Invitation::STATUS_ACCEPTED
307
     * @see Invitation::STATUS_DENIED
308
     *
309
     * @return int
310
     */
311
    protected static function castStatus($status)
312
    {
313
        if (in_array($status, self::$validStatuses)) {
314
            return $status;
315
        }
316
317
        return self::STATUS_PENDING;
318
    }
319
}
320