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 ( 4506f4...eceab6 )
by
unknown
02:11
created

LPTracker::editLeadCustom()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 17
nc 6
nop 3
1
<?php
2
3
namespace LPTracker;
4
5
use LPTracker\models\Comment;
6
use LPTracker\models\Contact;
7
use LPTracker\models\ContactField;
8
use LPTracker\models\Custom;
9
use LPTracker\models\Lead;
10
use LPTracker\models\Project;
11
use LPTracker\exceptions\LPTrackerSDKException;
12
use LPTracker\authentication\AccessToken;
13
use LPTracker\models\View;
14
15
/**
16
 * Class LPTracker
17
 * @package LPTracker
18
 */
19
class LPTracker extends LPTrackerBase
20
{
21
22
    /**
23
     * @param        $login
24
     * @param        $password
25
     * @param string $serviceName
26
     *
27
     * @return AccessToken
28
     * @throws LPTrackerSDKException
29
     */
30
    public function login($login, $password, $serviceName = '')
31
    {
32
        if (empty($login)) {
33
            throw new LPTrackerSDKException('Login is empty');
34
        }
35
        if (empty($password)) {
36
            throw new LPTrackerSDKException('Password is empty');
37
        }
38
        if (empty($serviceName)) {
39
            $serviceName = LPTrackerBase::DEFAULT_SERVICE_NAME;
40
        }
41
42
        $response = LPTrackerRequest::sendRequest('/login', [
43
            'login'    => $login,
44
            'password' => $password,
45
            'service'  => $serviceName,
46
            'version'  => LPTrackerBase::VERSION
47
        ], 'POST', null, $this->address);
48
49
        $accessToken = new AccessToken($response['token']);
50
51
        return $accessToken;
52
    }
53
54
55
    /**
56
     * @throws exceptions\LPTrackerResponseException
57
     * @throws exceptions\LPTrackerServerException
58
     */
59
    public function logout()
60
    {
61
        LPTrackerRequest::sendRequest('/logout', [], 'POST', $this->token, $this->address);
62
    }
63
64
65
    /**
66
     * @return Project[]
67
     * @throws exceptions\LPTrackerResponseException
68
     * @throws exceptions\LPTrackerServerException
69
     */
70 View Code Duplication
    public function getProjectList()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
71
    {
72
        $response = LPTrackerRequest::sendRequest('/projects', [], 'GET', $this->token, $this->address);
73
74
        $result = [];
75
        foreach ($response as $projectData) {
76
            $result[] = new Project($projectData);
77
        }
78
79
        return $result;
80
    }
81
82
83
    /**
84
     * @param $id
85
     *
86
     * @return Project
87
     * @throws exceptions\LPTrackerResponseException
88
     * @throws exceptions\LPTrackerServerException
89
     */
90 View Code Duplication
    public function getProject($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
91
    {
92
        $url = '/project/'.$id;
93
94
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
95
96
        $project = new Project($response);
97
98
        return $project;
99
    }
100
101
102
    /**
103
     * @param $project
104
     *
105
     * @return Custom[]
106
     * @throws exceptions\LPTrackerResponseException
107
     * @throws exceptions\LPTrackerServerException
108
     */
109 View Code Duplication
    public function getProjectCustoms($project)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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
    {
111
        if ($project instanceof Project) {
112
            $project = $project->getId();
113
        } else {
114
            $project = intval($project);
115
        }
116
117
        $url = '/project/'.$project.'/customs';
118
119
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
120
121
        $result = [];
122
        foreach ($response as $customData) {
123
            $result[] = new Custom($customData);
124
        }
125
126
        return $result;
127
    }
128
129
130
    /**
131
     * @param $project
132
     *
133
     * @return ContactField[]
134
     * @throws exceptions\LPTrackerResponseException
135
     * @throws exceptions\LPTrackerServerException
136
     */
137 View Code Duplication
    public function getProjectFields($project)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
138
    {
139
        if ($project instanceof Project) {
140
            $project = $project->getId();
141
        } else {
142
            $project = intval($project);
143
        }
144
145
        $url = '/project/'.$project.'/fields';
146
147
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
148
149
        $result = [];
150
        foreach ($response as $customData) {
151
            $result[] = new ContactField($customData);
152
        }
153
154
        return $result;
155
    }
156
157
158
    /**
159
     * @param       $project
160
     * @param array $details
161
     * @param array $contactData
162
     * @param array $fields
163
     *
164
     * @return Contact
165
     * @throws LPTrackerSDKException
166
     */
167
    public function createContact(
168
        $project,
169
        array $details,
170
        array $contactData = [],
171
        array $fields = []
172
    ) {
173
        if (empty($details)) {
174
            throw new LPTrackerSDKException('Contact details can not be empty');
175
        }
176
177
        if ($project instanceof Project) {
178
            $project = $project->getId();
179
        } else {
180
            $project = intval($project);
181
        }
182
183
        $contactData['project_id'] = $project;
184
        $contactData['details'] = $details;
185
186
        $fieldsArr = [];
187
        foreach ($fields as $fieldId => $fieldValue) {
188
            if ($fieldValue instanceof ContactField) {
189
                $fieldId = $fieldValue->getId();
190
                $fieldValue = $fieldValue->getValue();
191
            }
192
193
            $fieldsArr[$fieldId] = $fieldValue;
194
        }
195
196
        $contact = new Contact($contactData);
197
        $contact->validate();
198
199
        $data = $contact->toArray();
200
        $data['fields'] = $fieldsArr;
201
202
        $response = LPTrackerRequest::sendRequest('/contact', $data, 'POST', $this->token, $this->address);
203
204
        $resultContact = new Contact($response);
205
206
        return $resultContact;
207
    }
208
209
210
    /**
211
     * @param $contact
212
     *
213
     * @return Contact
214
     * @throws LPTrackerSDKException
215
     */
216 View Code Duplication
    public function getContact($contact)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
217
    {
218
        if ($contact instanceof Contact) {
219
            $contact = $contact->getId();
220
        } else {
221
            $contact = intval($contact);
222
        }
223
224
        if ($contact <= 0) {
225
            throw new LPTrackerSDKException('Invalid contact ID');
226
        }
227
228
        $url = '/contact/'.$contact;
229
230
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
231
232
        $resultContact = new Contact($response);
233
234
        return $resultContact;
235
    }
236
237
238
    /**
239
     * @param Contact $contact
240
     *
241
     * @return Contact
242
     * @throws LPTrackerSDKException
243
     */
244
    public function saveContact(Contact $contact)
245
    {
246
        if ( ! $contact->validate()) {
247
            throw new LPTrackerSDKException('Invalid contact');
248
        }
249
250
        $data = $contact->toArray();
251
        if ( ! empty($data['fields'])) {
252
            $fields = [];
253
            foreach ($data['fields'] as $field) {
254
                $fields[$field['id']] = $field['value'];
255
            }
256
            $data['fields'] = $fields;
257
        }
258
259
        if ($contact->getId() > 0) {
260
            $url = '/contact/'.$contact->getId();
261
262
            $response = LPTrackerRequest::sendRequest($url, $contact->toArray(), 'PUT', $this->token, $this->address);
263
        } else {
264
            $response = LPTrackerRequest::sendRequest('/contact', $contact->toArray(), 'POST', $this->token,
265
                $this->address);
266
        }
267
268
        $resultContact = new Contact($response);
269
270
        return $resultContact;
271
    }
272
273
274
    /**
275
     * @param       $contactId
276
     * @param array $details
277
     * @param array $contactData
278
     * @param array $fields
279
     *
280
     * @return Contact
281
     * @throws LPTrackerSDKException
282
     */
283
    public function editContact(
284
        $contactId,
285
        array $details,
286
        array $contactData = [],
287
        array $fields = []
288
    ) {
289
        if (empty($details)) {
290
            throw new LPTrackerSDKException('Contact details can not be empty');
291
        }
292
293
        $contactData['id'] = $contactId;
294
        $contactData['details'] = $details;
295
296
        foreach ($fields as $fieldId => $fieldValue) {
297
            if ($fieldValue instanceof ContactField) {
298
                $fieldId = $fieldValue->getId();
299
                $fieldValue = $fieldValue->getValue();
300
            }
301
302
            $contactData['fields'][] = [
303
                'id'    => $fieldId,
304
                'value' => $fieldValue
305
            ];
306
        }
307
308
        $contact = new Contact($contactData);
309
        $contact->validate();
310
311
        return $this->saveContact($contact);
312
    }
313
314
315
    /**
316
     * @param       $project
317
     * @param array $searchOptions
318
     *
319
     * @return array
320
     * @throws LPTrackerSDKException
321
     */
322
    public function searchContacts($project, array $searchOptions = [])
323
    {
324
        if ($project instanceof Project) {
325
            $project = $project->getId();
326
        }
327
        $project = intval($project);
328
        if ($project <= 0) {
329
            throw new LPTrackerSDKException('Invalid project id');
330
        }
331
332
        $url = '/contact/search?';
333
334
        $data = [
335
            'project_id' => $project
336
        ];
337
        if (isset($searchOptions['email'])) {
338
            $data['email'] = $searchOptions['email'];
339
        }
340
        if (isset($searchOptions['phone'])) {
341
            $data['phone'] = $searchOptions['phone'];
342
        }
343
344
        $url = $url.http_build_query($data);
345
346
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
347
348
        $result = [];
349
        foreach ($response as $contactData) {
350
            $result[] = new Contact($contactData);
351
        }
352
353
        return $result;
354
    }
355
356
357
    /**
358
     * @param $contact
359
     *
360
     * @return array
361
     * @throws LPTrackerSDKException
362
     */
363
    public function contactLeads($contact)
364
    {
365
        if ($contact instanceof Contact) {
366
            $contact = $contact->getId();
367
        }
368
        $contact = intval($contact);
369
        if ($contact <= 0) {
370
            throw new LPTrackerSDKException('Invalid contact id');
371
        }
372
373
        $url = '/contact/'.$contact.'/leads';
374
375
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
376
377
        $result = [];
378
        foreach ($response as $leadData) {
379
            $result[] = new Lead($leadData);
380
        }
381
382
        return $result;
383
    }
384
385
386
    /**
387
     * @param $contact
388
     * @param $field
389
     * @param $newValue
390
     *
391
     * @return ContactField
392
     * @throws exceptions\LPTrackerResponseException
393
     * @throws exceptions\LPTrackerServerException
394
     */
395
    public function updateContactField($contact, $field, $newValue)
396
    {
397
        if ($contact instanceof Contact) {
398
            $contact = $contact->getId();
399
        }
400
        if ($field instanceof ContactField) {
401
            $field = $field->getId();
402
        }
403
404
        $url = '/contact/'.$contact.'/field/'.$field;
405
406
        $data = [
407
            'value' => $newValue
408
        ];
409
410
        $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
411
412
        $contactField = new ContactField($response);
413
414
        return $contactField;
415
    }
416
417
418
    /**
419
     * @param       $project
420
     * @param array $viewData
421
     *
422
     * @return View
423
     * @throws LPTrackerSDKException
424
     */
425
    public function createView($project, array $viewData = [])
426
    {
427
        if ($project instanceof Project) {
428
            $viewData['project_id'] = $project->getId();
429
        } else {
430
            $viewData['project_id'] = intval($project);
431
        }
432
433
        $view = new View($viewData);
434
        if ( ! $view->validate()) {
435
            throw new LPTrackerSDKException('Invalid view data');
436
        }
437
438
        $data = $view->toArray();
439
440
        $response = LPTrackerRequest::sendRequest('/view', $data, 'POST', $this->token, $this->address);
441
442
        $resultView = new View($response);
443
444
        return $resultView;
445
    }
446
447
448
    /**
449
     * @param $view
450
     *
451
     * @return View
452
     * @throws LPTrackerSDKException
453
     */
454 View Code Duplication
    public function getView($view)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
455
    {
456
        if ($view instanceof View) {
457
            $view = $view->getId();
458
        } else {
459
            $view = intval($view);
460
        }
461
462
        if ($view <= 0) {
463
            throw new LPTrackerSDKException('Invalid view ID');
464
        }
465
466
        $url = '/view/'.$view;
467
468
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
469
470
        $resultView = new View($response);
471
472
        return $resultView;
473
    }
474
475
476
    /**
477
     * @param View $view
478
     *
479
     * @return View
480
     * @throws LPTrackerSDKException
481
     */
482 View Code Duplication
    public function saveView(View $view)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
483
    {
484
        if ( ! $view->validate()) {
485
            throw new LPTrackerSDKException('Invalid view');
486
        }
487
488
        if ($view->getId() > 0) {
489
            $url = '/view/'.$view->getId();
490
491
            $response = LPTrackerRequest::sendRequest($url, $view->toArray(), 'PUT', $this->token, $this->address);
492
        } else {
493
            $response = LPTrackerRequest::sendRequest('/view', $view->toArray(), 'POST', $this->token, $this->address);
494
        }
495
496
        $resultView = new View($response);
497
498
        return $resultView;
499
    }
500
501
502
    /**
503
     * @param       $viewId
504
     * @param array $viewData
505
     *
506
     * @return View
507
     * @throws LPTrackerSDKException
508
     */
509
    public function editView($viewId, array $viewData = [])
510
    {
511
        $viewData['id'] = $viewId;
512
513
        $view = new View($viewData);
514
        $view->validate();
515
516
        return $this->saveView($view);
517
    }
518
519
520
    /**
521
     * @param       $contact
522
     * @param array $leadData
523
     * @param array $options
524
     *
525
     * @return Lead
526
     * @throws LPTrackerSDKException
527
     */
528
    public function createLead($contact, array $leadData = [], array $options = [])
529
    {
530
        if ($contact instanceof Contact) {
531
            $leadData['contact_id'] = $contact->getId();
532
        } else {
533
            $leadData['contact_id'] = intval($contact);
534
        }
535
536
        $lead = new Lead($leadData);
537
        if ( ! $lead->validate()) {
538
            throw new LPTrackerSDKException('Invalid lead data');
539
        }
540
541
        if ( ! empty($lead->getView()) && empty($lead->getView()->getId())) {
542
            $contactModel = $this->getContact($contact);
543
            $viewData = $lead->getView()->toArray();
544
            $lead->setView($this->createView($contactModel->getProjectId(), $viewData));
545
        }
546
547
        $data = $lead->toArray(true);
548
        if (isset($options['callback'])) {
549
            $data['callback'] = $options['callback'] ? true : false;
550
        }
551
        if (isset($leadData['view_id'])) {
552
            $data['view_id'] = intval($leadData['view_id']);
553
        } elseif ( ! empty($lead->getView()) && ! empty($lead->getView()->getId())) {
554
            $data['view_id'] = $lead->getView()->getId();
555
        }
556
557
        $response = LPTrackerRequest::sendRequest('/lead', $data, 'POST', $this->token, $this->address);
558
559
        $resultLead = new Lead($response);
560
561
        return $resultLead;
562
    }
563
564
565
    /**
566
     * @param $lead
567
     *
568
     * @return Lead
569
     * @throws LPTrackerSDKException
570
     */
571 View Code Duplication
    public function getLead($lead)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
572
    {
573
        if ($lead instanceof Lead) {
574
            $lead = $lead->getId();
575
        } else {
576
            $lead = intval($lead);
577
        }
578
579
        if ($lead <= 0) {
580
            throw new LPTrackerSDKException('Invalid lead ID');
581
        }
582
583
        $url = '/lead/'.$lead;
584
585
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
586
587
        $resultLead = new Lead($response);
588
589
        return $resultLead;
590
    }
591
592
593
    /**
594
     * @param Lead $lead
595
     *
596
     * @return Lead
597
     * @throws LPTrackerSDKException
598
     */
599 View Code Duplication
    public function saveLead(Lead $lead)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
600
    {
601
        if ( ! $lead->validate()) {
602
            throw new LPTrackerSDKException('Invalid lead');
603
        }
604
605
        if ($lead->getId() > 0) {
606
            $url = '/lead/'.$lead->getId();
607
608
            $response = LPTrackerRequest::sendRequest($url, $lead->toArray(true), 'PUT', $this->token, $this->address);
609
        } else {
610
            $response = LPTrackerRequest::sendRequest('/lead', $lead->toArray(true), 'POST', $this->token,
611
                $this->address);
612
        }
613
614
        $resultLead = new Lead($response);
615
616
        return $resultLead;
617
    }
618
619
620
    /**
621
     * @param       $leadId
622
     * @param array $leadData
623
     *
624
     * @return Lead
625
     * @throws LPTrackerSDKException
626
     */
627
    public function editLead($leadId, array $leadData = [])
628
    {
629
        $leadData['id'] = $leadId;
630
631
        $lead = new Lead($leadData);
632
        $lead->validate();
633
634
        return $this->saveLead($lead);
635
    }
636
637
638
    /**
639
     * @param        $lead
640
     * @param string $category
641
     * @param string $purpose
642
     * @param float  $sum
643
     *
644
     * @return Lead
645
     * @throws LPTrackerSDKException
646
     */
647
    public function addLeadPayment($lead, $category, $purpose, $sum)
648
    {
649
        if ($lead instanceof Lead) {
650
            $lead = $lead->getId();
651
        } else {
652
            $lead = intval($lead);
653
        }
654
655
        if (empty($category)) {
656
            throw new LPTrackerSDKException('Category can not be empty');
657
        }
658
        if (empty($purpose)) {
659
            throw new LPTrackerSDKException('Purpose can not be empty');
660
        }
661
        $sum = floatval($sum);
662
663
        if ($sum <= 0) {
664
            throw new LPTrackerSDKException('Invalid sum');
665
        }
666
667
        if ($lead <= 0) {
668
            throw new LPTrackerSDKException('Invalid lead ID');
669
        }
670
671
        $url = '/lead/'.$lead.'/payment';
672
673
        $data = [
674
            'category' => $category,
675
            'purpose'  => $purpose,
676
            'sum'      => $sum,
677
        ];
678
679
        $response = LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address);
680
681
        $resultLead = new Lead($response);
682
683
        return $resultLead;
684
    }
685
686
687
    /**
688
     * @param $lead
689
     * @param $newFunnelId
690
     *
691
     * @return Lead
692
     * @throws LPTrackerSDKException
693
     */
694 View Code Duplication
    public function changeLeadFunnel($lead, $newFunnelId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
695
    {
696
        if ($lead instanceof Lead) {
697
            $lead = $lead->getId();
698
        }
699
700
        $url = '/lead/'.$lead.'/funnel';
701
702
        $data = [
703
            'funnel' => $newFunnelId
704
        ];
705
706
        $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
707
708
        $resultLead = new Lead($response);
709
710
        return $resultLead;
711
    }
712
713
714
    /**
715
     * @param $lead
716
     *
717
     * @return Comment[]
718
     * @throws exceptions\LPTrackerResponseException
719
     * @throws exceptions\LPTrackerServerException
720
     */
721 View Code Duplication
    public function getLeadComments($lead)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
722
    {
723
        if ($lead instanceof Lead) {
724
            $lead = $lead->getId();
725
        } else {
726
            $lead = intval($lead);
727
        }
728
729
        $url = '/lead/'.$lead.'/comments';
730
731
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
732
733
        $result = [];
734
        foreach ($response as $commentData) {
735
            $result[] = new Comment($commentData);
736
        }
737
738
        return $result;
739
    }
740
741
742
    /**
743
     * @param $lead
744
     * @param $text
745
     *
746
     * @return Comment
747
     * @throws exceptions\LPTrackerResponseException
748
     * @throws exceptions\LPTrackerServerException
749
     */
750 View Code Duplication
    public function addCommentToLead($lead, $text)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
751
    {
752
        if ($lead instanceof Lead) {
753
            $lead = $lead->getId();
754
        } else {
755
            $lead = intval($lead);
756
        }
757
758
        $url = '/lead/'.$lead.'/comment';
759
760
        $data = [
761
            'text' => $text
762
        ];
763
764
        $response = LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address);
765
766
        $comment = new Comment($response);
767
768
        return $comment;
769
    }
770
771
772
    /**
773
     * @param Custom $custom
774
     *
775
     * @return Custom
776
     * @throws LPTrackerSDKException
777
     */
778
    public function saveLeadCustom(Custom $custom)
779
    {
780
        if ( ! $custom->validate() || empty($custom->getLeadId())) {
781
            throw new LPTrackerSDKException('Invalid custom');
782
        }
783
784
        $url = '/lead/'.$custom->getLeadId().'/custom/'.$custom->getId();
785
786
        $data = [
787
            'value' => $custom->getValue()
788
        ];
789
790
        $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
791
792
        $resultCustom = new Custom($response);
793
794
        return $resultCustom;
795
    }
796
797
798
    /**
799
     * @param $lead
800
     * @param $custom
801
     * @param $newValue
802
     *
803
     * @return Custom
804
     * @throws LPTrackerSDKException
805
     */
806
    public function editLeadCustom($lead, $custom, $newValue)
807
    {
808
        if ($lead instanceof Lead) {
809
            $lead = $lead->getId();
810
        } else {
811
            $lead = intval($lead);
812
        }
813
814
        if ($custom instanceof Custom) {
815
            if (empty($newValue)) {
816
                $newValue = $custom->getValue();
817
            }
818
            $custom = $custom->getId();
819
        } else {
820
            $custom = intval($custom);
821
        }
822
823
        $customModel = new Custom([
824
            'id'      => $custom,
825
            'lead_id' => $lead,
826
            'value'   => $newValue
827
        ]);
828
        $customModel->validate();
829
830
        return $this->saveLeadCustom($customModel);
831
    }
832
}