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 ( 7d5c0e...1a563c )
by Sergey
01:59
created

LPTracker::createLead()   B

Complexity

Conditions 10
Paths 38

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 7.6666
c 0
b 0
f 0
cc 10
nc 38
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 $lead
522
     * @return Lead
523
     * @throws LPTrackerSDKException
524
     */
525 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...
526
    {
527
        if (!$lead->validate()) {
528
            throw new LPTrackerSDKException('Invalid lead');
529
        }
530
531
        if ($lead->getId() > 0) {
532
            $url = '/lead/' . $lead->getId();
533
            $response = LPTrackerRequest::sendRequest($url, $lead->toArray(true), 'PUT', $this->token, $this->address);
534
        } else {
535
            $response = LPTrackerRequest::sendRequest(
536
                '/lead',
537
                $lead->toArray(true),
538
                'POST',
539
                $this->token,
540
                $this->address
541
            );
542
        }
543
        return new Lead($response);
544
    }
545
546
    /**
547
     * @param Lead|int $lead
548
     * @param array $leadData
549
     * @return Lead
550
     * @throws LPTrackerSDKException
551
     */
552
    public function editLead($lead, array $leadData = [])
553
    {
554
        if ($lead instanceof Lead) {
555
            $leadData['id'] = $lead->getId();
556
        } else {
557
            $leadData['id'] = (int) $lead;
558
        }
559
        $lead = new Lead($leadData);
560
        $lead->validate();
561
        return $this->saveLead($lead);
562
    }
563
564
    /**
565
     * @param Lead|int $lead
566
     * @param string $category
567
     * @param string $purpose
568
     * @param float $sum
569
     * @return Lead
570
     * @throws LPTrackerSDKException
571
     */
572
    public function addLeadPayment($lead, $category, $purpose, $sum)
573
    {
574
        if ($lead instanceof Lead) {
575
            $lead = $lead->getId();
576
        } else {
577
            $lead = (int) $lead;
578
        }
579
        if (empty($category)) {
580
            throw new LPTrackerSDKException('Category can not be empty');
581
        }
582
583
        if (empty($purpose)) {
584
            throw new LPTrackerSDKException('Purpose can not be empty');
585
        }
586
587
        $sum = (float) $sum;
588
        if ($sum <= 0) {
589
            throw new LPTrackerSDKException('Invalid sum');
590
        }
591
592
        if ($lead <= 0) {
593
            throw new LPTrackerSDKException('Invalid lead ID');
594
        }
595
596
        $url = '/lead/' . $lead . '/payment';
597
        $data = [
598
            'category' => $category,
599
            'purpose' => $purpose,
600
            'sum' => $sum,
601
        ];
602
        $response = LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address);
603
        return new Lead($response);
604
    }
605
606
    /**
607
     * @param Lead|int $lead
608
     * @param int $newFunnelId
609
     * @return Lead
610
     * @throws LPTrackerSDKException
611
     */
612 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...
613
    {
614
        if ($lead instanceof Lead) {
615
            $lead = $lead->getId();
616
        } else {
617
            $lead = (int) $lead;
618
        }
619
        $url = '/lead/' . $lead . '/funnel';
620
        $data = [
621
            'funnel' => $newFunnelId,
622
        ];
623
        $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
624
        return new Lead($response);
625
    }
626
627
    /**
628
     * @param Lead|int $lead
629
     * @return Comment[]
630
     * @throws exceptions\LPTrackerResponseException
631
     * @throws exceptions\LPTrackerServerException
632
     */
633 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...
634
    {
635
        if ($lead instanceof Lead) {
636
            $lead = $lead->getId();
637
        } else {
638
            $lead = (int) $lead;
639
        }
640
        $url = '/lead/' . $lead . '/comments';
641
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
642
        $result = [];
643
        foreach ($response as $commentData) {
644
            $result[] = new Comment($commentData);
645
        }
646
        return $result;
647
    }
648
649
    /**
650
     * @param Lead|int $lead
651
     * @param string $text
652
     * @return Comment
653
     * @throws exceptions\LPTrackerResponseException
654
     * @throws exceptions\LPTrackerServerException
655
     */
656 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...
657
    {
658
        if ($lead instanceof Lead) {
659
            $lead = $lead->getId();
660
        } else {
661
            $lead = (int) $lead;
662
        }
663
        $url = '/lead/' . $lead . '/comment';
664
        $data = [
665
            'text' => $text,
666
        ];
667
        $response = LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address);
