ldc-crm-client/src/WordPress/WPCrmUser.php

199 lines
4.4 KiB
PHP
Raw Normal View History

2021-08-12 14:49:27 +02:00
<?php
/**
* LDCCRM For Wordpress usage
*
* @category API
* @package None
* @author Jérémy Munsch <jeremy.munsch@ldc.fr>
* @license MIT https://opensource.org/licenses/MIT
* @link /
*/
namespace LDC\LDCCRM\WordPress;
use JsonSerializable;
use LDC\LDCCRM\Models\Customer;
/**
* Class de gestion des users en session avec Wordpress (ce truc immonde)
*
* @category API
* @package LDCCrmClient
* @subpackage Wordpress
* @author Jérémy Munsch <jeremy.munsch@ldc.fr>
* @license MIT https://opensource.org/licenses/MIT
* @link /
*/
class WPCrmUser implements JsonSerializable
{
/**
* The CRM hashcode (UUID)
*
* @var string
*/
2021-08-16 13:24:58 +02:00
protected $phashcode = null;
2021-08-12 14:49:27 +02:00
/**
* The CRM customer email
*
* @var string
*/
2021-08-16 13:24:58 +02:00
protected $pemail = null;
2021-08-12 14:49:27 +02:00
/**
* The CRM customer info (the api response)
*
* @var \LDC\LDCCRM\Models\Customer
*/
2021-08-16 13:24:58 +02:00
protected $info = null;
2021-08-12 14:49:27 +02:00
/**
* WPCrmUser construct
*
* @param string $hashcode The customer hashcode (UUID)
* @param string $email The customer email
*/
2021-08-16 13:24:58 +02:00
public function __construct(string $hashcode = null, string $email = null)
2021-08-12 14:49:27 +02:00
{
2021-08-16 13:24:58 +02:00
$this->phashcode = $hashcode;
$this->pemail = $email;
2021-08-12 14:49:27 +02:00
}
/**
* Magic getter
*
* @param string $prop The prop to get
*
* @return bool|string|\LDC\LDCCRM\Enums\Country
*/
public function __get($prop)
{
if (is_null($this->info)) {
2021-08-16 13:24:58 +02:00
if (!is_null($this->pemail)) {
WPCrm::getUserFromApi($this->pemail);
} else {
$this->info = new Customer();
}
2021-08-12 14:49:27 +02:00
}
2021-08-16 13:24:58 +02:00
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;
2021-08-12 14:49:27 +02:00
}
/**
* Set the customer info
* with api response
*
* @param LDC\LDCCRM\Models\Customer $customer The Api object reponse (user)
*
* @return void
*/
public function setInfo(Customer $customer)
{
2021-08-16 13:24:58 +02:00
$this->info = $customer;
$this->pemail = $customer->email;
$this->phashcode = $customer->hashcode;
2021-08-12 14:49:27 +02:00
}
/**
* Generate a user from session data
*
* @param string $string The json data stored in session
*
* @return \LDCCRM\WPCrmUser
*/
public static function parseFromJson($string)
{
$a = json_decode($string, true);
return new self(
$a['hashcode'],
$a['email'],
$a['info']
);
}
/**
* Serialize this object to an array
*
* @return array
*/
public function jsonSerialize()
{
return [
2021-08-16 13:24:58 +02:00
'hashcode' => $this->phashcode,
'email' => $this->pemail,
2021-08-12 14:49:27 +02:00
'info' => $this->info
];
}
/**
* Save current customer to
* the local database and on crm
*
* @return boolean true if the operation succeeded false otherwise
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException
* with \LDC\LDCCRM\Enums\ApiError::customerExists()
*/
public function save(): bool
{
return WPCrm::saveUser();
}
/**
* Delete current customer to
* the local database and on crm
*
* @return boolean true if the operation succeeded false otherwise
*/
public function delete(): bool
{
return WPCrm::deleteUser();
}
/**
* Toggle the current user optin value
* on the current user and the crm
*
* @return boolean true if the operation succeeded
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException
* with \LDC\LDCCRM\Enums\ApiError::singleOptinCustomerExists()
*/
public function toggleOptin(): bool
{
return WPCrm::toggleUserOptin();
}
/**
* Activate the account in the crm
*
* @return boolean true if succeeded
*/
public function activate(): bool
{
return WPCrm::activateUser();
}
}