Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.33% |
14 / 15 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| CustomerInfo | |
93.33% |
14 / 15 |
|
80.00% |
4 / 5 |
5.01 | |
0.00% |
0 / 1 |
| booted | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| customer | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| login | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| brand | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getFromEmail | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Models; |
| 4 | |
| 5 | use App\Enums\Country; |
| 6 | use Illuminate\Support\Str; |
| 7 | use Illuminate\Database\Eloquent\Factories\HasFactory; |
| 8 | use Illuminate\Database\Eloquent\Model; |
| 9 | use Kwaadpepper\Enum\Traits\CastsEnums; |
| 10 | |
| 11 | class CustomerInfo extends Model |
| 12 | { |
| 13 | use HasFactory; |
| 14 | use CastsEnums; |
| 15 | |
| 16 | /** |
| 17 | * Map attribute names to enum classes. |
| 18 | * |
| 19 | * @var array |
| 20 | */ |
| 21 | protected $enumCasts = [ |
| 22 | 'country' => Country::class |
| 23 | ]; |
| 24 | |
| 25 | /** |
| 26 | * The attributes that should be cast to native types. |
| 27 | * |
| 28 | * @var array |
| 29 | */ |
| 30 | protected $casts = [ |
| 31 | 'opt_in' => 'bool' |
| 32 | ]; |
| 33 | |
| 34 | /** |
| 35 | * The attributes that are mass assignable. |
| 36 | * |
| 37 | * @var string[] |
| 38 | */ |
| 39 | protected $fillable = [ |
| 40 | 'firstname', |
| 41 | 'lastname', |
| 42 | 'birth_date', |
| 43 | 'address', |
| 44 | 'address2', |
| 45 | 'phone', |
| 46 | 'country', |
| 47 | 'city', |
| 48 | 'postcode', |
| 49 | 'opt_in' |
| 50 | ]; |
| 51 | |
| 52 | /** |
| 53 | * Perform any actions required after the model boots. |
| 54 | * |
| 55 | * @return void |
| 56 | */ |
| 57 | protected static function booted() |
| 58 | { |
| 59 | static::creating(function (CustomerInfo $customerInfo) { |
| 60 | $customerInfo->hashcode = Str::uuid(); |
| 61 | }); |
| 62 | static::deleting(function (CustomerInfo $customerInfo) { |
| 63 | CustomerOptInHistory::whereHas('customer', function ($q) use ($customerInfo) { |
| 64 | $q->whereId($customerInfo->customer->id); |
| 65 | })->whereHas('brand', function ($q) use ($customerInfo) { |
| 66 | $q->whereId($customerInfo->brand->id); |
| 67 | })->delete(); |
| 68 | }); |
| 69 | } |
| 70 | |
| 71 | // * RELATIONS |
| 72 | |
| 73 | /** |
| 74 | * Customer BelongsTo relation |
| 75 | * |
| 76 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
| 77 | */ |
| 78 | public function customer() |
| 79 | { |
| 80 | return $this->belongsTo(Customer::class); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * get Login |
| 85 | * |
| 86 | * @return \App\Models\CustomerLogin |
| 87 | */ |
| 88 | public function login() |
| 89 | { |
| 90 | return $this->customer->login($this->brand); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Brand BelongsTo relation |
| 95 | * |
| 96 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
| 97 | */ |
| 98 | public function brand() |
| 99 | { |
| 100 | return $this->belongsTo(Brand::class); |
| 101 | } |
| 102 | |
| 103 | // * METHODS |
| 104 | |
| 105 | /** |
| 106 | * Get customer info from email |
| 107 | * |
| 108 | * @param Brand $brand |
| 109 | * @param string $email |
| 110 | * @return \Illuminate\Database\Eloquent\Builder |
| 111 | */ |
| 112 | public static function getFromEmail(Brand $brand, string $email) |
| 113 | { |
| 114 | return CustomerInfo::whereHas('brand', function ($q) use ($brand) { |
| 115 | $q->where('id', $brand->id); |
| 116 | })->whereHas('customer', function ($q) use ($email) { |
| 117 | $q->where('email', $email); |
| 118 | }); |
| 119 | } |
| 120 | } |