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