Completed
Push — master ( 592d95...c00565 )
by Abdelrahman
18:11 queued 10s
created

AuthCode::client()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Oauth\Models;
6
7
use Illuminate\Database\Eloquent\Model;
8
use Rinvex\Support\Traits\ValidatingTrait;
9
use Illuminate\Database\Eloquent\Relations\MorphTo;
10
11
class AuthCode extends Model
12
{
13
    use ValidatingTrait;
14
15
    /**
16
     * {@inheritdoc}
17
     */
18
    protected $fillable = [
19
        'identifier',
20
        'user_id',
21
        'user_type',
22
        'client_id',
23
        'is_revoked',
24
        'expires_at',
25
    ];
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected $casts = [
31
        'identifier' => 'string',
32
        'user_id' => 'integer',
33
        'user_type' => 'string',
34
        'client_id' => 'integer',
35
        'is_revoked' => 'boolean',
36
        'expires_at' => 'datetime',
37
    ];
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected $observables = [
43
        'validating',
44
        'validated',
45
    ];
46
47
    /**
48
     * The default rules that the model will validate against.
49
     *
50
     * @var array
51
     */
52
    protected $rules = [];
53
54
    /**
55
     * Whether the model should throw a
56
     * ValidationException if it fails validation.
57
     *
58
     * @var bool
59
     */
60
    protected $throwValidationExceptions = true;
61
62
    /**
63
     * Create a new Eloquent model instance.
64
     *
65
     * @param array $attributes
66
     */
67
    public function __construct(array $attributes = [])
68
    {
69
        $this->setTable(config('rinvex.oauth.tables.auth_codes'));
70
        $this->mergeRules([
71
            'identifier' => 'required|string|strip_tags|max:100',
72
            'user_id' => 'required|integer',
73
            'user_type' => 'required|string|strip_tags|max:150',
74
            'client_id' => 'required|integer|exists:'.config('rinvex.oauth.tables.clients').',id',
75
            'is_revoked' => 'sometimes|boolean',
76
            'expires_at' => 'nullable|date',
77
        ]);
78
79
        parent::__construct($attributes);
80
    }
81
82
    /**
83
     * Get the client that owns the authentication code.
84
     *
85
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
86
     */
87
    public function client()
88
    {
89
        return $this->belongsTo(config('rinvex.oauth.models.client'));
90
    }
91
92
    /**
93
     * Get the user that the client belongs to.
94
     *
95
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
96
     */
97
    public function user(): MorphTo
98
    {
99
        return $this->morphTo('user', 'user_type', 'user_id', 'id');
100
    }
101
}
102