* @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 * @license MIT https://opensource.org/licenses/MIT * @link / */ class WPCrmUser implements JsonSerializable { /** * The CRM hashcode (UUID) * * @var string */ protected $phashcode = null; /** * The CRM customer email * * @var string */ protected $pemail = null; /** * The CRM customer info (the api response) * * @var \LDC\LDCCRM\Models\Customer */ protected $info = 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; } /** * 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)) { if (!is_null($this->pemail)) { WPCrm::getUserFromApi($this->pemail); } else { $this->info = new Customer(); } } 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; } /** * 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 \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->phashcode, 'email' => $this->pemail, '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(); } }