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

252 lines
6 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)
*
* @property string $uuidToken — A local transaction token for this user
*
*
* @property string $hashcode — The crm hashcode this is a readonly field
*
* @property string $email — 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'
*
2021-08-12 14:49:27 +02:00
* @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
/**
* The CRM customer temp uuidToken to validate transactions
*
* @var string UUID
*/
protected $uuidToken = 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 ($prop === 'uuidToken') {
return $this->uuidToken;
}
2021-08-12 14:49:27 +02:00
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 ($prop === 'uuidToken') {
return $this->uuidToken = $value;
}
2021-08-16 13:24:58 +02:00
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-12-06 00:16:13 +01:00
$this->info = $customer;
2021-08-16 13:24:58 +02:00
$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();
}
}