Request   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 220
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A boot() 0 15 1
A route() 0 4 1
A path() 0 4 1
A agent() 0 4 1
A geoip() 0 4 1
A device() 0 4 1
A platform() 0 4 1
A user() 0 4 1
A scopeOfUser() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tracking\Models\Statistics;
6
7
use Illuminate\Database\Eloquent\Model;
8
use Muleta\Recursos\Cacheable\CacheableEloquent;
9
use Illuminate\Database\Eloquent\Builder;
10
use Muleta\Traits\Models\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
    public $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(\Illuminate\Support\Facades\Config::get('tracking.statistics.tables.requests'));
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    protected static function boot()
136
    {
137
        parent::boot();
138
139
        static::saved(
140
            function (self $request) {
141
                $request->path()->increment('count');
142
                $request->route()->increment('count');
143
                $request->geoip()->increment('count');
144
                $request->agent()->increment('count');
145
                $request->device()->increment('count');
146
                $request->platform()->increment('count');
147
            }
148
        );
149
    }
150
151
    /**
152
     * The request always belongs to a route.
153
     *
154
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
155
     */
156
    public function route(): BelongsTo
157
    {
158
        return $this->belongsTo(\Illuminate\Support\Facades\Config::get('tracking.statistics.models.route'), 'route_id', 'id');
159
    }
160
161
    /**
162
     * The request always belongs to a path.
163
     *
164
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
165
     */
166
    public function path(): BelongsTo
167
    {
168
        return $this->belongsTo(\Illuminate\Support\Facades\Config::get('tracking.statistics.models.path'), 'path_id', 'id');
169
    }
170
171
    /**
172
     * The request always belongs to an agent.
173
     *
174
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
175
     */
176
    public function agent(): BelongsTo
177
    {
178
        return $this->belongsTo(\Illuminate\Support\Facades\Config::get('tracking.statistics.models.agent'), 'agent_id', 'id');
179
    }
180
181
    /**
182
     * The request always belongs to an geoip.
183
     *
184
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
185
     */
186
    public function geoip(): BelongsTo
187
    {
188
        return $this->belongsTo(\Illuminate\Support\Facades\Config::get('tracking.statistics.models.geoip'), 'geoip_id', 'id');
189
    }
190
191
    /**
192
     * The request always belongs to a device.
193
     *
194
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
195
     */
196
    public function device(): BelongsTo
197
    {
198
        return $this->belongsTo(\Illuminate\Support\Facades\Config::get('tracking.statistics.models.device'), 'device_id', 'id');
199
    }
200
201
    /**
202
     * The request always belongs to a platform.
203
     *
204
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
205
     */
206
    public function platform(): BelongsTo
207
    {
208
        return $this->belongsTo(\Illuminate\Support\Facades\Config::get('tracking.statistics.models.platform'), 'platform_id', 'id');
209
    }
210
211
    /**
212
     * Get the owning user.
213
     *
214
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
215
     */
216
    public function user(): MorphTo
217
    {
218
        return $this->morphTo('user', 'user_type', 'user_id');
219
    }
220
221
    /**
222
     * Get bookings of the given user.
223
     *
224
     * @param \Illuminate\Database\Eloquent\Builder $builder
225
     * @param \Illuminate\Database\Eloquent\Model   $user
226
     *
227
     * @return \Illuminate\Database\Eloquent\Builder
228
     */
229
    public function scopeOfUser(Builder $builder, Model $user): Builder
230
    {
231
        return $builder->where('user_type', $user->getMorphClass())->where('user_id', $user->getKey());
232
    }
233
}
234