GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b21c26...dc2a23 )
by
unknown
21s queued 17s
created

Lead::validate()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 8.4444
c 0
b 0
f 0
cc 8
nc 6
nop 0
1
<?php
2
3
namespace LPTracker\models;
4
5
use LPTracker\exceptions\LPTrackerSDKException;
6
7
/**
8
 * Class Lead
9
 * @package LPTracker\models
10
 */
11
class Lead extends Model
12
{
13
14
    /**
15
     * @var integer
16
     */
17
    protected $id;
18
19
    /**
20
     * @var integer
21
     */
22
    protected $contactId;
23
24
    /**
25
     * @var string
26
     */
27
    protected $name;
28
29
    /**
30
     * @var integer
31
     */
32
    protected $funnelId;
33
34
    /**
35
     * @var View
36
     */
37
    protected $view;
38
39
    /**
40
     * @var integer
41
     */
42
    protected $ownerId;
43
44
    /**
45
     * @var \DateTime
46
     */
47
    protected $createdAt;
48
49
    /**
50
     * @var Payment[]
51
     */
52
    protected $payments = [];
53
54
    /**
55
     * @var Custom[]
56
     */
57
    protected $customs = [];
58
59
    /**
60
     * @var array
61
     */
62
    protected $options = [];
63
64
65
    /**
66
     * Lead constructor.
67
     *
68
     * @param array $leadData
69
     */
70
    public function __construct(array $leadData = [])
71
    {
72
        if ( ! empty($leadData['id'])) {
73
            $this->id = intval($leadData['id']);
74
        }
75
        if ( ! empty($leadData['contact_id'])) {
76
            $this->contactId = intval($leadData['contact_id']);
77
        }
78
        if ( ! empty($leadData['name'])) {
79
            $this->name = $leadData['name'];
80
        }
81
        if ( ! empty($leadData['funnel'])) {
82
            $this->funnelId = intval($leadData['funnel']);
83
        }
84
        if ( ! empty($leadData['view'])) {
85
            $this->view = new View($leadData['view']);
86
        }
87
        if ( ! empty($leadData['owner'])) {
88
            $this->ownerId = intval($leadData['owner']);
89
        }
90
        if ( ! empty($leadData['payments']) && is_array($leadData['payments'])) {
91
            foreach ($leadData['payments'] as $paymentData) {
92
                $paymentModel = new Payment($paymentData);
93
                $this->addPayment($paymentModel);
94
            }
95
        }
96
        if ( ! empty($leadData['custom']) && is_array($leadData['custom'])) {
97
            foreach ($leadData['custom'] as $customData) {
98
                $customModel = new Custom($customData, $this->id);
99
                $this->addCustom($customModel);
100
            }
101
        }
102
        if ( ! empty($leadData['lead_date'])) {
103
            $date = \DateTime::createFromFormat('d.m.Y H:i', $leadData['lead_date']);
104
            $this->setCreatedAt($date);
105
        }
106
        if ( ! empty($leadData['deal_date'])) {
107
            $this->options['deal_date'] = $leadData['deal_date'];
108
        }
109
        if ( ! empty($leadData['params']) && is_array($leadData['params'])) {
110
            $this->options['params'] = $leadData['params'];
111
        }
112
    }
113
114
115
    /**
116
     * @param bool $toSave
117
     *
118
     * @return array
119
     */
120
    public function toArray($toSave = false)
121
    {
122
        $result = [
123
            'contact_id' => $this->contactId,
124
        ];
125
        if ( ! empty($this->id)) {
126
            $result['id'] = $this->getId();
127
        }
128
        if ( ! empty($this->name)) {
129
            $result['name'] = $this->getName();
130
        }
131
        if ( ! empty($this->funnelId)) {
132
            $result['funnel'] = $this->getFunnelId();
133
        }
134
        if ( ! empty($this->ownerId)) {
135
            $result['owner'] = $this->getOwnerId();
136
        }
137
        if ( ! empty($this->createdAt)) {
138
            $result['lead_date'] = $this->getCreatedAt()->format('d.m.Y H:i');
139
        }
140
        if ( ! empty($this->view)) {
141
            $result['view'] = $this->view->toArray();
142
        }
143
        foreach ($this->getPayments() as $payment) {
144
            $result['payments'][] = $payment->toArray();
145
        }
146
        foreach ($this->getCustoms() as $custom) {
147
            if ($toSave) {
148
                $result['custom'][$custom->getId()] = $custom->getValue();
149
            } else {
150
                $result['custom'][] = $custom->toArray();
151
            }
152
        }
153
        foreach ($this->options as $key => $value) {
154
            $result[$key] = $value;
155
        }
156
157
        return $result;
158
    }
159
160
161
    /**
162
     * @return bool
163
     * @throws LPTrackerSDKException
164
     */
165
    public function validate()
166
    {
167
        if (empty($this->contactId)) {
168
            throw new LPTrackerSDKException('Contact ID is required');
169
        }
170
        if (intval($this->contactId) <= 0) {
171
            throw new LPTrackerSDKException('Invalid contact id');
172
        }
173
        if ( ! empty($this->funnelId) && intval($this->funnelId) <= 0) {
174
            throw new LPTrackerSDKException('Invalid funnel ID');
175
        }
176
        if ( ! empty($this->ownerId) && intval($this->ownerId) < 0) {
177
            throw new LPTrackerSDKException('Invalid owner ID');
178
        }
179
        foreach ($this->getPayments() as $payment) {
180
            $payment->validate();
181
        }
182
183
        return true;
184
    }
185
186
187
    /**
188
     * @return int
189
     */
190
    public function getId()
191
    {
192
        return intval($this->id);
193
    }
194
195
196
    /**
197
     * @return int
198
     */
199
    public function getContactId()
200
    {
201
        return intval($this->contactId);
202
    }
203
204
205
    /**
206
     * @return string
207
     */
208
    public function getName()
209
    {
210
        return $this->name;
211
    }
212
213
214
    /**
215
     * @param string $name
216
     *
217
     * @return $this
218
     */
219
    public function setName($name)
220
    {
221
        $this->name = $name;
222
223
        return $this;
224
    }
225
226
227
    /**
228
     * @return int
229
     */
230
    public function getFunnelId()
231
    {
232
        return $this->funnelId;
233
    }
234
235
236
    /**
237
     * @param int $funnelId
238
     *
239
     * @return $this
240
     */
241
    public function setFunnelId($funnelId)
242
    {
243
        $this->funnelId = intval($funnelId);
244
245
        return $this;
246
    }
247
248
249
    /**
250
     * @return View
251
     */
252
    public function getView()
253
    {
254
        return $this->view;
255
    }
256
257
258
    /**
259
     * @param View $view
260
     *
261
     * @return $this
262
     */
263
    public function setView(View $view)
264
    {
265
        $this->view = $view;
266
267
        return $this;
268
    }
269
270
271
    /**
272
     * @return Payment[]
273
     */
274
    public function getPayments()
275
    {
276
        return $this->payments;
277
    }
278
279
280
    /**
281
     * @param array $payments
282
     *
283
     * @return $this
284
     */
285
    public function setPayments(array $payments)
286
    {
287
        $this->payments = $payments;
288
289
        return $this;
290
    }
291
292
293
    /**
294
     * @param Payment $payment
295
     *
296
     * @return $this
297
     */
298
    public function addPayment(Payment $payment)
299
    {
300
        $this->payments[] = $payment;
301
302
        return $this;
303
    }
304
305
306
    /**
307
     * @return int
308
     */
309
    public function getOwnerId()
310
    {
311
        return $this->ownerId;
312
    }
313
314
315
    /**
316
     * @param int $ownerId
317
     *
318
     * @return $this
319
     */
320
    public function setOwnerId($ownerId)
321
    {
322
        $this->ownerId = intval($ownerId);
323
324
        return $this;
325
    }
326
327
328
    /**
329
     * @return Custom[]
330
     */
331
    public function getCustoms()
332
    {
333
        return $this->customs;
334
    }
335
336
337
    /**
338
     * @param Custom[] $customs
339
     *
340
     * @return $this
341
     */
342
    public function setCustoms(array $customs)
343
    {
344
        $this->customs = $customs;
345
346
        return $this;
347
    }
348
349
350
    /**
351
     * @param Custom $custom
352
     *
353
     * @return $this
354
     */
355
    public function addCustom(Custom $custom)
356
    {
357
        $this->customs[] = $custom;
358
359
        return $this;
360
    }
361
362
363
    /**
364
     * @return \DateTime
365
     */
366
    public function getCreatedAt()
367
    {
368
        return $this->createdAt;
369
    }
370
371
372
    /**
373
     * @param \DateTime $createdAt
374
     *
375
     * @return $this
376
     */
377
    public function setCreatedAt($createdAt)
378
    {
379
        $this->createdAt = $createdAt;
380
381
        return $this;
382
    }
383
}