Completed
Push — master ( 425779...b6e961 )
by Abdelrahman
08:30
created

Session::scopeGuestsByMinutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Fort\Models;
6
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Builder;
9
10
/**
11
 * Rinvex\Fort\Models\Session.
12
 *
13
 * @property int                                $id
14
 * @property int|null                           $user_id
15
 * @property string|null                        $ip_address
16
 * @property string|null                        $user_agent
17
 * @property string                             $payload
18
 * @property \Carbon\Carbon                     $last_activity
19
 * @property-read \Rinvex\Fort\Models\User|null $user
20
 *
21
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Session guests($minutes = 5)
22
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Session guestsByHours($hours = 1)
23
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Session guestsByMinutes($minutes = 5)
24
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Session guestsBySeconds($seconds = 60)
25
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Session leastRecent($column = 'last_activity')
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
26
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Session mostRecent($column = 'last_activity')
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
27
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Session orderByUsers($column, $dir = 'ASC')
28
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Session users($minutes = 5)
29
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Session usersByHours($hours = 1)
30
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Session usersByMinutes($minutes = 5)
31
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Session usersBySeconds($seconds = 60)
32
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Session whereId($value)
33
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Session whereIpAddress($value)
34
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Session whereLastActivity($value)
35
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Session wherePayload($value)
36
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Session whereUserAgent($value)
37
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Fort\Models\Session whereUserId($value)
38
 * @mixin \Eloquent
39
 */
