98 lines
2.7 KiB
PHP
98 lines
2.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use App\Exceptions\CrmException;
|
||
|
|
use App\Http\Resources\CustomerCrmResource;
|
||
|
|
use Illuminate\Support\Str;
|
||
|
|
use Illuminate\Notifications\Notifiable;
|
||
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||
|
|
use Illuminate\Support\Carbon;
|
||
|
|
|
||
|
|
class Customers extends Authenticatable
|
||
|
|
{
|
||
|
|
use Notifiable;
|
||
|
|
|
||
|
|
protected $fillable = [
|
||
|
|
'email',
|
||
|
|
'username'
|
||
|
|
];
|
||
|
|
|
||
|
|
protected $guard = 'customer';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the crm Data of a customer
|
||
|
|
*
|
||
|
|
* @throws CrmException
|
||
|
|
* @return object crm user
|
||
|
|
* @author Jérémy Munsch <jeremy.munsch@ldc.fr>
|
||
|
|
*/
|
||
|
|
public function getApiDataAttribute()
|
||
|
|
{
|
||
|
|
/** @var \App\Lib\LDCCRM\ApiV1 */
|
||
|
|
$crm = app('crmApi');
|
||
|
|
$response = (object)$crm->getCustomer($this->crm_id);
|
||
|
|
switch ($response->status) {
|
||
|
|
case 'KO':
|
||
|
|
throw new CrmException(sprintf('Api call error %s', $response->message));
|
||
|
|
case 'OK':
|
||
|
|
return $response->user;
|
||
|
|
default:
|
||
|
|
throw new CrmException('Unknown status');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Set the crm Data of a customer
|
||
|
|
*
|
||
|
|
* @throws CrmException
|
||
|
|
* @return object crm user
|
||
|
|
* @author Jérémy Munsch <jeremy.munsch@ldc.fr>
|
||
|
|
*/
|
||
|
|
public function updateApiData(CustomerCrmResource $resource)
|
||
|
|
{
|
||
|
|
/** @var \App\Lib\LDCCRM\ApiV1 */
|
||
|
|
$crm = app('crmApi');
|
||
|
|
$response = (object) \call_user_func_array([$crm, 'updateCustomer'], $resource->toArray(null));
|
||
|
|
switch ($response->status) {
|
||
|
|
case 'KO':
|
||
|
|
throw new CrmException(sprintf('Api call error %s', $response->message));
|
||
|
|
case 'OK':
|
||
|
|
return $response->user;
|
||
|
|
default:
|
||
|
|
throw new CrmException('Unknown status');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* This mutate Customer attributes to array
|
||
|
|
* including only non critical properties and crm values
|
||
|
|
* (One that can be send to front office without security breach)
|
||
|
|
*
|
||
|
|
* @return array
|
||
|
|
*/
|
||
|
|
public function toArray()
|
||
|
|
{
|
||
|
|
if ($this->crm_id) {
|
||
|
|
return collect(array_merge(
|
||
|
|
parent::toArray(),
|
||
|
|
(array)$this->apiData
|
||
|
|
))->intersectByKeys([
|
||
|
|
'email' => '',
|
||
|
|
'firstname' => '',
|
||
|
|
'lastname' => '',
|
||
|
|
'address' => '',
|
||
|
|
'address2' => '',
|
||
|
|
'postcode' => '',
|
||
|
|
'birth_date' => '',
|
||
|
|
'city' => '',
|
||
|
|
'country' => '',
|
||
|
|
'opt_in' => '',
|
||
|
|
'phone' => ''
|
||
|
|
])->toArray();
|
||
|
|
} else {
|
||
|
|
return parent::toArray();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|