Completed
Push — master ( f22c3b...c2c878 )
by Abdelrahman
01:18
created

Testimonial   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 166
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A scopeOfSubject() 0 4 1
A scopeOfAttestant() 0 4 1
A scopeApproved() 0 4 1
A scopeDisapproved() 0 4 1
A subject() 0 4 1
A attestant() 0 4 1
A approve() 0 6 1
A disapprove() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Testimonials\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
/**
14
 * Rinvex\Testimonials\Models\Testimonial.
15
 *
16
 * @property int                                                                             $id
17
 * @property int                                                                             $subject_id
18
 * @property string                                                                          $subject_type
19
 * @property int                                                                             $attestant_id
20
 * @property string                                                                          $attestant_type
21
 * @property bool                                                                            $is_approved
22
 * @property string                                                                          $body
23
 * @property \Carbon\Carbon|null                                                             $created_at
24
 * @property \Carbon\Carbon|null                                                             $updated_at
25
 * @property \Carbon\Carbon|null                                                             $deleted_at
26
 * @property-read \Illuminate\Database\Eloquent\Model|\Eloquent                              $attestant
27
 * @property-read \Illuminate\Database\Eloquent\Model|\Eloquent                              $subject
28
 *
29
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial approved()
30
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial disapproved()
31
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereAttestantId($value)
32
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereAttestantType($value)
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...
33
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereBody($value)
34
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereCreatedAt($value)
35
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereDeletedAt($value)
36
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereId($value)
37
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereIsApproved($value)
38
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereSubjectId($value)
39
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereSubjectType($value)
40
 * @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Testimonials\Models\Testimonial whereUpdatedAt($value)
41
 * @mixin \Eloquent
42
 */
43
class Testimonial extends Model
44
{
45
    use ValidatingTrait;
46
    use CacheableEloquent;
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    protected $fillable = [
52
        'subject_id',
53
        'subject_type',
54
        'attestant_id',
55
        'attestant_type',
56
        'is_approved',
57
        'body',
58
    ];
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected $casts = [
64
        'subject_id' => 'integer',
65
        'subject_type' => 'string',
66
        'attestant_id' => 'integer',
67
        'attestant_type' => 'string',
68
        'is_approved' => 'boolean',
69
        'body' => 'string',
70
        'deleted_at' => 'datetime',
71
    ];
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    protected $observables = [
77
        'validating',
78
        'validated',
79
    ];
80
81
    /**
82
     * The default rules that the model will validate against.
83
     *
84
     * @var array
85
     */
86
    protected $rules = [
87
        'subject_id' => 'required|integer',
88
        'subject_type' => 'required|string|max:150',
89
        'attestant_id' => 'required|integer',
90
        'attestant_type' => 'required|string|max:150',
91
        'is_approved' => 'sometimes|boolean',
92
        'body' => 'required|string|max:150',
93
    ];
94
95
    /**
96
     * Whether the model should throw a
97
     * ValidationException if it fails validation.
98
     *
99
     * @var bool
100
     */
101
    protected $throwValidationExceptions = true;
102
103
    /**
104
     * Create a new Eloquent model instance.
105
     *
106
     * @param array $attributes
107
     */
108
    public function __construct(array $attributes = [])
109
    {
110
        parent::__construct($attributes);
111
112
        $this->setTable(config('rinvex.testimonials.tables.testimonials'));
113
    }
114
115
    /**
116
     * Get testimonials of the given subject.
117
     *
118
     * @param \Illuminate\Database\Eloquent\Builder $builder
119
     * @param \Illuminate\Database\Eloquent\Model   $subject
120
     *
121
     * @return \Illuminate\Database\Eloquent\Builder
122
     */
123
    public function scopeOfSubject(Builder $builder, Model $subject): Builder
124
    {
125
        return $builder->where('subject_type', $subject->getMorphClass())->where('subject_id', $subject->getKey());
126
    }
127
128
    /**
129
     * Get testimonials of the given attestant.
130
     *
131
     * @param \Illuminate\Database\Eloquent\Builder $builder
132
     * @param \Illuminate\Database\Eloquent\Model   $attestant
133
     *
134
     * @return \Illuminate\Database\Eloquent\Builder
135
     */
136
    public function scopeOfAttestant(Builder $builder, Model $attestant): Builder
137
    {
138
        return $builder->where('attestant_type', $attestant->getMorphClass())->where('attestant_id', $attestant->getKey());
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 123 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...
139
    }
140
141
    /**
142
     * Get the approved testimonials.
143
     *
144
     * @param \Illuminate\Database\Eloquent\Builder $builder
145
     *
146
     * @return \Illuminate\Database\Eloquent\Builder
147
     */
148
    public function scopeApproved(Builder $builder): Builder
149
    {
150
        return $builder->where('is_approved', true);
151
    }
152
153
    /**
154
     * Get the disapproved testimonials.
155
     *
156
     * @param \Illuminate\Database\Eloquent\Builder $builder
157
     *
158
     * @return \Illuminate\Database\Eloquent\Builder
159
     */
160
    public function scopeDisapproved(Builder $builder): Builder
161
    {
162
        return $builder->where('is_approved', false);
163
    }
164
165
    /**
166
     * Get the subject model of the testimonial.
167
     *
168
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
169
     */
170
    public function subject(): MorphTo
171
    {
172
        return $this->morphTo();
173
    }
174
175
    /**
176
     * Get the attestant model of the testimonial.
177
     *
178
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
179
     */
180
    public function attestant(): MorphTo
181
    {
182
        return $this->morphTo();
183
    }
184
185
    /**
186
     * Approve the testimonial.
187
     *
188
     * @return $this
189
     */
190
    public function approve()
191
    {
192
        $this->update(['is_approved' => true]);
193
194
        return $this;
195
    }
196
197
    /**
198
     * Disapprove the testimonial.
199
     *
200
     * @return $this
201
     */
202
    public function disapprove()
203
    {
204
        $this->update(['is_approved' => false]);
205
206
        return $this;
207
    }
208
}
209