1 | <?php |
||
42 | class Purchase extends Eloquent\Model |
||
43 | { |
||
44 | const UPCOMING = 'upcoming'; |
||
45 | const DELIVERED = 'delivered'; |
||
46 | const COMPLETED = 'completed'; |
||
47 | |||
48 | use Eloquent\SoftDeletes; |
||
49 | |||
50 | /** |
||
51 | * We do not want any timestamps. |
||
52 | * |
||
53 | * @var bool |
||
54 | */ |
||
55 | public $timestamps = false; |
||
56 | |||
57 | /** |
||
58 | * The attributes that are not mass assignable. |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $guarded = []; |
||
63 | |||
64 | /** |
||
65 | * Date mutators. |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $dates = ['date_purchased', 'delivery_date', 'deleted_at']; |
||
70 | |||
71 | /** |
||
72 | * Default date format. |
||
73 | * |
||
74 | * @var string |
||
75 | */ |
||
76 | protected $dateFormat = 'Y-m-d'; |
||
77 | |||
78 | 10 | public static function boot() |
|
79 | { |
||
80 | 10 | parent::boot(); |
|
81 | |||
82 | 10 | static::deleting(function (Purchase $purchase) { |
|
83 | 1 | $purchase->guitars()->delete(); |
|
84 | 1 | $purchase->notifications()->delete(); |
|
85 | 10 | }); |
|
86 | 10 | } |
|
87 | |||
88 | /** |
||
89 | * Delivered Scope. |
||
90 | * |
||
91 | * @param $query |
||
92 | * |
||
93 | * @return Eloquent\Builder |
||
94 | */ |
||
95 | public function scopeDelivered($query) |
||
99 | |||
100 | /** |
||
101 | * Stored Scope. |
||
102 | * |
||
103 | * @param $query |
||
104 | * |
||
105 | * @return Eloquent\Builder |
||
106 | */ |
||
107 | public function scopeStored($query) |
||
111 | |||
112 | /** |
||
113 | * Pending Storage scope. |
||
114 | * |
||
115 | * @param $query |
||
116 | * |
||
117 | * @return Eloquent\Builder |
||
118 | */ |
||
119 | public function scopePendingStorage($query) |
||
123 | |||
124 | /** |
||
125 | * Get status attribute. |
||
126 | * |
||
127 | * @return string |
||
128 | */ |
||
129 | 2 | public function getStatusAttribute() |
|
130 | { |
||
131 | 2 | if ($this->delivery_date->isFuture()) { |
|
132 | 2 | return self::UPCOMING; |
|
133 | } |
||
134 | if (! $this->isPendingStorage()) { |
||
135 | return self::COMPLETED; |
||
136 | } |
||
137 | |||
138 | return self::DELIVERED; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Has Guitars Pending Storage? |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | public function isPendingStorage() |
||
150 | |||
151 | /** |
||
152 | * Guitars bought. |
||
153 | * |
||
154 | * @return Eloquent\Relations\HasMany |
||
155 | */ |
||
156 | 2 | public function guitars() |
|
157 | { |
||
158 | 2 | return $this->hasMany(Guitar::class); |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * Storage attribute. |
||
163 | * |
||
164 | * @return int |
||
165 | */ |
||
166 | 1 | public function getStoredAttribute() |
|
167 | { |
||
168 | 1 | return $this->guitars()->count(); |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * Pending Storage attribute. |
||
173 | * |
||
174 | * @return int |
||
175 | */ |
||
176 | public function getPendingStorageAttribute() |
||
180 | |||
181 | /** |
||
182 | * Number of Guitars Sold. |
||
183 | * |
||
184 | * @return int |
||
185 | */ |
||
186 | 1 | public function getSoldAttribute() |
|
187 | { |
||
188 | 1 | return $this->guitars()->sold()->count(); |
|
189 | } |
||
190 | |||
191 | /** |
||
192 | * Whether the purchase has arrived. |
||
193 | * |
||
194 | * @return bool |
||
195 | */ |
||
196 | 1 | public function hasArrived() |
|
197 | { |
||
198 | 1 | return $this->isStatus(self::DELIVERED); |
|
199 | } |
||
200 | |||
201 | /** |
||
202 | * Verify the status. |
||
203 | * |
||
204 | * @param $status |
||
205 | * |
||
206 | * @return bool |
||
207 | */ |
||
208 | 1 | public function isStatus($status) |
|
209 | { |
||
210 | 1 | return $this->status == $status; |
|
211 | } |
||
212 | |||
213 | /** |
||
214 | * Supplier from which guitars were bought. |
||
215 | * |
||
216 | * @return Eloquent\Relations\BelongsTo |
||
217 | */ |
||
218 | public function supplier() |
||
222 | |||
223 | /** |
||
224 | * Make of the guitars. |
||
225 | * |
||
226 | * @return Eloquent\Relations\BelongsTo |
||
227 | */ |
||
228 | 1 | public function make() |
|
229 | { |
||
230 | 1 | return $this->belongsTo(Make::class); |
|
231 | } |
||
232 | |||
233 | /** |
||
234 | * Notifications about the purchase. |
||
235 | * |
||
236 | * @return Eloquent\Relations\MorphMany |
||
237 | */ |
||
238 | 1 | public function notifications() |
|
239 | { |
||
240 | 1 | return $this->morphMany(Notification::class, 'notifiable'); |
|
241 | } |
||
242 | |||
243 | /** |
||
244 | * Create a new notification. |
||
245 | * @return Notification |
||
246 | */ |
||
247 | public function makeNotification() |
||
259 | } |
||
260 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.