WPCRM update

This commit is contained in:
Jérémy Munsch 2021-08-16 13:24:58 +02:00
parent b626dfca55
commit 363a23659c
2 changed files with 64 additions and 39 deletions

View file

@ -110,6 +110,7 @@ class WPCrm
}
} catch (ApiErrorException $e) {
switch ($e->getApiError()) {
case ApiError::authorizationException():
case ApiError::modelNotFound():
return false;
default:
@ -138,19 +139,8 @@ class WPCrm
private static function loginUserWithApi(string $email, string $password)
{
self::initLdcApi();
try {
if (!($response = self::makeApiV2Call(self::$api, 'login', [$email, $password]))) {
return $response;
}
} catch (ApiErrorException $e) {
switch ($e->getApiError()) {
case ApiError::customerNotActivated():
case ApiError::modelNotFound():
case ApiError::authFailed():
return false;
default:
throw $e;
}
if (!($response = self::makeApiV2Call(self::$api, 'login', [$email, $password]))) {
return $response;
}
if ($out = self::getUserFromApi($email)) {
self::saveOrUpdateUserToTable();
@ -340,6 +330,16 @@ class WPCrm
return true;
}
/**
* Generate a new user
*
* @return WPCrmUser
*/
public static function getNewUser(): WPCrmUser
{
return self::$user = new WPCrmUser();
}
// --- PRIVATE --- METHODS
/**
@ -352,11 +352,11 @@ class WPCrm
if (!self::getUser()) {
return null;
}
if (!self::getRawProperty(self::$user, 'customer')) {
if (!self::getRawProperty(self::$user, 'info')) {
self::getUserFromApi(self::$user->hashcode);
}
/** @var \LDC\LDCCRM\Models\Customer */
$customer = self::getRawProperty(self::$user, 'customer');
$customer = self::getRawProperty(self::$user, 'info');
return $customer;
}
@ -373,7 +373,7 @@ class WPCrm
{
self::bootSessionIfNeeded();
if (!self::$user) {
self::$user = new self($customer->hashcode, $customer->email);
self::$user = new WPCrmUser($customer->hashcode, $customer->email);
}
self::$user->setInfo($customer);
$_SESSION[self::$sessionKey] = json_encode(self::$user);
@ -459,7 +459,7 @@ class WPCrm
public static function getUserFromTable(string $hashcode)
{
return self::getWpDb()->get_row(
'SELECT * FROM `ldc_wp_customers` WHERE `hashcode` = ' . $hashcode
'SELECT * FROM `ldc_wp_customers` WHERE `hashcode` = "' . $hashcode . '"'
);
}
@ -485,8 +485,8 @@ class WPCrm
public static function getUserToTable()
{
return self::getWpDb()->get_row(
'SELECT * FROM `ldc_wp_customers` WHERE `hashcode` = ' .
self::$user->hashcode
'SELECT * FROM `ldc_wp_customers` WHERE `hashcode` = "' .
self::$user->hashcode . '"'
);
}
@ -497,20 +497,18 @@ class WPCrm
*/
private static function saveOrUpdateUserToTable()
{
if (self::getUserToTable(self::$user->crm_id)) {
if (self::getUserToTable(self::$user->hashcode)) {
self::query(
'UPDATE `ldc_wp_customers` SET `hashcode` = %d, `email` = %d WHERE `crm_id` = %d',
'UPDATE `ldc_wp_customers` SET `email` = %s WHERE `hashcode` = %s',
[
'crm_id' => self::$user->crm_id,
'hashcode' => self::$user->hashcode,
'email' => self::$user->email
'email' => self::$user->email,
'hashcode' => self::$user->hashcode
]
);
} else {
self::query(
'INSERT INTO `ldc_wp_customers`(crm_id, hashcode, email, uuid) VALUES (%d, %s, %s, %s)',
'INSERT INTO `ldc_wp_customers`(hashcode, email, uuid) VALUES (%s, %s, %s)',
[
'crm_id' => self::$user->crm_id,
'hashcode' => self::$user->hashcode,
'email' => self::$user->email,
'uuid' => ''

View file

@ -33,21 +33,21 @@ class WPCrmUser implements JsonSerializable
*
* @var string
*/
protected $hashcode = null;
protected $phashcode = null;
/**
* The CRM customer email
*
* @var string
*/
protected $email = null;
protected $pemail = null;
/**
* The CRM customer info (the api response)
*
* @var \LDC\LDCCRM\Models\Customer
*/
protected $customer = null;
protected $info = null;
/**
* WPCrmUser construct
@ -55,10 +55,10 @@ class WPCrmUser implements JsonSerializable
* @param string $hashcode The customer hashcode (UUID)
* @param string $email The customer email
*/
public function __construct(string $hashcode, string $email)
public function __construct(string $hashcode = null, string $email = null)
{
$this->hashcode = $hashcode;
$this->email = $email;
$this->phashcode = $hashcode;
$this->pemail = $email;
}
/**
@ -71,9 +71,36 @@ class WPCrmUser implements JsonSerializable
public function __get($prop)
{
if (is_null($this->info)) {
WPCrm::getUserFromApi($this->email);
if (!is_null($this->pemail)) {
WPCrm::getUserFromApi($this->pemail);
} else {
$this->info = new Customer();
}
}
return $this->customer->{$prop};
return $this->info->{$prop};
}
/**
* Magic setter
*
* @param string $prop The prop to get
*
* @return bool|string|\LDC\LDCCRM\Enums\Country
*/
public function __set($prop, $value)
{
if (is_null($this->info)) {
if (!is_null($this->pemail)) {
WPCrm::getUserFromApi($this->pemail);
} else {
$this->info = new Customer();
}
}
$this->info->{$prop} = $value;
if ($prop === 'email') {
$this->pemail = $value;
}
return $value;
}
/**
@ -86,9 +113,9 @@ class WPCrmUser implements JsonSerializable
*/
public function setInfo(Customer $customer)
{
$this->info = $customer;
$this->email = $customer->email;
$this->hashcode = $customer->hashcode;
$this->info = $customer;
$this->pemail = $customer->email;
$this->phashcode = $customer->hashcode;
}
/**
@ -116,8 +143,8 @@ class WPCrmUser implements JsonSerializable
public function jsonSerialize()
{
return [
'hashcode' => $this->hashcode,
'email' => $this->email,
'hashcode' => $this->phashcode,
'email' => $this->pemail,
'info' => $this->info
];
}