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