Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 38 |
| WPCrmUser | |
0.00% |
0 / 1 |
|
0.00% |
0 / 10 |
380 | |
0.00% |
0 / 38 |
| __construct | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 3 |
|||
| __get | |
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 7 |
|||
| __set | |
0.00% |
0 / 1 |
30 | |
0.00% |
0 / 11 |
|||
| setInfo | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 4 |
|||
| parseFromJson | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 7 |
|||
| jsonSerialize | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
| save | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| delete | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| toggleOptin | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| activate | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * LDCCRM For Wordpress usage |
| 5 | * |
| 6 | * @category API |
| 7 | * @package None |
| 8 | * @author Jérémy Munsch <jeremy.munsch@ldc.fr> |
| 9 | * @license MIT https://opensource.org/licenses/MIT |
| 10 | * @link / |
| 11 | */ |
| 12 | |
| 13 | namespace LDC\LDCCRM\WordPress; |
| 14 | |
| 15 | use JsonSerializable; |
| 16 | use LDC\LDCCRM\Exceptions\WpCrmInvalidJsonUserInfo; |
| 17 | use LDC\LDCCRM\Models\Customer; |
| 18 | |
| 19 | /** |
| 20 | * Class de gestion des users en session avec Wordpress (ce truc immonde) |
| 21 | * |
| 22 | * @property string $uuidToken — A local transaction token for this user |
| 23 | * |
| 24 | * |
| 25 | * @property string $hashcode — The crm hashcode this is a readonly field |
| 26 | * |
| 27 | * @property string $email — Must be a rfc valid email on a valid domain name |
| 28 | * |
| 29 | * @property string $password |
| 30 | * Required only on create, |
| 31 | * must have upper and lowers chars, |
| 32 | * at least 1 number, |
| 33 | * at leat 1 upper char, |
| 34 | * at least 1 lower char and |
| 35 | * at leat 1 special char |
| 36 | * |
| 37 | * @property string $firstname — The account first name |
| 38 | * |
| 39 | * @property string $lastname — The account last name |
| 40 | * |
| 41 | * @property string $birth_date — The account birth date 'yyyy-mm-dd' |
| 42 | * |
| 43 | * @property string $phone — The account phone |
| 44 | * |
| 45 | * @property string $address |
| 46 | * The account address (street number, name, build, flat number) |
| 47 | * |
| 48 | * @property string $address2 — The account phone (address complement) |
| 49 | * |
| 50 | * @property string $city — The account city name |
| 51 | * |
| 52 | * @property string $postcode — The account city postcode |
| 53 | * |
| 54 | * @property \LDC\LDCCRM\Enums\Country $country — The account country residence |
| 55 | * |
| 56 | * @property bool $opt_in — true if the account is opt in |
| 57 | * |
| 58 | * @property bool $status — true if the account is activated |
| 59 | * |
| 60 | * @property string $last_connection the last time user was connected 'yyyy-mm-dd HH:ii:ss' |
| 61 | * |
| 62 | * @category API |
| 63 | * @package LDCCrmClient |
| 64 | * @subpackage Wordpress |
| 65 | * @author Jérémy Munsch <jeremy.munsch@ldc.fr> |
| 66 | * @license MIT https://opensource.org/licenses/MIT |
| 67 | * @link / |
| 68 | */ |
| 69 | class WPCrmUser implements JsonSerializable |
| 70 | { |
| 71 | /** |
| 72 | * The CRM hashcode (UUID) |
| 73 | * |
| 74 | * @var string |
| 75 | */ |
| 76 | protected $phashcode = null; |
| 77 | |
| 78 | /** |
| 79 | * The CRM customer email |
| 80 | * |
| 81 | * @var string |
| 82 | */ |
| 83 | protected $pemail = null; |
| 84 | |
| 85 | /** |
| 86 | * The CRM customer info (the api response) |
| 87 | * |
| 88 | * @var \LDC\LDCCRM\Models\Customer |
| 89 | */ |
| 90 | protected $info = null; |
| 91 | |
| 92 | /** |
| 93 | * The CRM customer temp uuidToken to validate transactions |
| 94 | * |
| 95 | * @var string UUID |
| 96 | */ |
| 97 | protected $uuidToken = null; |
| 98 | |
| 99 | /** |
| 100 | * WPCrmUser construct |
| 101 | * |
| 102 | * @param string $hashcode The customer hashcode (UUID). |
| 103 | * @param string $email The customer email. |
| 104 | */ |
| 105 | public function __construct(string $hashcode = null, string $email = null) |
| 106 | { |
| 107 | $this->phashcode = $hashcode; |
| 108 | $this->pemail = $email; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Generic accessor |
| 113 | * |
| 114 | * @param string $prop The prop to get. |
| 115 | * |
| 116 | * @return boolean|string|\LDC\LDCCRM\Enums\Country|null |
| 117 | */ |
| 118 | public function __get(string $prop) |
| 119 | { |
| 120 | if ($prop === 'uuidToken') { |
| 121 | return $this->uuidToken; |
| 122 | } |
| 123 | if (is_null($this->info)) { |
| 124 | if (!is_null($this->pemail)) { |
| 125 | WPCrm::getUserFromApi($this->pemail); |
| 126 | } else { |
| 127 | $this->info = new Customer(); |
| 128 | } |
| 129 | } |
| 130 | return $this->info->{$prop}; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Generic mutator |
| 135 | * |
| 136 | * @param string $prop The property to assign. |
| 137 | * @param mixed $value The value to set. |
| 138 | * |
| 139 | * @return void |
| 140 | */ |
| 141 | public function __set(string $prop, $value): void |
| 142 | { |
| 143 | if ($prop === 'uuidToken') { |
| 144 | $this->uuidToken = $value; |
| 145 | return; |
| 146 | } |
| 147 | if (is_null($this->info)) { |
| 148 | if (!is_null($this->pemail)) { |
| 149 | WPCrm::getUserFromApi($this->pemail); |
| 150 | } else { |
| 151 | $this->info = new Customer(); |
| 152 | } |
| 153 | } |
| 154 | $this->info->{$prop} = $value; |
| 155 | if ($prop === 'email') { |
| 156 | $this->pemail = $value; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Set the customer info |
| 162 | * with api response |
| 163 | * |
| 164 | * @param \LDC\LDCCRM\Models\Customer $customer The Api object reponse (user). |
| 165 | * |
| 166 | * @return void |
| 167 | */ |
| 168 | public function setInfo(Customer $customer) |
| 169 | { |
| 170 | $this->info = $customer; |
| 171 | $this->pemail = $customer->email; |
| 172 | $this->phashcode = $customer->hashcode; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Generate a user from session data |
| 177 | * |
| 178 | * @param string $string The json data stored in session. |
| 179 | * |
| 180 | * @return \LDC\LDCCRM\WordPress\WPCrmUser |
| 181 | * @throws \LDC\LDCCRM\Exceptions\WpCrmInvalidJsonUserInfo If the json string does no |
| 182 | * contain the needed information. |
| 183 | */ |
| 184 | public static function parseFromJson(string $string): WPCrmUser |
| 185 | { |
| 186 | $a = json_decode($string, true); |
| 187 | if (!isset($a['hashcode']) or !isset($a['email'])) { |
| 188 | throw new WpCrmInvalidJsonUserInfo(\sprintf( |
| 189 | '%s::%s is missing some information to parse from, uuid was %s', |
| 190 | __CLASS__, |
| 191 | __METHOD__, |
| 192 | $a['hashcode'] |
| 193 | )); |
| 194 | } |
| 195 | return new self( |
| 196 | $a['hashcode'], |
| 197 | $a['email'] |
| 198 | ); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Serialize this object to an array |
| 203 | * |
| 204 | * @return array |
| 205 | */ |
| 206 | public function jsonSerialize() |
| 207 | { |
| 208 | return [ |
| 209 | 'hashcode' => $this->phashcode, |
| 210 | 'email' => $this->pemail |
| 211 | ]; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Save current customer to |
| 216 | * the local database and on crm |
| 217 | * |
| 218 | * @return boolean true if the operation succeeded false otherwise |
| 219 | * @throws LDC\LDCCRM\Exceptions\CrmException If a critical error occured. |
| 220 | * @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs. |
| 221 | * Possibles are : - validationFailed, If provided data is wrong(CRM side). |
| 222 | * - modelNotFound, If this customer hashcode could not be found. |
| 223 | */ |
| 224 | public function save(): bool |
| 225 | { |
| 226 | return WPCrm::saveUser(); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Delete current customer to |
| 231 | * the local database and on crm |
| 232 | * |
| 233 | * @return boolean true if the operation succeeded false otherwise |
| 234 | * @throws LDC\LDCCRM\Exceptions\CrmException If a critical error occured. |
| 235 | * @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs. |
| 236 | * Possibles are : - modelNotFound, If this customer hashcode could not be found. |
| 237 | */ |
| 238 | public function delete(): bool |
| 239 | { |
| 240 | return WPCrm::deleteUser(); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Toggle the current user optin value |
| 245 | * on the current user and the crm |
| 246 | * |
| 247 | * @return boolean true if the operation succeeded |
| 248 | * @throws LDC\LDCCRM\Exceptions\CrmException If a critical error occured. |
| 249 | * @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs. |
| 250 | * Possibles are : - modelNotFound, If this customer hashcode could not be found. |
| 251 | */ |
| 252 | public function toggleOptin(): bool |
| 253 | { |
| 254 | return WPCrm::toggleUserOptin(); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Activate the account in the crm |
| 259 | * |
| 260 | * @return boolean true if succeeded |
| 261 | * @throws \LDC\LDCCRM\Exceptions\ValidationException If a customer's hashcode could not be validated. |
| 262 | * @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response. |
| 263 | * @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs. |
| 264 | * Possibles are : - modelNotFound, If this customer hashcode could not be found. |
| 265 | * - customerAlreadyActive, It the customer is already activated. |
| 266 | */ |
| 267 | public function activate(): bool |
| 268 | { |
| 269 | return WPCrm::activateUser(); |
| 270 | } |
| 271 | } |