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 ( 998aed...ca3854 )
by
unknown
02:33
created

LPTracker::searchContacts()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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