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