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