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