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 ( 2e6ed1...3999e5 )
by Sergey
01:18
created

LPTracker::editContactField()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 3
nc 4
nop 3
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\Employee;
13
use LPTracker\models\Lead;
14
use LPTracker\models\LeadFile;
15
use LPTracker\models\Project;
16
use LPTracker\models\Stage;
17
use LPTracker\models\View;
18
19
class LPTracker extends LPTrackerBase
20
{
21
    /**
22
     * @param string $login
23
     * @param string $password
24
     * @param string $serviceName
25
     * @return AccessToken
26
     * @throws LPTrackerSDKException
27
     */
28
    public function login($login, $password, $serviceName = '')
29
    {
30
        if (empty($login)) {
31
            throw new LPTrackerSDKException('Login is empty');
32
        }
33
34
        if (empty($password)) {
35
            throw new LPTrackerSDKException('Password is empty');
36
        }
37
38
        if (empty($serviceName)) {
39
            $serviceName = LPTrackerBase::DEFAULT_SERVICE_NAME;
40
        }
41
        $response = LPTrackerRequest::sendRequest(
42
            '/login',
43
            [
44
                'login' => $login,
45
                'password' => $password,
46
                'service' => $serviceName,
47
                'version' => LPTrackerBase::VERSION,
48
            ],
49
            'POST',
50
            null,
51
            $this->address
52
        );
53
        return new AccessToken($response['token']);
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
     * @return Project[]
67
     * @throws exceptions\LPTrackerResponseException
68
     * @throws exceptions\LPTrackerServerException
69
     */
70 View Code Duplication
    public function getProjects()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $response = LPTrackerRequest::sendRequest('/projects', [], 'GET', $this->token, $this->address);
73
        $result = [];
74
        foreach ($response as $projectData) {
75
            $result[] = new Project($projectData);
76
        }
77
        return $result;
78
    }
79
80
    /**
81
     * @param Project|int $project
82
     * @return Project
83
     * @throws exceptions\LPTrackerResponseException
84
     * @throws exceptions\LPTrackerServerException
85
     */
86
    public function getProject($project)
87
    {
88
        if ($project instanceof Project) {
89
            $project = $project->getId();
90
        } else {
91
            $project = (int) $project;
92
        }
93
        $url = '/project/' . $project;
94
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
95
        return new Project($response);
96
    }
97
98
    /**
99
     * @return Project[]
100
     * @throws exceptions\LPTrackerResponseException
101
     * @throws exceptions\LPTrackerServerException
102
     * @deprecated Use getProjects()
103
     */
104
    public function getProjectList()
105
    {
106
        return $this->getProjects();
107
    }
108
109
    /**
110
     * @param Project|int $project
111
     * @return Custom[]
112
     * @throws exceptions\LPTrackerResponseException
113
     * @throws exceptions\LPTrackerServerException
114
     */
115 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...
116
    {
117
        if ($project instanceof Project) {
118
            $project = $project->getId();
119
        } else {
120
            $project = (int) $project;
121
        }
122
        $url = '/project/' . $project . '/customs';
123
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
124
        $result = [];
125
        foreach ($response as $customData) {
126
            $result[] = new Custom($customData);
127
        }
128
        return $result;
129
    }
130
131
    /**
132
     * @param Project|int $project
133
     * @return Stage[]
134
     * @throws exceptions\LPTrackerResponseException
135
     * @throws exceptions\LPTrackerServerException
136
     */
137 View Code Duplication
    public function getProjectStages($project)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
    {
139
        if ($project instanceof Project) {
140
            $project = $project->getId();
141
        } else {
142
            $project = (int) $project;
143
        }
144
        $url = '/project/' . $project . '/funnel';
145
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
146
        $result = [];
147
        foreach ($response as $stageData) {
148
            $result[] = new Stage($stageData);
149
        }
150
        return $result;
151
    }
152
153
    /**
154
     * @param Project|int $project
155
     * @return ContactField[]
156
     * @throws exceptions\LPTrackerResponseException
157
     * @throws exceptions\LPTrackerServerException
158
     */
159 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...
160
    {
161
        if ($project instanceof Project) {
162
            $project = $project->getId();
163
        } else {
164
            $project = (int) $project;
165
        }
166
        $url = '/project/' . $project . '/fields';
167
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
168
        $result = [];
169
        foreach ($response as $customData) {
170
            $result[] = new ContactField($customData);
171
        }
172
        return $result;
173
    }
174
175
    /**
176
     * @param Project|int $project
177
     * @param string $callbackUrl
178
     * @throws exceptions\LPTrackerResponseException
179
     * @throws exceptions\LPTrackerServerException
180
     */
181
    public function setProjectCallbackUrl($project, $callbackUrl)
182
    {
183
        if ($project instanceof Project) {
184
            $project = $project->getId();
185
        } else {
186
            $project = (int) $project;
187
        }
188
        $url = '/project/' . $project . '/callback-url';
189
        LPTrackerRequest::sendRequest($url, ['url' => $callbackUrl], 'PUT', $this->token, $this->address);
190
    }
191
192
    /**
193
     * @return Employee[]
194
     * @throws exceptions\LPTrackerResponseException
195
     * @throws exceptions\LPTrackerServerException
196
     */
197 View Code Duplication
    public function getEmployees()
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...
198
    {
199
        $response = LPTrackerRequest::sendRequest('/staff', [], 'GET', $this->token, $this->address);
200
        $result = [];
201
        foreach ($response as $employeeData) {
202
            $result[] = new Employee($employeeData);
203
        }
204
        return $result;
205
    }
206
207
    /**
208
     * @param Project|int $project
209
     * @param array $details
210
     * @param array $contactData
211
     * @param array $fields
212
     * @return Contact
213
     * @throws LPTrackerSDKException
214
     */
215
    public function createContact(
216
        $project,
217
        array $details,
218
        array $contactData = [],
219
        array $fields = []
220
    ) {
221
        if (empty($details)) {
222
            throw new LPTrackerSDKException('Contact details can not be empty');
223
        }
224
225
        if ($project instanceof Project) {
226
            $project = $project->getId();
227
        } else {
228
            $project = (int) $project;
229
        }
230
        $contactData['project_id'] = $project;
231
        $contactData['details'] = $details;
232
        $fieldsArr = [];
233
        foreach ($fields as $fieldId => $fieldValue) {
234
            if ($fieldValue instanceof ContactField) {
235
                $fieldId = $fieldValue->getId();
236
                $fieldValue = $fieldValue->getValue();
237
            }
238
            $fieldsArr[$fieldId] = $fieldValue;
239
        }
240
        $contact = new Contact($contactData);
241
        $contact->validate();
242
        $data = $contact->toArray();
243
        $data['fields'] = $fieldsArr;
244
        $response = LPTrackerRequest::sendRequest('/contact', $data, 'POST', $this->token, $this->address);
245
        return new Contact($response);
246
    }
247
248
    /**
249
     * @param Contact|int $contact
250
     * @return Contact
251
     * @throws LPTrackerSDKException
252
     */
253
    public function getContact($contact)
254
    {
255
        if ($contact instanceof Contact) {
256
            $contact = $contact->getId();
257
        } else {
258
            $contact = (int) $contact;
259
        }
260
        if ($contact <= 0) {
261
            throw new LPTrackerSDKException('Invalid contact ID');
262
        }
263
264
        $url = '/contact/' . $contact;
265
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
266
        return new Contact($response);
267
    }
268
269
    /**
270
     * @param Contact $contact
271
     * @return Contact
272
     * @throws LPTrackerSDKException
273
     */
274
    public function saveContact(Contact $contact)
275
    {
276
        if (!$contact->validate()) {
277
            throw new LPTrackerSDKException('Invalid contact');
278
        }
279
280
        $data = $contact->toArray();
281
        if (!empty($data['fields'])) {
282
            $fields = [];
283
            foreach ($data['fields'] as $field) {
284
                $fields[$field['id']] = $field['value'];
285
            }
286
            $data['fields'] = $fields;
287
        }
288
        if ($contact->getId() > 0) {
289
            $url = '/contact/' . $contact->getId();
290
            $response = LPTrackerRequest::sendRequest($url, $contact->toArray(), 'PUT', $this->token, $this->address);
291
        } else {
292
            $response = LPTrackerRequest::sendRequest('/contact', $contact->toArray(), 'POST', $this->token, $this->address);
293
        }
294
        return new Contact($response);
295
    }
296
297
    /**
298
     * @param Contact|int $contact
299
     * @param array $details
300
     * @param array $contactData
301
     * @param array $fields
302
     * @return Contact
303
     * @throws LPTrackerSDKException
304
     */
305
    public function editContact(
306
        $contact,
307
        array $details,
308
        array $contactData = [],
309
        array $fields = []
310
    ) {
311
        if ($contact instanceof Contact) {
312
            $contact = $contact->getId();
313
        } else {
314
            $contact = (int) $contact;
315
        }
316
        if (empty($details)) {
317
            throw new LPTrackerSDKException('Contact details can not be empty');
318
        }
319
320
        $contactData['id'] = $contact;
321
        $contactData['details'] = $details;
322
        foreach ($fields as $fieldId => $fieldValue) {
323
            if ($fieldValue instanceof ContactField) {
324
                $fieldId = $fieldValue->getId();
325
                $fieldValue = $fieldValue->getValue();
326
            }
327
            $contactData['fields'][] = [
328
                'id' => $fieldId,
329
                'value' => $fieldValue,
330
            ];
331
        }
332
        $contact = new Contact($contactData);
333
        $contact->validate();
334
        return $this->saveContact($contact);
335
    }
336
337
    /**
338
     * @param Project|int $project
339
     * @param array $searchOptions
340
     * @return Contact[]
341
     * @throws LPTrackerSDKException
342
     */
343
    public function searchContacts($project, array $searchOptions = [])
344
    {
345
        if ($project instanceof Project) {
346
            $project = $project->getId();
347
        } else {
348
            $project = (int) $project;
349
        }
350
        if ($project <= 0) {
351
            throw new LPTrackerSDKException('Invalid project id');
352
        }
353
354
        $data = [
355
            'project_id' => $project,
356
        ];
357
        if (isset($searchOptions['email'])) {
358
            $data['email'] = $searchOptions['email'];
359
        }
360
        if (isset($searchOptions['phone'])) {
361
            $data['phone'] = $searchOptions['phone'];
362
        }
363
        $url = '/contact/search?' . http_build_query($data);
364
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
365
        $result = [];
366
        foreach ($response as $contactData) {
367
            $result[] = new Contact($contactData);
368
        }
369
        return $result;
370
    }
371
372
    /**
373
     * @param Contact|int $contact
374
     * @return Lead[]
375
     * @throws LPTrackerSDKException
376
     */
377
    public function getContactLeads($contact)
378
    {
379
        if ($contact instanceof Contact) {
380
            $contact = $contact->getId();
381
        } else {
382
            $contact = (int) $contact;
383
        }
384
        if ($contact <= 0) {
385
            throw new LPTrackerSDKException('Invalid contact id');
386
        }
387
388
        $url = '/contact/' . $contact . '/leads';
389
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
390
        $result = [];
391
        foreach ($response as $leadData) {
392
            $result[] = new Lead($leadData);
393
        }
394
        return $result;
395
    }
396
397
    /**
398
     * @param Contact|int $contact
399
     * @return array
400
     * @throws LPTrackerSDKException
401
     * @deprecated Use getContactLeads()
402
     */
403
    public function contactLeads($contact)
404
    {
405
        return $this->getContactLeads($contact);
406
    }
407
408
    /**
409
     * @param Contact|int $contact
410
     * @param ContactField|int $field
411
     * @param mixed $newValue
412
     * @return ContactField
413
     * @throws LPTrackerSDKException
414
     * @throws exceptions\LPTrackerResponseException
415
     * @throws exceptions\LPTrackerServerException
416
     */
417
    public function editContactField($contact, $field, $newValue)
418
    {
419
        if ($contact instanceof Contact) {
420
            $contact = $contact->getId();
421
        } else {
422
            $contact = (int) $contact;
423
        }
424
        if ($field instanceof ContactField) {
425
            $field = $field->getId();
426
        } else {
427
            $field = (int) $field;
428
        }
429
        $fieldModel = new ContactField([
430
            'id' => $field,
431
            'contact_id' => $contact,
432
            'value' => $newValue,
433
        ]);
434
        $fieldModel->validate();
435
        return $this->saveContactField($fieldModel);
436
    }
437
438
    /**
439
     * @param Contact|int $contact
440
     * @param ContactField|int $field
441
     * @param string $newValue
442
     * @return ContactField
443
     * @throws exceptions\LPTrackerResponseException
444
     * @throws exceptions\LPTrackerServerException
445
     * @deprecated Use editContactField()
446
     */
447
    public function updateContactField($contact, $field, $newValue)
448
    {
449
        return $this->editContactField($contact, $field, $newValue);
450
    }
451
452
    /**
453
     * @param ContactField $field
454
     * @return ContactField
455
     * @throws LPTrackerSDKException
456
     * @throws exceptions\LPTrackerResponseException
457
     * @throws exceptions\LPTrackerServerException
458
     */
459
    public function saveContactField(ContactField $field)
460
    {
461
        if (!$field->validate() || empty($field->getContactId())) {
462
            throw new LPTrackerSDKException('Invalid field');
463
        }
464
465
        $url = '/contact/' . $field->getContactId() . '/field/' . $field->getId();
466
        if ($field->getValue() === null) {
467
            LPTrackerRequest::sendRequest($url, [], 'DELETE', $this->token, $this->address);
468
            $response = $field->toArray();
469
            $response['value'] = null;
470 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
471
            $data = [
472
                'value' => $field->getValue(),
473
            ];
474
            $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
475
        }
476
        return new ContactField($response);
477
    }
478
479
    /**
480
     * @param Project|int $project
481
     * @param array $viewData
482
     * @return View
483
     * @throws LPTrackerSDKException
484
     */
485
    public function createView($project, array $viewData = [])
486
    {
487
        if ($project instanceof Project) {
488
            $viewData['project_id'] = $project->getId();
489
        } else {
490
            $viewData['project_id'] = (int) $project;
491
        }
492
        $view = new View($viewData);
493
        if (!$view->validate()) {
494
            throw new LPTrackerSDKException('Invalid view data');
495
        }
496
497
        $data = $view->toArray();
498
        $response = LPTrackerRequest::sendRequest('/view', $data, 'POST', $this->token, $this->address);
499
        return new View($response);
500
    }
501
502
    /**
503
     * @param View|int $view
504
     * @return View
505
     * @throws LPTrackerSDKException
506
     */
507
    public function getView($view)
508
    {
509
        if ($view instanceof View) {
510
            $view = $view->getId();
511
        } else {
512
            $view = (int) $view;
513
        }
514
        if ($view <= 0) {
515
            throw new LPTrackerSDKException('Invalid view ID');
516
        }
517
518
        $url = '/view/' . $view;
519
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
520
        return new View($response);
521
    }
522
523
    /**
524
     * @param View $view
525
     * @return View
526
     * @throws LPTrackerSDKException
527
     */
528 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...
529
    {
530
        if (!$view->validate()) {
531
            throw new LPTrackerSDKException('Invalid view');
532
        }
533
534
        if ($view->getId() > 0) {
535
            $url = '/view/' . $view->getId();
536
            $response = LPTrackerRequest::sendRequest($url, $view->toArray(), 'PUT', $this->token, $this->address);
537
        } else {
538
            $response = LPTrackerRequest::sendRequest('/view', $view->toArray(), 'POST', $this->token, $this->address);
539
        }
540
        return new View($response);
541
    }
542
543
    /**
544
     * @param View|int $view
545
     * @param array $viewData
546
     * @return View
547
     * @throws LPTrackerSDKException
548
     */
549
    public function editView($view, array $viewData = [])
550
    {
551
        if ($view instanceof View) {
552
            $viewData['id'] = $view->getId();
553
        } else {
554
            $viewData['id'] = (int) $view;
555
        }
556
        $view = new View($viewData);
557
        $view->validate();
558
        return $this->saveView($view);
559
    }
560
561
    /**
562
     * @param Contact|int $contact
563
     * @param array $leadData
564
     * @param array $options
565
     * @return Lead
566
     * @throws LPTrackerSDKException
567
     */
568
    public function createLead($contact, array $leadData = [], array $options = [])
569
    {
570
        if ($contact instanceof Contact) {
571
            $leadData['contact_id'] = $contact->getId();
572
        } else {
573
            $leadData['contact_id'] = (int) $contact;
574
        }
575
        $lead = new Lead($leadData);
576
        if (!$lead->validate()) {
577
            throw new LPTrackerSDKException('Invalid lead data');
578
        }
579
580
        if (!empty($lead->getView()) && empty($lead->getView()->getId())) {
581
            $contactModel = $this->getContact($contact);
582
            $viewData = $lead->getView()->toArray();
583
            $lead->setView($this->createView($contactModel->getProjectId(), $viewData));
584
        }
585
        $data = $lead->toArray(true);
586
        if (isset($options['callback'])) {
587
            $data['callback'] = $options['callback'] ? true : false;
588
        }
589
        if (isset($leadData['view_id'])) {
590
            $data['view_id'] = (int) $leadData['view_id'];
591
        } elseif (!empty($lead->getView()) && !empty($lead->getView()->getId())) {
592
            $data['view_id'] = $lead->getView()->getId();
593
        }
594
        $response = LPTrackerRequest::sendRequest('/lead', $data, 'POST', $this->token, $this->address);
595
        return new Lead($response);
596
    }
597
598
    /**
599
     * @param Lead|int $lead
600
     * @return Lead
601
     * @throws LPTrackerSDKException
602
     */
603
    public function getLead($lead)
604
    {
605
        if ($lead instanceof Lead) {
606
            $lead = $lead->getId();
607
        } else {
608
            $lead = (int) $lead;
609
        }
610
        if ($lead <= 0) {
611
            throw new LPTrackerSDKException('Invalid lead ID');
612
        }
613
614
        $url = '/lead/' . $lead;
615
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
616
        return new Lead($response);
617
    }
618
619
    /**
620
     * @param Lead|int $lead
621
     * @param Custom|int $custom
622
     * @return LeadFile
623
     * @throws LPTrackerSDKException
624
     */
625
    public function getCustomFile($lead, $custom, $file)
626
    {
627
        if ($lead instanceof Lead) {
628
            $lead = $lead->getId();
629
        } else {
630
            $lead = (int) $lead;
631
        }
632
        if ($custom instanceof Custom) {
633
            $custom = $custom->getId();
634
        } else {
635
            $custom = (int) $custom;
636
        }
637
        if ($lead <= 0) {
638
            throw new LPTrackerSDKException('Invalid lead ID');
639
        }
640
        if ($custom <= 0) {
641
            throw new LPTrackerSDKException('Invalid custom ID');
642
        }
643
        $file = (int)$file;
644
        if ($file <= 0) {
645
            throw new LPTrackerSDKException('Invalid file ID');
646
        }
647
648
        $url = '/lead/' . $lead . '/custom/' . $custom . '/file/' . $file;
649
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
650
        return new LeadFile($response);
651
    }
652
653
    /**
654
     * @param Lead $lead
655
     * @return Lead
656
     * @throws LPTrackerSDKException
657
     */
658 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...
659
    {
660
        if (!$lead->validate()) {
661
            throw new LPTrackerSDKException('Invalid lead');
662
        }
663
664
        if ($lead->getId() > 0) {
665
            $url = '/lead/' . $lead->getId();
666
            $response = LPTrackerRequest::sendRequest($url, $lead->toArray(true), 'PUT', $this->token, $this->address);
667
        } else {
668
            $response = LPTrackerRequest::sendRequest(
669
                '/lead',
670
                $lead->toArray(true),
671
                'POST',
672
                $this->token,
673
                $this->address
674
            );
675
        }
676
        return new Lead($response);
677
    }
678
679
    /**
680
     * @param Lead|int $lead
681
     * @param array $leadData
682
     * @return Lead
683
     * @throws LPTrackerSDKException
684
     */
685
    public function editLead($lead, array $leadData = [])
686
    {
687
        if ($lead instanceof Lead) {
688
            $leadData['id'] = $lead->getId();
689
        } else {
690
            $leadData['id'] = (int) $lead;
691
        }
692
        $lead = new Lead($leadData);
693
        $lead->validate();
694
        return $this->saveLead($lead);
695
    }
696
697
    /**
698
     * @param Lead|int $lead
699
     * @param string $category
700
     * @param string $purpose
701
     * @param float $sum
702
     * @return Lead
703
     * @throws LPTrackerSDKException
704
     */
705
    public function addPaymentToLead($lead, $category, $purpose, $sum)
706
    {
707
        if ($lead instanceof Lead) {
708
            $lead = $lead->getId();
709
        } else {
710
            $lead = (int) $lead;
711
        }
712
        if (empty($category)) {
713
            throw new LPTrackerSDKException('Category can not be empty');
714
        }
715
716
        if (empty($purpose)) {
717
            throw new LPTrackerSDKException('Purpose can not be empty');
718
        }
719
720
        $sum = (float) $sum;
721
        if ($sum <= 0) {
722
            throw new LPTrackerSDKException('Invalid sum');
723
        }
724
725
        if ($lead <= 0) {
726
            throw new LPTrackerSDKException('Invalid lead ID');
727
        }
728
729
        $url = '/lead/' . $lead . '/payment';
730
        $data = [
731
            'category' => $category,
732
            'purpose' => $purpose,
733
            'sum' => $sum,
734
        ];
735
        $response = LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address);
736
        return new Lead($response);
737
    }
