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