255 lines
7.6 KiB
PHP
255 lines
7.6 KiB
PHP
<?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\Exceptions\WpCrmInvalidJsonUserInfo;
|
|
use LDC\LDCCRM\Models\Customer;
|
|
|
|
/**
|
|
* Class de gestion des users en session avec Wordpress
|
|
*
|
|
* @property string|null $uuidToken — A local transaction token for this user
|
|
* @property string|null $hashcode — The crm hashcode this is a readonly field
|
|
* @property string|null $pemail — Must be a rfc valid email on a valid domain name
|
|
* @property string $password
|
|
* Required only on create,
|
|
* must have upper and lowers chars,
|
|
* at least 1 number,
|
|
* at leat 1 upper char,
|
|
* at least 1 lower char and
|
|
* at leat 1 special char
|
|
* @property string $firstname — The account first name
|
|
* @property string $lastname — The account last name
|
|
* @property string $birth_date — The account birth date 'yyyy-mm-dd'
|
|
* @property string $phone — The account phone
|
|
* @property string $address
|
|
* The account address (street number, name, build, flat number)
|
|
* @property string $address2 — The account phone (address complement)
|
|
* @property string $city — The account city name
|
|
* @property string $postcode — The account city postcode
|
|
* @property \LDC\LDCCRM\Enums\Country $country — The account country residence
|
|
* @property bool $opt_in — true if the account is opt in
|
|
* @property bool $status — true if the account is activated
|
|
* @property string $last_connection the last time user was connected 'yyyy-mm-dd HH:ii:ss'
|
|
*
|
|
* @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|null
|
|
*/
|
|
protected ?string $phashcode = null;
|
|
|
|
/**
|
|
* The CRM customer email
|
|
*
|
|
* @var string|null
|
|
*/
|
|
protected ?string $pemail = null;
|
|
|
|
/**
|
|
* The CRM customer info (the api response)
|
|
*
|
|
* @var \LDC\LDCCRM\Models\Customer|null
|
|
*/
|
|
protected ?Customer $info = null;
|
|
|
|
/**
|
|
* The CRM customer temp uuidToken to validate transactions
|
|
*
|
|
* @var string|null UUID
|
|
*/
|
|
protected ?string $uuidToken = null;
|
|
|
|
/**
|
|
* WPCrmUser construct
|
|
*
|
|
* @param string $hashcode The customer hashcode (UUID).
|
|
* @param string $email The customer email.
|
|
*/
|
|
public function __construct(string $hashcode = null, string $email = null)
|
|
{
|
|
$this->phashcode = $hashcode;
|
|
$this->pemail = $email;
|
|
}
|
|
|
|
/**
|
|
* Generic accessor
|
|
*
|
|
* @param string $prop The prop to get.
|
|
*
|
|
* @return boolean|string|\LDC\LDCCRM\Enums\Country|null
|
|
*/
|
|
public function __get(string $prop)
|
|
{
|
|
if ($prop === 'uuidToken') {
|
|
return $this->uuidToken;
|
|
}
|
|
if (is_null($this->info)) {
|
|
if (!is_null($this->pemail)) {
|
|
WPCrm::getUserFromApi($this->pemail);
|
|
} else {
|
|
$this->info = new Customer();
|
|
}
|
|
}
|
|
return $this->info->{$prop};
|
|
}
|
|
|
|
/**
|
|
* Generic mutator
|
|
*
|
|
* @param string $prop The property to assign.
|
|
* @param mixed $value The value to set.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __set(string $prop, mixed $value): void
|
|
{
|
|
if ($prop === 'uuidToken') {
|
|
$this->uuidToken = $value;
|
|
return;
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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)
|
|
{
|
|
$this->info = $customer;
|
|
$this->pemail = $customer->email;
|
|
$this->phashcode = $customer->hashcode;
|
|
}
|
|
|
|
/**
|
|
* Generate a user from session data
|
|
*
|
|
* @param string $string The json data stored in session.
|
|
*
|
|
* @return \LDC\LDCCRM\WordPress\WPCrmUser
|
|
* @throws \LDC\LDCCRM\Exceptions\WpCrmInvalidJsonUserInfo If the json string does no
|
|
* contain the needed information.
|
|
*/
|
|
public static function parseFromJson(string $string): WPCrmUser
|
|
{
|
|
$a = json_decode($string, true);
|
|
if (!isset($a['hashcode']) or !isset($a['email'])) {
|
|
throw new WpCrmInvalidJsonUserInfo(\sprintf(
|
|
'%s::%s is missing some information to parse from, uuid was %s',
|
|
__CLASS__,
|
|
__METHOD__,
|
|
$a['hashcode']
|
|
));
|
|
}
|
|
return new self(
|
|
$a['hashcode'],
|
|
$a['email']
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Serialize this object to an array
|
|
*
|
|
* @return array
|
|
*/
|
|
public function jsonSerialize()
|
|
{
|
|
return [
|
|
'hashcode' => $this->phashcode,
|
|
'email' => $this->pemail
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Save current customer to
|
|
* the local database and on crm
|
|
*
|
|
* @return boolean true if the operation succeeded false otherwise
|
|
* @throws \LDC\LDCCRM\Exceptions\CrmException If a critical error occured.
|
|
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
|
|
* Possibles are : - validationFailed, If provided data is wrong(CRM side).
|
|
* - modelNotFound, If this customer hashcode could not be found.
|
|
*/
|
|
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
|
|
* @throws \LDC\LDCCRM\Exceptions\CrmException If a critical error occured.
|
|
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
|
|
* Possibles are : - modelNotFound, If this customer hashcode could not be found.
|
|
*/
|
|
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\CrmException If a critical error occured.
|
|
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
|
|
* Possibles are : - modelNotFound, If this customer hashcode could not be found.
|
|
*/
|
|
public function toggleOptin(): bool
|
|
{
|
|
return WPCrm::toggleUserOptin();
|
|
}
|
|
|
|
/**
|
|
* Activate the account in the crm
|
|
*
|
|
* @return boolean true if succeeded
|
|
* @throws \LDC\LDCCRM\Exceptions\ValidationException If a customer's hashcode could not be validated.
|
|
* @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
|
|
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
|
|
* Possibles are : - modelNotFound, If this customer hashcode could not be found.
|
|
* - customerAlreadyActive, It the customer is already activated.
|
|
*/
|
|
public function activate(): bool
|
|
{
|
|
return WPCrm::activateUser();
|
|
}
|
|
}
|