Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
9 / 9 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| CustomerLogin | |
100.00% |
9 / 9 |
|
100.00% |
4 / 4 |
6 | |
100.00% |
1 / 1 |
| booted | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| customer | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| brand | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| passwordMatch | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Models; |
| 4 | |
| 5 | use Illuminate\Database\Eloquent\Factories\HasFactory; |
| 6 | use Illuminate\Database\Eloquent\Model; |
| 7 | use Illuminate\Support\Facades\Hash; |
| 8 | |
| 9 | class CustomerLogin extends Model |
| 10 | { |
| 11 | use HasFactory; |
| 12 | |
| 13 | /** |
| 14 | * The model's attributes. |
| 15 | * |
| 16 | * @var array |
| 17 | */ |
| 18 | protected $attributes = [ |
| 19 | 'status' => false |
| 20 | ]; |
| 21 | |
| 22 | /** |
| 23 | * The attributes that are mass assignable. |
| 24 | * |
| 25 | * @var string[] |
| 26 | */ |
| 27 | protected $fillable = [ |
| 28 | 'password' |
| 29 | ]; |
| 30 | |
| 31 | /** |
| 32 | * The attributes that should be cast to native types. |
| 33 | * |
| 34 | * @var array |
| 35 | */ |
| 36 | protected $casts = [ |
| 37 | 'status' => 'bool' |
| 38 | ]; |
| 39 | |
| 40 | /** |
| 41 | * Perform any actions required after the model boots. |
| 42 | * |
| 43 | * @return void |
| 44 | */ |
| 45 | protected static function booted() |
| 46 | { |
| 47 | static::creating(function ($customerLogin) { |
| 48 | if (Hash::needsRehash($customerLogin->password)) { |
| 49 | $customerLogin->password = Hash::make(trim($customerLogin->password)); |
| 50 | } |
| 51 | }); |
| 52 | static::updating(function ($customerLogin) { |
| 53 | if (Hash::needsRehash($customerLogin->password)) { |
| 54 | $customerLogin->password = Hash::make(trim($customerLogin->password)); |
| 55 | } |
| 56 | }); |
| 57 | } |
| 58 | |
| 59 | // * RELATIONS |
| 60 | |
| 61 | /** |
| 62 | * Customer BelongsTo relation |
| 63 | * |
| 64 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
| 65 | */ |
| 66 | public function customer() |
| 67 | { |
| 68 | return $this->belongsTo(Customer::class); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Brand BelongsTo relation |
| 73 | * |
| 74 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
| 75 | */ |
| 76 | public function brand() |
| 77 | { |
| 78 | return $this->belongsTo(Brand::class); |
| 79 | } |
| 80 | |
| 81 | // * METHODS |
| 82 | /** |
| 83 | * Check if the user password is correct |
| 84 | * |
| 85 | * @param string $password |
| 86 | * @return boolean |
| 87 | */ |
| 88 | public function passwordMatch(string $password): bool |
| 89 | { |
| 90 | return Hash::check($password, $this->password); |
| 91 | } |
| 92 | } |