668
        return new Comment($response);
669
    }
670
671
    /**
672
     * @param Lead|int $lead
673
     * @param Custom|int $custom
674
     * @param string $absolutePath
675
     * @throws exceptions\LPTrackerResponseException
676
     * @throws exceptions\LPTrackerServerException
677
     */
678
    public function addFileToLead($lead, $custom, $absolutePath)
679
    {
680
        if ($lead instanceof Lead) {
681
            $lead = $lead->getId();
682
        } else {
683
            $lead = (int) $lead;
684
        }
685
        if ($custom instanceof Custom) {
686
            $custom = $custom->getId();
687
        } else {
688
            $custom = (int) $custom;
689
        }
690
        $url = '/lead/' . $lead . '/file';
691
        $data = [
692
            'name' => pathinfo($absolutePath, PATHINFO_BASENAME),
693
            'mime' => mime_content_type($absolutePath),
694
            'data' => base64_encode(file_get_contents($absolutePath)),
695
            'custom_field_id' => $custom,
696
        ];
697
        LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address);
698
    }
699
700
    /**
701
     * @param Custom $custom
702
     * @return Custom
703
     * @throws LPTrackerSDKException
704
     */
705
    public function saveLeadCustom(Custom $custom)
706
    {
707
        if (!$custom->validate() || empty($custom->getLeadId())) {
708
            throw new LPTrackerSDKException('Invalid custom');
709
        }
710
711
        $url = '/lead/' . $custom->getLeadId() . '/custom/' . $custom->getId();
712
        $data = [
713
            'value' => $custom->getValue(),
714
        ];
715
        $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
716
        return new Custom($response, $custom->getLeadId());
717
    }
718
719
    /**
720
     * @param Lead|int $lead
721
     * @param Custom|int $custom
722
     * @param mixed $newValue
723
     * @return Custom
724
     * @throws LPTrackerSDKException
725
     */
726
    public function editLeadCustom($lead, $custom, $newValue)
727
    {
728
        if ($lead instanceof Lead) {
729
            $lead = $lead->getId();
730
        } else {
731
            $lead = (int) $lead;
732
        }
733
        if ($custom instanceof Custom) {
734
            if (empty($newValue)) {
735
                $newValue = $custom->getValue();
736
            }
737
            $custom = $custom->getId();
738
        } else {
739
            $custom = (int) $custom;
740
        }
741
        $customModel = new Custom([
742
            'id' => $custom,
743
            'value' => $newValue,
744
        ], $lead);
745
        $customModel->validate();
746
        return $this->saveLeadCustom($customModel);
747
    }
748
749
    /**
750
     * @param Project|int $project
751
     * @param array $options
752
     * @return CustomField
753
     * @throws exceptions\LPTrackerResponseException
754
     * @throws exceptions\LPTrackerServerException
755
     */
756 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...
757
    {
758
        if ($project instanceof Project) {
759
            $project = $project->getId();
760
        } else {
761
            $project = (int) $project;
762
        }
763
        $actionUrl = '/custom/' . $project . '/create';
764
        $response = LPTrackerRequest::sendRequest($actionUrl, $options, 'POST', $this->token, $this->address);
765
        return new CustomField($response);
766
    }
767
768
    /**
769
     * @param Project|int $project
770
     * @param int $offset
771
     * @param int $limit
772
     * @param array $sort
773
     * @param bool $isDeal
774
     * @param array $filter
775
     * @return Lead[]
776
     * @throws exceptions\LPTrackerResponseException
777
     * @throws exceptions\LPTrackerServerException
778
     */
779
    public function getLeadsList($project, $offset = null, $limit = null, $sort = [], $isDeal = false, $filter = [])
780
    {
781
        if ($project instanceof Project) {
782
            $project = $project->getId();
783
        } else {
784
            $project = (int) $project;
785
        }
786
        $actionUrl = '/lead/' . $project . '/list?' . http_build_query([
787
            'offset' => $offset,
788
            'limit' => $limit,
789
            'sort' => $sort,
790
            'is_deal' => $isDeal,
791
            'filter' => $filter,
792
        ]);
793
        $response = LPTrackerRequest::sendRequest($actionUrl, [], 'GET', $this->token, $this->address);
794
        $result = [];
795
        foreach ($response as $lead) {
796
            $result[] = new Lead($lead);
797
        }
798
        return $result;
799
    }
800
}
801