ldc-crm/app/Models/CustomerInfo.php

138 lines
4.3 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Country;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Kwaadpepper\Enum\Traits\CastsEnums;
class CustomerInfo extends Model
{
use HasFactory;
use CastsEnums;
protected $enumCasts = [
'newcountry' => Country::class
];
protected $fillable = [
'firstname',
'lastname',
'birth_date',
'address',
'address2',
'phone',
'newcountry',
'country',
'city',
'postcode',
'opt_in'
];
/**
*! TODO Try to refactor this but beware this is difficult and breaks unit tests
*
* @return void
*/
// phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh
protected static function booted()
{
static::creating(function ($customerInfo) {
$customerInfo->hashcode = Str::uuid();
if (ctype_digit($customerInfo->country)) {
try {
$customerInfo->newcountry = Country::make((int)$customerInfo->country);
} catch (\BadMethodCallException $e) {
// TODO change this when country column is updated
// Set afghanistan as default country for now
$customerInfo->newcountry = Country::afghanistan();
}
}
if (\is_object($customerInfo->country) and \get_class($customerInfo->country) === Country::class) {
$customerInfo->newcountry = $customerInfo->country;
$customerInfo->country = $customerInfo->newcountry->label;
}
if (!$customerInfo->newcountry) {
// TODO change this when country column is updated
// Set afghanistan as default country for now
$customerInfo->newcountry = Country::afghanistan();
}
if (!$customerInfo->newcountry->equals(Country::afghanistan())) {
$customerInfo->country = $customerInfo->newcountry->label;
}
});
static::updating(function ($customerInfo) {
if (ctype_digit($customerInfo->country)) {
try {
$customerInfo->newcountry = Country::make((int)$customerInfo->country);
} catch (\BadMethodCallException $e) {
// TODO change this when country column is updated
// Set afghanistan as default country for now
$customerInfo->newcountry = Country::afghanistan();
}
}
if (\is_object($customerInfo->country) and \get_class($customerInfo->country) === Country::class) {
$customerInfo->newcountry = $customerInfo->country;
$customerInfo->country = $customerInfo->newcountry->label;
}
if (!$customerInfo->newcountry) {
// TODO change this when country column is updated
// Set afghanistan as default country for now
$customerInfo->newcountry = Country::afghanistan();
}
if (!$customerInfo->newcountry->equals(Country::afghanistan())) {
$customerInfo->country = $customerInfo->newcountry->label;
}
});
}
/**
* Customer BelongsTo relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function customer()
{
return $this->belongsTo(Customer::class);
}
/**
* get Login
*
* @return \App\Models\CustomerLogin
*/
public function login()
{
return $this->customer->login($this->brand);
}
/**
* Brand BelongsTo relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function brand()
{
return $this->belongsTo(Brand::class);
}
/**
* Get customer info from email
*
* @param Brand $brand
* @param string $email
* @return \Illuminate\Database\Eloquent\Builder
*/
public static function getFromEmail(Brand $brand, string $email)
{
return CustomerInfo::whereHas('brand', function ($q) use ($brand) {
$q->where('id', $brand->id);
})->whereHas('customer', function ($q) use ($email) {
$q->where('email', $email);
});
}
}