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

172 lines
3.7 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
*/
protected $hashcode = null;
/**
* The CRM customer email
*
* @var string
*/
protected $email = null;
/**
* The CRM customer info (the api response)
*
* @var \LDC\LDCCRM\Models\Customer
*/
protected $customer = null;
/**
* WPCrmUser construct
*
* @param string $hashcode The customer hashcode (UUID)
* @param string $email The customer email
*/
public function __construct(string $hashcode, string $email)
{
$this->hashcode = $hashcode;
$this->email = $email;
}
/**
* 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)) {
WPCrm::getUserFromApi($this->email);
}
return $this->customer->{$prop};
}
/**
* 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->email = $customer->email;
$this->hashcode = $customer->hashcode;
}
/**
* 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 [
'hashcode' => $this->hashcode,
'email' => $this->email,
'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();
}
}