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

Datum::user()   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
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
13
class Datum extends Model
14
{
15
    use ValidatingTrait;
16
    use CacheableEloquent;
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    protected $fillable = [
22
        'session_id',
23
        'user_id',
24
        'user_type',
25
        'status_code',
26
        'uri',
27
        'method',
28
        'server',
29
        'input',
30
        'created_at',
31
    ];
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    protected $casts = [
37
        'session_id' => 'string',
38
        'user_id' => 'integer',
39
        'user_type' => 'string',
40
        'status_code' => 'integer',
41
        'uri' => 'string',
42
        'method' => 'string',
43
        'server' => 'json',
44
        'input' => 'json',
45
        'created_at' => 'datetime',
46
    ];
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public $timestamps = false;
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    protected $observables = [
57
        'validating',
58
        'validated',
59
    ];
60
61
    /**
62
     * The default rules that the model will validate against.
63
     *
64
     * @var array
65
     */
66
    protected $rules = [
67
        'session_id' => 'required|string',
68
        'user_id' => 'nullable|integer',
69
        'user_type' => 'nullable|string',
70
        'status_code' => 'required|integer',
71
        'uri' => 'required|string',
72
        'method' => 'required|string',
73
        'server' => 'required|array',
74
        'input' => 'nullable|array',
75
        'created_at' => 'required|date',
76
    ];
77
78
    /**
79
     * Whether the model should throw a
80
     * ValidationException if it fails validation.
81
     *
82
     * @var bool
83
     */
84
    protected $throwValidationExceptions = true;
85
86
    /**
87
     * Create a new Eloquent model instance.
88
     *
89
     * @param array $attributes
90
     */
91
    public function __construct(array $attributes = [])
92
    {
93
        parent::__construct($attributes);
94
95
        $this->setTable(config('rinvex.statistics.tables.data'));
96
    }
97
98
    /**
99
     * Get the owning user.
100
     *
101
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
102
     */
103
    public function user(): MorphTo
104
    {
105
        return $this->morphTo();
106
    }
107
108
    /**
109
     * Get bookings of the given user.
110
     *
111
     * @param \Illuminate\Database\Eloquent\Builder $builder
112
     * @param \Illuminate\Database\Eloquent\Model   $user
113
     *
114
     * @return \Illuminate\Database\Eloquent\Builder
115
     */
116
    public function scopeOfUser(Builder $builder, Model $user): Builder
117
    {
118
        return $builder->where('user_type', $user->getMorphClass())->where('user_id', $user->getKey());
119
    }
120
}
121