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 ( 2c32a2...b98093 )
by
unknown
03:55
created

LPTracker   F

Complexity

Total Complexity 101

Size/Duplication

Total Lines 919
Duplicated Lines 16.97 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
wmc 101
lcom 1
cbo 12
dl 156
loc 919
rs 1.681
c 0
b 0
f 0

31 Methods

Rating   Name   Duplication   Size   Complexity  
A login() 0 23 4
A logout() 0 4 1
A getProjectList() 11 11 2
A getProject() 10 10 1
A getProjectCustoms() 0 19 3
A getProjectFields() 0 19 3
A setProjectCallbackUrl() 0 11 2
B createContact() 0 41 5
A getContact() 20 20 3
A saveContact() 0 28 5
A editContact() 0 30 4
B searchContacts() 0 33 6
A contactLeads() 0 21 4
A updateContactField() 0 21 3
A createView() 0 21 3
A getView() 20 20 3
A saveView() 18 18 3
A editView() 0 9 1
B createLead() 0 35 10
A getLead() 20 20 3
A saveLead() 19 19 3
A editLead() 0 9 1
B addLeadPayment() 0 38 6
A changeLeadFunnel() 18 18 2
A getLeadComments() 0 19 3
A addCommentToLead() 20 20 2
A addFileToLead() 0 20 3
A saveLeadCustom() 0 18 3
A editLeadCustom() 0 26 4
A createCustom() 0 8 2
A getLeadsList() 0 20 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like LPTracker often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use LPTracker, and based on these observations, apply Extract Interface, too.

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