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 ( e92006...214e66 )
by Sergey
01:23
created

Lead::getPayments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace LPTracker\models;
4
5
use LPTracker\exceptions\LPTrackerSDKException;
6
7
class Lead extends Model
8
{
9
    /**
10
     * @var integer
11
     */
12
    protected $id;
13
14
    /**
15
     * @var integer
16
     */
17
    protected $contactId;
18
19
    /**
20
     * @var Contact
21
     */
22
    protected $contact;
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
    public function __construct(array $leadData = [])
65
    {
66
        if (!empty($leadData['id'])) {
67
            $this->id = (int) $leadData['id'];
68
        }
69
        if (!empty($leadData['contact_id'])) {
70
            $this->contactId = (int) $leadData['contact_id'];
71
        }
72
        if (!empty($leadData['contact'])) {
73
            $this->contact = new Contact($leadData['contact']);
74
        }
75
        if (!empty($leadData['name'])) {
76
            $this->name = $leadData['name'];
77
        }
78
        if (!empty($leadData['funnel'])) {
79
            $this->funnelId = (int) $leadData['funnel'];
80
        }
81
        if (!empty($leadData['stage_id'])) {
82
            $this->funnelId = (int) $leadData['stage_id'];
83
        }
84
        if (!empty($leadData['view'])) {
85
            $this->view = new View($leadData['view']);
86
        }
87
        if (isset($leadData['owner'])) {
88
            $this->ownerId = (int) $leadData['owner'];
89
        }
90
        if (isset($leadData['owner_id'])) {
91
            $this->ownerId = (int) $leadData['owner_id'];
92
        }
93
        if (!empty($leadData['payments']) && is_array($leadData['payments'])) {
94
            foreach ($leadData['payments'] as $paymentData) {
95
                $paymentModel = new Payment($paymentData);
96
                $this->addPayment($paymentModel);
97
            }
98
        }
99
        if (!empty($leadData['custom']) && is_array($leadData['custom'])) {
100
            foreach ($leadData['custom'] as $customData) {
101
                $customModel = new Custom($customData, $this->id);
102
                $this->addCustom($customModel);
103
            }
104
        }
105 View Code Duplication
        if (!empty($leadData['lead_date'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
            $date = \DateTime::createFromFormat('d.m.Y H:i', $leadData['lead_date']);
107
            $this->setCreatedAt($date);
0 ignored issues
show
Security Bug introduced by
It seems like $date defined by \DateTime::createFromFor...$leadData['lead_date']) on line 106 can also be of type false; however, LPTracker\models\Lead::setCreatedAt() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
108
        }
109 View Code Duplication
        if (!empty($leadData['created_at'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
            $date = \DateTime::createFromFormat('d.m.Y H:i', $leadData['created_at']);
111
            $this->setCreatedAt($date);
0 ignored issues
show
Security Bug introduced by
It seems like $date defined by \DateTime::createFromFor...leadData['created_at']) on line 110 can also be of type false; however, LPTracker\models\Lead::setCreatedAt() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
112
        }
113
        if (!empty($leadData['deal_date'])) {
114
            $this->options['deal_date'] = $leadData['deal_date'];
115
        }
116
        if (!empty($leadData['params']) && is_array($leadData['params'])) {
117
            $this->options['params'] = $leadData['params'];
118
        }
119
    }
120
121
    /**
122
     * @param bool $toSave
123
     * @return array
124
     */
125
    public function toArray($toSave = false)
126
    {
127
        $result = [
128
            'contact_id' => $this->contactId,
129
        ];
130
        if (!empty($this->contact)) {
131
            $result['contact'] = $this->contact->toArray();
132
        }
133
        if (!empty($this->id)) {
134
            $result['id'] = $this->getId();
135
        }
136
        if (!empty($this->name)) {
137
            $result['name'] = $this->getName();
138
        }
139
        if (!empty($this->funnelId)) {
140
            $result['funnel'] = $this->getFunnelId();
141
            $result['stage_id'] = $this->getFunnelId();
142
        }
143
        if (!empty($this->ownerId)) {
144
            $result['owner'] = $this->getOwnerId();
145
            $result['owner_id'] = $this->getOwnerId();
146
        }
147
        if (!empty($this->createdAt)) {
148
            $result['lead_date'] = $this->getCreatedAt()->format('d.m.Y H:i');
149
        }
150
        if (!empty($this->view)) {
151
            $result['view'] = $this->view->toArray();
152
        }
153
        foreach ($this->getPayments() as $payment) {
154
            $result['payments'][] = $payment->toArray();
155
        }
156
        foreach ($this->getCustoms() as $custom) {
157
            if ($toSave) {
158
                $result['custom'][$custom->getId()] = $custom->getValue();
159
            } else {
160
                $result['custom'][] = $custom->toArray();
161
            }
162
        }
163
        foreach ($this->options as $key => $value) {
164
            $result[$key] = $value;
165
        }
166
        return $result;
167
    }
168
169
    /**
170
     * @return bool
171
     * @throws LPTrackerSDKException
172
     */
173
    public function validate()
174
    {
175
        if (empty($this->contactId)) {
176
            throw new LPTrackerSDKException('Contact ID is required');
177
        }
178
179
        if ((int) $this->contactId <= 0) {
180
            throw new LPTrackerSDKException('Invalid contact id');
181
        }
182
183
        if (!empty($this->funnelId) && (int) $this->funnelId <= 0) {
184
            throw new LPTrackerSDKException('Invalid funnel ID');
185
        }
186
187
        if (!empty($this->ownerId) && (int) $this->ownerId < 0) {
188
            throw new LPTrackerSDKException('Invalid owner ID');
189
        }
190
191
        foreach ($this->getPayments() as $payment) {
192
            $payment->validate();
193
        }
194
        return true;
195
    }
196
197
    /**
198
     * @return int
199
     */
200
    public function getId()
201
    {
202
        return (int) $this->id;
203
    }
204
205
    /**
206
     * @return int
207
     */
208
    public function getContactId()
209
    {
210
        return (int) $this->contactId;
211
    }
212
213
    /**
214
     * @return Contact
215
     */
216
    public function getContact()
217
    {
218
        return $this->contact;
219
    }
220
221
    /**
222
     * @return string
223
     */
224
    public function getName()
225
    {
226
        return $this->name;
227
    }
228
229
    /**
230
     * @param string $name
231
     * @return $this
232
     */
233
    public function setName($name)
234
    {
235
        $this->name = $name;
236
        return $this;
237
    }
238
239
    /**
240
     * @return int
241
     */
242
    public function getFunnelId()
243
    {
244
        return $this->funnelId;
245
    }
246
247
    /**
248
     * @param int $funnelId
249
     * @return $this
250
     */
251
    public function setFunnelId($funnelId)
252
    {
253
        $this->funnelId = (int) $funnelId;
254
        return $this;
255
    }
256
257
    /**
258
     * @return View
259
     */
260
    public function getView()
261
    {
262
        return $this->view;
263
    }
264
265
    /**
266
     * @param View $view
267
     * @return $this
268
     */
269
    public function setView(View $view)
270
    {
271
        $this->view = $view;
272
        return $this;
273
    }
274
275
    /**
276
     * @return Payment[]
277
     */
278
    public function getPayments()
279
    {
280
        return $this->payments;
281
    }
282
283
    /**
284
     * @param array $payments
285
     * @return $this
286
     */
287
    public function setPayments(array $payments)
288
    {
289
        $this->payments = $payments;
290
        return $this;
291
    }
292
293
    /**
294
     * @param Payment $payment
295
     * @return $this
296
     */
297
    public function addPayment(Payment $payment)
298
    {
299
        $this->payments[] = $payment;
300
        return $this;
301
    }
302
303
    /**
304
     * @return int
305
     */
306
    public function getOwnerId()
307
    {
308
        return $this->ownerId;
309
    }
310
311
    /**
312
     * @param int $ownerId
313
     * @return $this
314
     */
315
    public function setOwnerId($ownerId)
316
    {
317
        $this->ownerId = (int) $ownerId;
318
        return $this;
319
    }
320
321
    /**
322
     * @return Custom[]
323
     */
324
    public function getCustoms()
325
    {
326
        return $this->customs;
327
    }
328
329
    /**
330
     * @param Custom[] $customs
331
     * @return $this
332
     */
333
    public function setCustoms(array $customs)
334
    {
335
        $this->customs = $customs;
336
        return $this;
337
    }
338
339
    /**
340
     * @param Custom $custom
341
     * @return $this
342
     */
343
    public function addCustom(Custom $custom)
344
    {
345
        $this->customs[] = $custom;
346
        return $this;
347
    }
348
349
    /**
350
     * @return \DateTime
351
     */
352
    public function getCreatedAt()
353
    {
354
        return $this->createdAt;
355
    }
356
357
    /**
358
     * @param \DateTime $createdAt
359
     * @return $this
360
     */
361
    public function setCreatedAt($createdAt)
362
    {
363
        $this->createdAt = $createdAt;
364
        return $this;
365
    }
366
}
367