Completed
Push — develop ( cc9a49...c1329f )
by Abdelrahman
09:49
created

Session::scopeGuests()   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 Cortex\Fort\Models;
6
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Builder;
9
use Illuminate\Database\Eloquent\Relations\MorphTo;
10
11
/**
12
 * Cortex\Fort\Models\Session.
13
 *
14
 * @property int                                                $id
15
 * @property int                                                $user_id
16
 * @property string                                             $user_type
17
 * @property string                                             $ip_address
18
 * @property string                                             $user_agent
19
 * @property string                                             $payload
20
 * @property \Carbon\Carbon                                     $last_activity
21
 * @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $user
22
 *
23
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Fort\Models\Session guests($minutes = 5)
24
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Fort\Models\Session guestsByHours($hours = 1)
25
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Fort\Models\Session guestsByMinutes($minutes = 5)
26
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Fort\Models\Session guestsBySeconds($seconds = 60)
27
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\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...
28
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\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...
29
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Fort\Models\Session usersByHours($hours = 1)
30
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Fort\Models\Session usersByMinutes($minutes = 5)
31
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Fort\Models\Session usersBySeconds($seconds = 60)
32
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Fort\Models\Session whereId($value)
33
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Fort\Models\Session whereIpAddress($value)
34
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Fort\Models\Session whereLastActivity($value)
35
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Fort\Models\Session wherePayload($value)
36
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Fort\Models\Session whereUserAgent($value)
37
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Fort\Models\Session whereUserId($value)
38
 * @method static \Illuminate\Database\Eloquent\Builder|\Cortex\Fort\Models\Session whereUserType($value)
39
 * @mixin \Eloquent
40
 */
