Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.33% covered (success)
93.33%
56 / 60
71.43% covered (warning)
71.43%
5 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ArcadiaMailDefault
93.33% covered (success)
93.33%
56 / 60
71.43% covered (warning)
71.43%
5 / 7
21.13
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 __get
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 __set
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 fill
75.00% covered (warning)
75.00%
6 / 8
0.00% covered (danger)
0.00%
0 / 1
6.56
 validateAllValues
80.00% covered (warning)
80.00%
8 / 10
0.00% covered (danger)
0.00%
0 / 1
3.07
 send
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 jsonSerialize
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace LDC\LDCCRM\Models;
4
5use LDC\LDCCRM\Enums\ApiStatus;
6use LDC\LDCCRM\Enums\ArcadiaOrderType;
7use LDC\LDCCRM\Utils;
8
9/**
10 * This class represents the customer info with
11 * the account validation status
12 *
13 * @property string|null $email Must be a rfc valid email on a valid domain name
14 * @property string|null $firstname The account first name
15 * @property string|null $lastname The account last name
16 * @property string|null $phone The account phone
17 * @property string|null $address The account address (street number, name, build, flat number)
18 * @property string|null $address2 The account phone (address complement)
19 * @property string|null $city The account city name
20 * @property string|null $postcode The account city postcode
21 *
22 * @property integer|null $operation The arcadia operation code
23 * @property integer|null $support_code The arcadia support code
24 * @property string|null  $label_type_support The arcadia type support label
25 * @property string|null  $order_date The arcadia order date  'yyyy-mm-dd HH:ii:ss'
26 * @property integer|null $order_num The arcadia order number
27 * @property \LDC\LDCCRM\Enums\ArcadiaOrderType|null $state_order The arcadia order date
28 * @property integer|null $package_unit_weight The arcadia package unit weight
29 *
30 * @category Api Models
31 * @package LDCCrmClient
32 * @subpackage models
33 * @author Jérémy Munsch <jeremy.munsch@ldc.fr>
34 */
35class ArcadiaMailDefault extends ClientModel
36{
37    /**
38     * An RFC Valid Email
39     *
40     * Validate: rfc,dns,spoof,filter
41     *
42     * Required
43     * Min length N/A
44     * Max length 255
45     * Field Type varchar
46     *
47     * @var string|null
48     */
49    private ?string $email = null;
50
51    /**
52     * First name
53     *
54     * Required
55     * Min length 2
56     * Max length 255
57     * Field Type varchar
58     *
59     * @var string|null
60     */
61    private ?string $firstname = null;
62
63    /**
64     * Last name
65     *
66     * Required
67     * Min length 2
68     * Max length 255
69     * Field Type varchar
70     *
71     * @var string|null
72     */
73    private ?string $lastname = null;
74
75    /**
76     * The user phone
77     *
78     * Validate: sometimes,string
79     *
80     * Required
81     * Min length NA
82     * Max length 255
83     * Field Type varchar
84     *
85     * @var string|null
86     */
87    private ?string $phone = null;
88
89    /**
90     * The user address (street number, name, etc)
91     *
92     * Validate: sometimes,string
93     *
94     * Min length NA
95     * Max length 255
96     * Field Type varchar
97     *
98     * @var string|null
99     */
100    private ?string $address = null;
101
102    /**
103     * The user address 2nd part (street number, name, more, etc)
104     *
105     * Validate: sometimes,string
106     *
107     * Min length NA
108     * Max length 255
109     * Field Type varchar
110     *
111     * @var string|null
112     */
113    private ?string $address2 = null;
114
115    /**
116     * The user address city
117     *
118     * Validate: sometimes,string
119     *
120     * Min length NA
121     * Max length 255
122     * Field Type varchar
123     *
124     * @var string|null
125     */
126    private ?string $city = null;
127
128    /**
129     * The user address postcode
130     *
131     * Validate: string
132     *
133     * Required
134     * Min length NA
135     * Max length 255
136     * Field Type varchar
137     *
138     * @var string|null
139     */
140    private ?string $postcode = null;
141
142    /**
143     * The arcadia operation code
144     *
145     * Validate: int
146     *
147     * Required
148     *
149     * @var integer|null
150     */
151    private ?int $operation = null;
152
153    /**
154     * The arcadia support code
155     *
156     * Validate: int
157     *
158     * Required
159     *
160     * @var integer|null
161     */
162    private ?int $support_code = null;
163
164    /**
165     * The arcadia type support label
166     *
167     * Validate: string
168     *
169     * Required
170     *
171     * @var string|null
172     */
173    private ?string $label_type_support = null;
174
175    /**
176     * The arcadia order date
177     *
178     * Validate: date
179     *
180     * Required
181     *
182     * @var string|null
183     */
184    private ?string $order_date = null;
185
186    /**
187     * The arcadia order number
188     *
189     * Validate: numeric,digits:9
190     *
191     * Required
192     *
193     * @var integer|null
194     */
195    private ?int $order_num = null;
196
197    /**
198     * The arcadia state order
199     *
200     * Validate: in ArcadiaOrderType::toValues()
201     *
202     * Required
203     *
204     * @var \LDC\LDCCRM\Enums\ArcadiaOrderType|null
205     */
206    private ?int $state_order = null;
207
208    /**
209     * The arcadia package unit weight
210     *
211     * Validate: integer
212     *
213     * Required
214     *
215     * @var integer|null
216     */
217    private ?int $package_unit_weight = null;
218
219    /**
220     * The crm customer fields
221     *
222     * @var array
223     */
224    protected array $modelFields = [
225        'email',
226        'firstname',
227        'lastname',
228        'phone',
229        'address',
230        'address2',
231        'city',
232        'postcode',
233        'operation',
234        'support_code',
235        'label_type_support',
236        'order_date',
237        'order_num',
238        'state_order',
239        'package_unit_weight',
240    ];
241
242    /**
243     * Validation rules
244     *
245     * @var array
246     */
247    protected static array $validation = [
248        'email'               => 'required|email',
249        'firstname'           => 'required|string|min:2|max:255',
250        'lastname'            => 'required|string|min:2|max:255',
251        'phone'               => 'sometimes|string|min:10|max:255',
252        'address'             => 'sometimes|string|max:255',
253        'address2'            => 'sometimes|string|max:255',
254        'city'                => 'sometimes|string|max:255',
255        'postcode'            => 'sometimes|string|max:255',
256        'operation'           => 'required|int',
257        'support_code'        => 'required|int',
258        'label_type_support'  => 'required|string|min:2|max:255',
259        'order_date'          => 'required|date_format:Y/m/d H:i:s',
260        'order_num'           => 'required|int',
261        'state_order'         => ['required'],
262        'package_unit_weight' => 'required|int'
263    ];
264
265    /**
266     * ArcadiaMailDefault
267     *
268     * @param array $properties
269     * @throws \LDC\LDCCRM\Exceptions\ValidationException If a field could not be validated.
270     */
271    public function __construct(array $properties = [])
272    {
273        parent::__construct();
274        $this->fill($properties);
275        return $this;
276    }
277
278    /**
279     * Generic accessor
280     *
281     * @param string $field
282     * @return mixed
283     */
284    public function __get(string $field)
285    {
286        $props = \array_diff(
287            $this->modelFields,
288            $this->invisibleFields
289        );
290        if (in_array($field, $props)) {
291            if ($field === 'state_order' and !\is_object($this->{$field})) {
292                return ArcadiaOrderType::make($this->{$field});
293            }
294        }
295        return parent::__get($field);
296    }
297
298    /**
299     * Set prop
300     *
301     * @param string $field
302     * @param mixed  $value
303     * @return void
304     * @throws \TypeError If the field has an incorrect value.
305     * @throws \LDC\LDCCRM\Exceptions\AccessError If trying to set a value on a unknown property.
306     */
307    public function __set(string $field, mixed $value): void
308    {
309        if ($field === 'state_order') {
310            $value = (\is_object($value) and
311                get_class($value) === ArcadiaOrderType::class) ? $value->value() : $value;
312        }
313        parent::__set($field, $value);
314    }
315
316    /**
317     * Fill data into this model
318     * using validation rules
319     *
320     * @param array $properties
321     * @return void
322     * @throws \LDC\LDCCRM\Exceptions\ValidationException If a field could not be validated.
323     */
324    public function fill(array $properties)
325    {
326        if (isset($properties['state_order'])) {
327            try {
328                $properties['state_order'] = (\is_object($properties['state_order']) and
329                    get_class($properties['state_order']) === ArcadiaOrderType::class) ?
330                    $properties['state_order']->value() : (ctype_digit($properties['state_order']) ?
331                        (int)$properties['state_order'] : $properties['state_order']);
332            } catch (\TypeError | \BadMethodCallException) {
333                // * Cast anyway
334                $properties['state_order'] = (int)$properties['state_order'];
335            }
336        }
337        parent::fill($properties);
338    }
339
340    /**
341     * Validate all model properties
342     *
343     * @param array $ignore
344     * @return array the validated values
345     * @throws ValidationException If a field could not be validated.
346     */
347    protected function validateAllValues(array $ignore = []): array
348    {
349        $out = [];
350        if (!\in_array('state_order', $ignore)) {
351            try {
352                $this->validateValue(
353                    'state_order',
354                    ArcadiaOrderType::make(Utils::getRawProperty($this, 'state_order'))
355                );
356            } catch (\TypeError | \BadMethodCallException) {
357                // Use this to throw validation error.
358                $this->validateValue('state_order', Utils::getRawProperty($this, 'state_order'));
359            }
360            $out['state_order'] = Utils::getRawProperty($this, 'state_order');
361        }
362        return array_merge($out, parent::validateAllValues(array_merge(['state_order'], $ignore)));
363    }
364
365    // * PUBLIC METHODS
366
367    /**
368     * Send The actual mail
369     *
370     * @return boolean true if succeeded
371     * @throws ValidationException If a field could not be validated.
372     * @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
373     * @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
374     * Possibles are : - validationFailed, If provided data is wrong(CRM side).
375     */
376    public function send(): bool
377    {
378        $this->validateAllValues();
379        static::setUpApiInterface();
380        $request  = Utils::getResquest('apiV4.arcadia.outgoingMailDefault', [], $this->jsonSerialize());
381        $response = static::$apiInterface->call($request);
382        if ($status = $response->status->equals(ApiStatus::OK)) {
383            $this->updatedValues = [];
384        }
385        $this->isNew = !$status;
386        return $status;
387    }
388
389    /**
390     * Serialize this object to an array
391     *
392     * @return array
393     */
394    public function jsonSerialize()
395    {
396        return [
397            'deliver'             => [
398                'email'     => $this->email,
399                'firstname' => $this->firstname,
400                'lastname'  => $this->lastname,
401                'phone'     => $this->phone,
402                'address'   => $this->address,
403                'address2'  => $this->address2,
404                'city'      => $this->city,
405                'postcode'  => $this->postcode,
406            ],
407            'operation'           => $this->operation,
408            'support_code'        => $this->support_code,
409            'label_type_support'  => $this->label_type_support,
410            'order_date'          => $this->order_date,
411            'order_num'           => $this->order_num,
412            'state_order'         => $this->state_order,
413            'package_unit_weight' => $this->package_unit_weight
414        ];
415    }
416}