738
739
    /**
740
     * @param Lead|int $lead
741
     * @param string $category
742
     * @param string $purpose
743
     * @param float $sum
744
     * @return Lead
745
     * @throws LPTrackerSDKException
746
     * @deprecated Use addPaymentToLead()
747
     */
748
    public function addLeadPayment($lead, $category, $purpose, $sum)
749
    {
750
        return $this->addPaymentToLead($lead, $category, $purpose, $sum);
751
    }
752
753
    /**
754
     * @param Lead|int $lead
755
     * @param int $newStageId
756
     * @param array $options
757
     * @return Lead
758
     * @throws LPTrackerSDKException
759
     */
760 View Code Duplication
    public function editLeadStage($lead, $newStageId, array $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...
761
    {
762
        if ($lead instanceof Lead) {
763
            $lead = $lead->getId();
764
        } else {
765
            $lead = (int) $lead;
766
        }
767
        $url = '/lead/' . $lead . '/funnel';
768
        $data = [
769
            'funnel' => $newStageId,
770
            'options' => $options,
771
        ];
772
        $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
773
        return new Lead($response);
774
    }
775
776
    /**
777
     * @param Lead|int $lead
778
     * @param int $newFunnelId
779
     * @return Lead
780
     * @throws LPTrackerSDKException
781
     * @deprecated Use editLeadStage()
782
     */
783
    public function changeLeadFunnel($lead, $newFunnelId)
784
    {
785
        return $this->editLeadStage($lead, $newFunnelId);
786
    }
787
788
    /**
789
     * @param Lead|int $lead
790
     * @param int $newOwnerId
791
     * @param array $options
792
     * @return Lead
793
     * @throws LPTrackerSDKException
794
     */
795 View Code Duplication
    public function editLeadOwner($lead, $newOwnerId, array $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...
796
    {
797
        if ($lead instanceof Lead) {
798
            $lead = $lead->getId();
799
        } else {
800
            $lead = (int) $lead;
801
        }
802
        $url = '/lead/' . $lead . '/owner';
803
        $data = [
804
            'owner' => $newOwnerId,
805
            'options' => $options,
806
        ];
807
        $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
808
        return new Lead($response);
809
    }
810
811
    /**
812
     * @param Lead|int $lead
813
     * @return Comment[]
814
     * @throws exceptions\LPTrackerResponseException
815
     * @throws exceptions\LPTrackerServerException
816
     */
817 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...
818
    {
819
        if ($lead instanceof Lead) {
820
            $lead = $lead->getId();
821
        } else {
822
            $lead = (int) $lead;
823
        }
824
        $url = '/lead/' . $lead . '/comments';
825
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
826
        $result = [];
827
        foreach ($response as $commentData) {
828
            $result[] = new Comment($commentData);
829
        }
830
        return $result;
831
    }
832
833
    /**
834
     * @param Lead|int $lead
835
     * @param string $text
836
     * @param array $options
837
     * @return Comment
838
     * @throws exceptions\LPTrackerResponseException
839
     * @throws exceptions\LPTrackerServerException
840
     */
841 View Code Duplication
    public function addCommentToLead($lead, $text, array $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...
842
    {
843
        if ($lead instanceof Lead) {
844
            $lead = $lead->getId();
845
        } else {
846
            $lead = (int) $lead;
847
        }
848
        $url = '/lead/' . $lead . '/comment';
849
        $data = [
850
            'text' => $text,
851
            'options' => $options,
852
        ];
853
        $response = LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address);
854
        return new Comment($response);
855
    }
856
857
    /**
858
     * @param Lead|int $lead
859
     * @param Custom|int $custom
860
     * @param string $absolutePath
861
     * @throws exceptions\LPTrackerResponseException
862
     * @throws exceptions\LPTrackerServerException
863
     */
864
    public function addFileToLead($lead, $custom, $absolutePath)
865
    {
866
        if ($lead instanceof Lead) {
867
            $lead = $lead->getId();
868
        } else {
869
            $lead = (int) $lead;
870
        }
871
        if ($custom instanceof Custom) {
872
            $custom = $custom->getId();
873
        } else {
874
            $custom = (int) $custom;
875
        }
876
        $url = '/lead/' . $lead . '/file';
877
        $data = [
878
            'name' => pathinfo($absolutePath, PATHINFO_BASENAME),
879
            'mime' => mime_content_type($absolutePath),
880
            'data' => base64_encode(file_get_contents($absolutePath)),
881
            'custom_field_id' => $custom,
882
        ];
883
        LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address);
884
    }
885
886
    /**
887
     * @param Custom $custom
888
     * @return Custom
889
     * @throws LPTrackerSDKException
890
     */
891
    public function saveLeadCustom(Custom $custom, array $options = [])
892
    {
893
        if (!$custom->validate() || empty($custom->getLeadId())) {
894
            throw new LPTrackerSDKException('Invalid custom');
895
        }
896
897
        $url = '/lead/' . $custom->getLeadId() . '/custom/' . $custom->getId();
898
        if ($custom->getValue() === null) {
899
            $data = [
900
                'options' => $options,
901
            ];
902
            LPTrackerRequest::sendRequest($url, $data, 'DELETE', $this->token, $this->address);
903
            $response = $custom->toArray();
904
            $response['value'] = null;
905 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
906
            $data = [
907
                'value' => $custom->getValue(),
908
                'options' => $options,
909
            ];
910
            $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
911
        }
912
        return new Custom($response, $custom->getLeadId());
913
    }
914
915
    /**
916
     * @param Lead|int $lead
917
     * @param Custom|int $custom
918
     * @param mixed $newValue
919
     * @param array $options
920
     * @return Custom
921
     * @throws LPTrackerSDKException
922
     */
923
    public function editLeadCustom($lead, $custom, $newValue, array $options = [])
924
    {
925
        if ($lead instanceof Lead) {
926
            $lead = $lead->getId();
927
        } else {
928
            $lead = (int) $lead;
929
        }
930
        if ($custom instanceof Custom) {
931
            if (empty($newValue)) {
932
                $newValue = $custom->getValue();
933
            }
934
            $custom = $custom->getId();
935
        } else {
936
            $custom = (int) $custom;
937
        }
938
        $customModel = new Custom([
939
            'id' => $custom,
940
            'value' => $newValue,
941
        ], $lead);
942
        $customModel->validate();
943
        return $this->saveLeadCustom($customModel, $options);
944
    }
945
946
    /**
947
     * @param Project|int $project
948
     * @param array $options
949
     * @return CustomField
950
     * @throws exceptions\LPTrackerResponseException
951
     * @throws exceptions\LPTrackerServerException
952
     */
953
    public function createCustom($project, $options)
954
    {
955
        if ($project instanceof Project) {
956
            $project = $project->getId();
957
        } else {
958
            $project = (int) $project;
959
        }
960
        $actionUrl = '/custom/' . $project . '/create';
961
        $response = LPTrackerRequest::sendRequest($actionUrl, $options, 'POST', $this->token, $this->address);
962
        return new CustomField($response);
963
    }
964
965
    /**
966
     * @param Project|int $project
967
     * @param int $offset
968
     * @param int $limit
969
     * @param array $sort
970
     * @param bool $isDeal
971
     * @param array $filter
972
     * @return Lead[]
973
     * @throws exceptions\LPTrackerResponseException
974
     * @throws exceptions\LPTrackerServerException
975
     */
976
    public function getLeads($project, $offset = null, $limit = null, $sort = [], $isDeal = false, $filter = [])
977
    {
978
        if ($project instanceof Project) {
979
            $project = $project->getId();
980
        } else {
981
            $project = (int) $project;
982
        }
983
        $actionUrl = '/lead/' . $project . '/list?' . http_build_query([
984
            'offset' => $offset,
985
            'limit' => $limit,
986
            'sort' => $sort,
987
            'is_deal' => $isDeal,
988
            'filter' => $filter,
989
        ]);
990
        $response = LPTrackerRequest::sendRequest($actionUrl, [], 'GET', $this->token, $this->address);
991
        $result = [];
992
        foreach ($response as $lead) {
993
            $result[] = new Lead($lead);
994
        }
995
        return $result;
996
    }
997
998
    /**
999
     * @param Project|int $project
1000
     * @param int $offset
1001
     * @param int $limit
1002
     * @param array $sort
1003
     * @param bool $isDeal
1004
     * @param array $filter
1005
     * @return Lead[]
1006
     * @throws exceptions\LPTrackerResponseException
1007
     * @throws exceptions\LPTrackerServerException
1008
     * @deprecated Use getLeads()
1009
     */
1010
    public function getLeadsList($project, $offset = null, $limit = null, $sort = [], $isDeal = false, $filter = [])
1011
    {
1012
        return $this->getLeads($project, $offset, $limit, $sort, $isDeal, $filter);
1013
    }
1014
}
1015