41
class Session extends Model
42
{
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public $incrementing = false;
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    protected $fillable = [
52
        'id',
53
        'user_id',
54
        'user_type',
55
        'ip_address',
56
        'user_agent',
57
        'payload',
58
        'last_activity',
59
    ];
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    protected $casts = [
65
        'user_id' => 'integer',
66
        'user_type' => 'string',
67
        'ip_address' => 'string',
68
        'user_agent' => 'string',
69
        'payload' => 'string',
70
        'last_activity' => 'datetime',
71
    ];
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public $timestamps = false;
77
78
    /**
79
     * Create a new Eloquent model instance.
80
     *
81
     * @param array $attributes
82
     */
83
    public function __construct(array $attributes = [])
84
    {
85
        parent::__construct($attributes);
86
87
        $this->setTable(config('session.table'));
88
    }
89
90
    /**
91
     * Add an "order by" clause to retrieve most recent sessions.
92
     *
93
     * @param \Illuminate\Database\Eloquent\Builder $builder
94
     * @param string                                $column
95
     *
96
     * @return \Illuminate\Database\Eloquent\Builder
97
     */
98
    public function scopeMostRecent(Builder $builder, $column = 'last_activity'): Builder
99
    {
100
        return $builder->latest($column);
101
    }
102
103
    /**
104
     * Add an "order by" clause to retrieve least recent sessions.
105
     *
106
     * @param \Illuminate\Database\Eloquent\Builder $builder
107
     * @param string                                $column
108
     *
109
     * @return \Illuminate\Database\Eloquent\Builder
110
     */
111
    public function scopeLeastRecent(Builder $builder, $column = 'last_activity'): Builder
112
    {
113
        return $builder->oldest($column);
114
    }
115
116
    /**
117
     * Constrain the query to retrieve only sessions of guests who
118
     * have been active within the specified number of seconds.
119
     *
120
     * @param \Illuminate\Database\Eloquent\Builder $builder
121
     * @param int                                   $seconds
122
     *
123
     * @return \Illuminate\Database\Eloquent\Builder
124
     */
125
    public function scopeGuestsBySeconds(Builder $builder, $seconds = 60): Builder
126
    {
127
        return $builder->where('last_activity', '>=', time() - $seconds)->whereNull('user_id');
128
    }
129
130
    /**
131
     * Alias for the `guestsByMinutes` query method.
132
     *
133
     * @param \Illuminate\Database\Eloquent\Builder $builder
134
     * @param int                                   $minutes
135
     *
136
     * @return \Illuminate\Database\Eloquent\Builder
137
     */
138
    public function scopeGuests(Builder $builder, $minutes = 5): Builder
139
    {
140
        return $builder->guestsByMinutes($minutes);
141
    }
142
143
    /**
144
     * Constrain the query to retrieve only sessions of guests who
145
     * have been active within the specified number of minutes.
146
     *
147
     * @param \Illuminate\Database\Eloquent\Builder $builder
148
     * @param int                                   $minutes
149
     *
150
     * @return \Illuminate\Database\Eloquent\Builder
151
     */
152
    public function scopeGuestsByMinutes(Builder $builder, $minutes = 5): Builder
153
    {
154
        return $builder->guestsBySeconds($minutes * 60);
155
    }
156
157
    /**
158
     * Constrain the query to retrieve only sessions of guests who
159
     * have been active within the specified number of hours.
160
     *
161
     * @param \Illuminate\Database\Eloquent\Builder $builder
162
     * @param int                                   $hours
163
     *
164
     * @return \Illuminate\Database\Eloquent\Builder
165
     */
166
    public function scopeGuestsByHours(Builder $builder, $hours = 1): Builder
167
    {
168
        return $builder->guestsByMinutes($hours * 60);
169
    }
170
171
    /**
172
     * Constrain the query to retrieve only sessions of users who
173
     * have been active within the specified number of seconds.
174
     *
175
     * @param \Illuminate\Database\Eloquent\Builder $builder
176
     * @param int                                   $seconds
177
     *
178
     * @return \Illuminate\Database\Eloquent\Builder
179
     */
180
    public function scopeUsersBySeconds(Builder $builder, $seconds = 60): Builder
181
    {
182
        return $builder->with(['user'])->where('last_activity', '>=', now()->subSeconds($seconds))->whereNotNull('user_id');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 124 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...
183
    }
184
185
    /**
186
     * Constrain the query to retrieve only sessions of users who
187
     * have been active within the specified number of minutes.
188
     *
189
     * @param \Illuminate\Database\Eloquent\Builder $builder
190
     * @param int                                   $minutes
191
     *
192
     * @return \Illuminate\Database\Eloquent\Builder
193
     */
194
    public function scopeUsersByMinutes(Builder $builder, $minutes = 5): Builder
195
    {
196
        return $builder->usersBySeconds($minutes * 60);
197
    }
198
199
    /**
200
     * Constrain the query to retrieve only sessions of users who
201
     * have been active within the specified number of hours.
202
     *
203
     * @param \Illuminate\Database\Eloquent\Builder $builder
204
     * @param int                                   $hours
205
     *
206
     * @return \Illuminate\Database\Eloquent\Builder
207
     */
208
    public function scopeUsersByHours(Builder $builder, $hours = 1): Builder
209
    {
210
        return $builder->usersByMinutes($hours * 60);
211
    }
212
213
    /**
214
     * Get the owning user.
215
     *
216
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
217
     */
218
    public function user(): MorphTo
219
    {
220
        return $this->morphTo();
221
    }
222
223
    /**
224
     * Get sessions of the given user.
225
     *
226
     * @param \Illuminate\Database\Eloquent\Builder $builder
227
     * @param \Illuminate\Database\Eloquent\Model   $user
228
     *
229
     * @return \Illuminate\Database\Eloquent\Builder
230
     */
231
    public function scopeOfUser(Builder $builder, Model $user): Builder
232
    {
233
        return $builder->where('user_type', $user->getMorphClass())->where('user_id', $user->getKey());
234
    }
235
}
236