Completed
Push — master ( abe753...7ead0d )
by Abdelrahman
02:50 queued 01:29
created

Request::scopeOfUser()   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 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Statistics\Models;
6
7
use Illuminate\Database\Eloquent\Model;
8
use Rinvex\Cacheable\CacheableEloquent;
9
use Illuminate\Database\Eloquent\Builder;
10
use Rinvex\Support\Traits\ValidatingTrait;
11
use Illuminate\Database\Eloquent\Relations\MorphTo;
12
use Illuminate\Database\Eloquent\Relations\BelongsTo;
13
14
class Request extends Model
15
{
16
    use ValidatingTrait;
17
    use CacheableEloquent;
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    protected $fillable = [
23
        'route_id',
24
        'agent_id',
25
        'device_id',
26
        'platform_id',
27
        'path_id',
28
        'geoip_id',
29
        'user_id',
30
        'user_type',
31
        'session_id',
32
        'status_code',
33
        'protocol_version',
34
        'referer',
35
        'language',
36
        'is_no_cache',
37
        'wants_json',
38
        'is_secure',
39
        'is_json',
40
        'is_ajax',
41
        'is_pjax',
42
        'created_at',
43
    ];
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected $casts = [
49
        'route_id' => 'integer',
50
        'agent_id' => 'integer',
51
        'device_id' => 'integer',
52
        'platform_id' => 'integer',
53
        'path_id' => 'integer',
54
        'geoip_id' => 'integer',
55
        'user_id' => 'integer',
56
        'user_type' => 'string',
57
        'session_id' => 'string',
58
        'status_code' => 'integer',
59
        'protocol_version' => 'string',
60
        'referer' => 'string',
61
        'language' => 'string',
62
        'is_no_cache' => 'boolean',
63
        'wants_json' => 'boolean',
64
        'is_secure' => 'boolean',
65
        'is_json' => 'boolean',
66
        'is_ajax' => 'boolean',
67
        'is_pjax' => 'boolean',
68
        'created_at' => 'datetime',
69
    ];
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public $timestamps = false;
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    protected $observables = [
80
        'validating',
81
        'validated',
82
    ];
83
84
    /**
85
     * The default rules that the model will validate against.
86
     *
87
     * @var array
88
     */
89
    protected $rules = [
90
        'route_id' => 'required|integer',
91
        'agent_id' => 'required|integer',
92
        'device_id' => 'required|integer',
93
        'platform_id' => 'required|integer',
94
        'path_id' => 'required|integer',
95
        'geoip_id' => 'required|integer',
96
        'user_id' => 'nullable|integer',
97
        'user_type' => 'nullable|string',
98
        'session_id' => 'required|string',
99
        'status_code' => 'required|integer',
100
        'protocol_version' => 'nullable|string',
101
        'referer' => 'nullable|string',
102
        'language' => 'required|string',
103
        'is_no_cache' => 'sometimes|boolean',
104
        'wants_json' => 'sometimes|boolean',
105
        'is_secure' => 'sometimes|boolean',
106
        'is_json' => 'sometimes|boolean',
107
        'is_ajax' => 'sometimes|boolean',
108
        'is_pjax' => 'sometimes|boolean',
109
        'created_at' => 'required|date',
110
    ];
111
112
    /**
113
     * Whether the model should throw a
114
     * ValidationException if it fails validation.
115
     *
116
     * @var bool
117
     */
118
    protected $throwValidationExceptions = true;
119
120
    /**
121
     * Create a new Eloquent model instance.
122
     *
123
     * @param array $attributes
124
     */
125
    public function __construct(array $attributes = [])
126
    {
127
        parent::__construct($attributes);
128
129
        $this->setTable(config('rinvex.statistics.tables.requests'));
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    protected static function boot()
136
    {
137
        parent::boot();
138
139
        static::saved(function (self $request) {
140
            $request->path()->increment('count');
141
            $request->route()->increment('count');
142
            $request->geoip()->increment('count');
143
            $request->agent()->increment('count');
144
            $request->device()->increment('count');
145
            $request->platform()->increment('count');
146
        });
147
    }
148
149
    /**
150
     * The request always belongs to a route.
151
     *
152
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
153
     */
154
    public function route(): BelongsTo
155
    {
156
        return $this->belongsTo(config('rinvex.statistics.models.route'), 'route_id', 'id');
157
    }
158
159
    /**
160
     * The request always belongs to a path.
161
     *
162
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
163
     */
164
    public function path(): BelongsTo
165
    {
166
        return $this->belongsTo(config('rinvex.statistics.models.path'), 'path_id', 'id');
167
    }
168
169
    /**
170
     * The request always belongs to an agent.
171
     *
172
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
173
     */
174
    public function agent(): BelongsTo
175
    {
176
        return $this->belongsTo(config('rinvex.statistics.models.agent'), 'agent_id', 'id');
177
    }
178
179
    /**
180
     * The request always belongs to an geoip.
181
     *
182
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
183
     */
184
    public function geoip(): BelongsTo
185
    {
186
        return $this->belongsTo(config('rinvex.statistics.models.geoip'), 'geoip_id', 'id');
187
    }
188
189
    /**
190
     * The request always belongs to a device.
191
     *
192
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
193
     */
194
    public function device(): BelongsTo
195
    {
196
        return $this->belongsTo(config('rinvex.statistics.models.device'), 'device_id', 'id');
197
    }
198
199
    /**
200
     * The request always belongs to a platform.
201
     *
202
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
203
     */
204
    public function platform(): BelongsTo
205
    {
206
        return $this->belongsTo(config('rinvex.statistics.models.platform'), 'platform_id', 'id');
207
    }
208
209
    /**
210
     * Get the owning user.
211
     *
212
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
213
     */
214
    public function user(): MorphTo
215
    {
216
        return $this->morphTo();
217
    }
218
219
    /**
220
     * Get bookings of the given user.
221
     *
222
     * @param \Illuminate\Database\Eloquent\Builder $builder
223
     * @param \Illuminate\Database\Eloquent\Model   $user
224
     *
225
     * @return \Illuminate\Database\Eloquent\Builder
226
     */
227
    public function scopeOfUser(Builder $builder, Model $user): Builder
228
    {
229
        return $builder->where('user_type', $user->getMorphClass())->where('user_id', $user->getKey());
230
    }
231
}
232