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 ( 1b8c97...552815 )
by
unknown
15s queued 13s
created

LPTracker::searchContacts()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

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