40
class Session extends Model
41
{
42
    /**
43
     * {@inheritdoc}
44
     */
45
    protected $fillable = [
46
        'id',
47
        'user_id',
48
        'ip_address',
49
        'user_agent',
50
        'payload',
51
        'last_activity',
52
    ];
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public $timestamps = false;
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    protected $dates = ['last_activity'];
63
64
    /**
65
     * Create a new Eloquent model instance.
66
     *
67
     * @param array $attributes
68
     */
69
    public function __construct(array $attributes = [])
70
    {
71
        parent::__construct($attributes);
72
73
        $this->setTable(config('session.table'));
74
    }
75
76
    /**
77
     * A session always belongs to a user.
78
     *
79
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
80
     */
81
    public function user()
82
    {
83
        $userModel = config('auth.providers.'.config('auth.guards.'.config('auth.defaults.guard').'.provider').'.model');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
84
85
        return $this->belongsTo($userModel, 'user_id', 'id');
86
    }
87
88
    /**
89
     * Add an "order by" clause to retrieve most recent sessions.
90
     *
91
     * @param \Illuminate\Database\Eloquent\Builder $query
92
     * @param string                                $column
93
     *
94
     * @return \Illuminate\Database\Eloquent\Builder
95
     */
96
    public function scopeMostRecent(Builder $query, $column = 'last_activity')
97
    {
98
        return $query->latest($column);
99
    }
100
101
    /**
102
     * Add an "order by" clause to retrieve least recent sessions.
103
     *
104
     * @param \Illuminate\Database\Eloquent\Builder $query
105
     * @param string                                $column
106
     *
107
     * @return \Illuminate\Database\Eloquent\Builder
108
     */
109
    public function scopeLeastRecent(Builder $query, $column = 'last_activity')
110
    {
111
        return $query->oldest($column);
112
    }
113
114
    /**
115
     * Use joins to order by the users' column attributes.
116
     *
117
     * @param \Illuminate\Database\Eloquent\Builder $query
118
     * @param string                                $column
119
     *
120
     * @return \Illuminate\Database\Eloquent\Builder
121
     */
122
    public function scopeOrderByUsers(Builder $query, $column, $dir = 'ASC')
123
    {
124
        $table = $this->getTable();
125
126
        $userModel = config('auth.providers.'.config('auth.guards.'.config('auth.defaults.guard').'.provider').'.model');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
127
        $user = new $userModel();
128
        $userTable = $user->getTable();
129
        $userKey = $user->getKeyName();
130
131
        return $query->join($userTable, "{$table}.user_id", '=', "{$userTable}.{$userKey}")->orderBy("{$userTable}.{$column}", $dir);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 133 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
132
    }
133
134
    /**
135
     * Constrain the query to retrieve only sessions of guests who
136
     * have been active within the specified number of seconds.
137
     *
138
     * @param \Illuminate\Database\Eloquent\Builder $query
139
     * @param int                                   $seconds
140
     *
141
     * @return \Illuminate\Database\Eloquent\Builder
142
     */
143
    public function scopeGuestsBySeconds(Builder $query, $seconds = 60)
144
    {
145
        return $query->where('last_activity', '>=', time() - $seconds)->whereNull('user_id');
146
    }
147
148
    /**
149
     * Alias for the `guestsByMinutes` query method.
150
     *
151
     * @param \Illuminate\Database\Eloquent\Builder $query
152
     * @param int                                   $minutes
153
     *
154
     * @return \Illuminate\Database\Eloquent\Builder
155
     */
156
    public function scopeGuests(Builder $query, $minutes = 5)
157
    {
158
        return $query->guestsByMinutes($minutes);
159
    }
160
161
    /**
162
     * Constrain the query to retrieve only sessions of guests who
163
     * have been active within the specified number of minutes.
164
     *
165
     * @param \Illuminate\Database\Eloquent\Builder $query
166
     * @param int                                   $minutes
167
     *
168
     * @return \Illuminate\Database\Eloquent\Builder
169
     */
170
    public function scopeGuestsByMinutes(Builder $query, $minutes = 5)
171
    {
172
        return $query->guestsBySeconds($minutes * 60);
173
    }
174
175
    /**
176
     * Constrain the query to retrieve only sessions of guests who
177
     * have been active within the specified number of hours.
178
     *
179
     * @param \Illuminate\Database\Eloquent\Builder $query
180
     * @param int                                   $hours
181
     *
182
     * @return \Illuminate\Database\Eloquent\Builder
183
     */
184
    public function scopeGuestsByHours(Builder $query, $hours = 1)
185
    {
186
        return $query->guestsByMinutes($hours * 60);
187
    }
188
189
    /**
190
     * Constrain the query to retrieve only sessions of users who
191
     * have been active within the specified number of seconds.
192
     *
193
     * @param \Illuminate\Database\Eloquent\Builder $query
194
     * @param int                                   $seconds
195
     *
196
     * @return \Illuminate\Database\Eloquent\Builder
197
     */
198
    public function scopeUsersBySeconds(Builder $query, $seconds = 60)
199
    {
200
        return $query->with(['user'])->where('last_activity', '>=', time() - $seconds)->whereNotNull('user_id');
201
    }
202
203
    /**
204
     * Alias for the `usersByMinutes` query method.
205
     *
206
     * @param \Illuminate\Database\Eloquent\Builder $query
207
     * @param int                                   $minutes
208
     *
209
     * @return \Illuminate\Database\Eloquent\Builder
210
     */
211
    public function scopeUsers(Builder $query, $minutes = 5)
212
    {
213
        return $query->usersByMinutes($minutes);
214
    }
215
216
    /**
217
     * Constrain the query to retrieve only sessions of users who
218
     * have been active within the specified number of minutes.
219
     *
220
     * @param \Illuminate\Database\Eloquent\Builder $query
221
     * @param int                                   $minutes
222
     *
223
     * @return \Illuminate\Database\Eloquent\Builder
224
     */
225
    public function scopeUsersByMinutes(Builder $query, $minutes = 5)
226
    {
227
        return $query->usersBySeconds($minutes * 60);
228
    }
229
230
    /**
231
     * Constrain the query to retrieve only sessions of users who
232
     * have been active within the specified number of hours.
233
     *
234
     * @param \Illuminate\Database\Eloquent\Builder $query
235
     * @param int                                   $hours
236
     *
237
     * @return \Illuminate\Database\Eloquent\Builder
238
     */
239
    public function scopeUsersByHours(Builder $query, $hours = 1)
240
    {
241
        return $query->usersByMinutes($hours * 60);
242
    }
243
}
244