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 ( 17cb6d...c6383e )
by
unknown
01:37
created

LPTracker::saveLead()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 18
Ratio 94.74 %

Importance

Changes 0
Metric Value
dl 18
loc 19
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 1
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()->getId())) {
542
            $lead->setView($this->saveView($lead->getView()));
543
        }
544
545
        $data = $lead->toArray(true);
546
        if (isset($options['callback'])) {
547
            $data['callback'] = $options['callback'] ? true : false;
548
        }
549
        if (isset($leadData['view_id'])) {
550
            $data['view_id'] = intval($leadData['view_id']);
551
        }
552
553
        $response = LPTrackerRequest::sendRequest('/lead', $data, 'POST', $this->token, $this->address);
554
555
        $resultLead = new Lead($response);
556
557
        return $resultLead;
558
    }
559
560
561
    /**
562
     * @param $lead
563
     *
564
     * @return Lead
565
     * @throws LPTrackerSDKException
566
     */
567 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...
568
    {
569
        if ($lead instanceof Lead) {
570
            $lead = $lead->getId();
571
        } else {
572
            $lead = intval($lead);
573
        }
574
575
        if ($lead <= 0) {
576
            throw new LPTrackerSDKException('Invalid lead ID');
577
        }
578
579
        $url = '/lead/'.$lead;
580
581
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
582
583
        $resultLead = new Lead($response);
584
585
        return $resultLead;
586
    }
587
588
589
    /**
590
     * @param Lead $lead
591
     *
592
     * @return Lead
593
     * @throws LPTrackerSDKException
594
     */
595 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...
596
    {
597
        if ( ! $lead->validate()) {
598
            throw new LPTrackerSDKException('Invalid lead');
599
        }
600
601
        if ($lead->getId() > 0) {
602
            $url = '/lead/'.$lead->getId();
603
604
            $response = LPTrackerRequest::sendRequest($url, $lead->toArray(true), 'PUT', $this->token, $this->address);
605
        } else {
606
            $response = LPTrackerRequest::sendRequest('/lead', $lead->toArray(true), 'POST', $this->token,
607
                $this->address);
608
        }
609
610
        $resultLead = new Lead($response);
611
612
        return $resultLead;
613
    }
614
615
616
    /**
617
     * @param       $leadId
618
     * @param array $leadData
619
     *
620
     * @return Lead
621
     * @throws LPTrackerSDKException
622
     */
623
    public function editLead($leadId, array $leadData = [])
624
    {
625
        $leadData['id'] = $leadId;
626
627
        $lead = new Lead($leadData);
628
        $lead->validate();
629
630
        return $this->saveLead($lead);
631
    }
632
633
634
    /**
635
     * @param        $lead
636
     * @param string $category
637
     * @param string $purpose
638
     * @param float  $sum
639
     *
640
     * @return Lead
641
     * @throws LPTrackerSDKException
642
     */
643
    public function addLeadPayment($lead, $category, $purpose, $sum)
644
    {
645
        if ($lead instanceof Lead) {
646
            $lead = $lead->getId();
647
        } else {
648
            $lead = intval($lead);
649
        }
650
651
        if (empty($category)) {
652
            throw new LPTrackerSDKException('Category can not be empty');
653
        }
654
        if (empty($purpose)) {
655
            throw new LPTrackerSDKException('Purpose can not be empty');
656
        }
657
        $sum = floatval($sum);
658
659
        if ($sum <= 0) {
660
            throw new LPTrackerSDKException('Invalid sum');
661
        }
662
663
        if ($lead <= 0) {
664
            throw new LPTrackerSDKException('Invalid lead ID');
665
        }
666
667
        $url = '/lead/'.$lead.'/payment';
668
669
        $data = [
670
            'category' => $category,
671
            'purpose'  => $purpose,
672
            'sum'      => $sum,
673
        ];
674
675
        $response = LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address);
676
677
        $resultLead = new Lead($response);
678
679
        return $resultLead;
680
    }
681
682
683
    /**
684
     * @param $lead
685
     * @param $newFunnelId
686
     *
687
     * @return Lead
688
     * @throws LPTrackerSDKException
689
     */
690 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...
691
    {
692
        if ($lead instanceof Lead) {
693
            $lead = $lead->getId();
694
        }
695
696
        $url = '/lead/'.$lead.'/funnel';
697
698
        $data = [
699
            'funnel' => $newFunnelId
700
        ];
701
702
        $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
703
704
        $resultLead = new Lead($response);
705
706
        return $resultLead;
707
    }
708
709
710
    /**
711
     * @param $lead
712
     *
713
     * @return Comment[]
714
     * @throws exceptions\LPTrackerResponseException
715
     * @throws exceptions\LPTrackerServerException
716
     */
717 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...
718
    {
719
        if ($lead instanceof Lead) {
720
            $lead = $lead->getId();
721
        } else {
722
            $lead = intval($lead);
723
        }
724
725
        $url = '/lead/'.$lead.'/comments';
726
727
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
728
729
        $result = [];
730
        foreach ($response as $commentData) {
731
            $result[] = new Comment($commentData);
732
        }
733
734
        return $result;
735
    }
736
737
738
    /**
739
     * @param $lead
740
     * @param $text
741
     *
742
     * @return Comment
743
     * @throws exceptions\LPTrackerResponseException
744
     * @throws exceptions\LPTrackerServerException
745
     */
746 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...
747
    {
748
        if ($lead instanceof Lead) {
749
            $lead = $lead->getId();
750
        } else {
751
            $lead = intval($lead);
752
        }
753
754
        $url = '/lead/'.$lead.'/comment';
755
756
        $data = [
757
            'text' => $text
758
        ];
759
760
        $response = LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address);
761
762
        $comment = new Comment($response);
763
764
        return $comment;
765
    }
766
767
768
    /**
769
     * @param Custom $custom
770
     *
771
     * @return Custom
772
     * @throws LPTrackerSDKException
773
     */
774
    public function saveLeadCustom(Custom $custom)
775
    {
776
        if ( ! $custom->validate() || empty($custom->getLeadId())) {
777
            throw new LPTrackerSDKException('Invalid custom');
778
        }
779
780
        $url = '/lead/'.$custom->getLeadId().'/custom/'.$custom->getId();
781
782
        $data = [
783
            'value' => $custom->getValue()
784
        ];
785
786
        $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
787
788
        $resultCustom = new Custom($response);
789
790
        return $resultCustom;
791
    }
792
793
794
    /**
795
     * @param $lead
796
     * @param $custom
797
     * @param $newValue
798
     *
799
     * @return Custom
800
     * @throws LPTrackerSDKException
801
     */
802
    public function editLeadCustom($lead, $custom, $newValue)
803
    {
804
        if ($lead instanceof Lead) {
805
            $lead = $lead->getId();
806
        } else {
807
            $lead = intval($lead);
808
        }
809
810
        if ($custom instanceof Custom) {
811
            if (empty($newValue)) {
812
                $newValue = $custom->getValue();
813
            }
814
            $custom = $custom->getId();
815
        } else {
816
            $custom = intval($custom);
817
        }
818
819
        $customModel = new Custom([
820
            'id'      => $custom,
821
            'lead_id' => $lead,
822
            'value'   => $newValue
823
        ]);
824
        $customModel->validate();
825
826
        return $this->saveLeadCustom($customModel);
827
    }
828
}