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 ( 174054...6dbaad )
by Sergey
01:28
created

LPTracker::saveContact()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.5706
c 0
b 0
f 0
cc 7
nc 5
nop 1
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 View Code Duplication
    public function setProjectCallbackUrl($project, $callbackUrl)
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...
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 View Code Duplication
    public function getContact($contact)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
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 $key => $field) {
284
                if (is_array($field) && isset($field['id'], $field['value'])) {
285
                    $fields[$field['id']] = $field['value'];
286
                } else {
287
                    $fields[$key] = $field;
288
                }
289
            }
290
            $data['fields'] = $fields;
291
        }
292
        if ($contact->getId() > 0) {
293
            $url = '/contact/' . $contact->getId();
294
            $response = LPTrackerRequest::sendRequest($url, $contact->toArray(), 'PUT', $this->token, $this->address);
295
        } else {
296
            $response = LPTrackerRequest::sendRequest('/contact', $contact->toArray(), 'POST', $this->token, $this->address);
297
        }
298
        return new Contact($response);
299
    }
300
301
    /**
302
     * @param Contact|int $contact
303
     * @param array $details
304
     * @param array $contactData
305
     * @param array $fields
306
     * @return Contact
307
     * @throws LPTrackerSDKException
308
     */
309
    public function editContact(
310
        $contact,
311
        array $details,
312
        array $contactData = [],
313
        array $fields = []
314
    ) {
315
        if ($contact instanceof Contact) {
316
            $contact = $contact->getId();
317
        } else {
318
            $contact = (int) $contact;
319
        }
320
        if (empty($details)) {
321
            throw new LPTrackerSDKException('Contact details can not be empty');
322
        }
323
324
        $contactData['id'] = $contact;
325
        $contactData['details'] = $details;
326
        foreach ($fields as $fieldId => $fieldValue) {
327
            if ($fieldValue instanceof ContactField) {
328
                $fieldId = $fieldValue->getId();
329
                $fieldValue = $fieldValue->getValue();
330
            }
331
            $contactData['fields'][] = [
332
                'id' => $fieldId,
333
                'value' => $fieldValue,
334
            ];
335
        }
336
        $contact = new Contact($contactData);
337
        $contact->validate();
338
        return $this->saveContact($contact);
339
    }
340
341
    /**
342
     * @param Project|int $project
343
     * @param array $searchOptions
344
     * @return Contact[]
345
     * @throws LPTrackerSDKException
346
     */
347
    public function searchContacts($project, array $searchOptions = [])
348
    {
349
        if ($project instanceof Project) {
350
            $project = $project->getId();
351
        } else {
352
            $project = (int) $project;
353
        }
354
        if ($project <= 0) {
355
            throw new LPTrackerSDKException('Invalid project id');
356
        }
357
358
        $data = [
359
            'project_id' => $project,
360
        ];
361
        if (isset($searchOptions['email'])) {
362
            $data['email'] = $searchOptions['email'];
363
        }
364
        if (isset($searchOptions['phone'])) {
365
            $data['phone'] = $searchOptions['phone'];
366
        }
367
        $url = '/contact/search?' . http_build_query($data);
368
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
369
        $result = [];
370
        foreach ($response as $contactData) {
371
            $result[] = new Contact($contactData);
372
        }
373
        return $result;
374
    }
375
376
    /**
377
     * @param Contact|int $contact
378
     * @return Lead[]
379
     * @throws LPTrackerSDKException
380
     */
381
    public function getContactLeads($contact)
382
    {
383
        if ($contact instanceof Contact) {
384
            $contact = $contact->getId();
385
        } else {
386
            $contact = (int) $contact;
387
        }
388
        if ($contact <= 0) {
389
            throw new LPTrackerSDKException('Invalid contact id');
390
        }
391
392
        $url = '/contact/' . $contact . '/leads';
393
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
394
        $result = [];
395
        foreach ($response as $leadData) {
396
            $result[] = new Lead($leadData);
397
        }
398
        return $result;
399
    }
400
401
    /**
402
     * @param Contact|int $contact
403
     * @return array
404
     * @throws LPTrackerSDKException
405
     * @deprecated Use getContactLeads()
406
     */
407
    public function contactLeads($contact)
408
    {
409
        return $this->getContactLeads($contact);
410
    }
411
412
    /**
413
     * @param Contact|int $contact
414
     * @param ContactField|int $field
415
     * @param mixed $newValue
416
     * @return ContactField
417
     * @throws LPTrackerSDKException
418
     * @throws exceptions\LPTrackerResponseException
419
     * @throws exceptions\LPTrackerServerException
420
     */
421
    public function editContactField($contact, $field, $newValue)
422
    {
423
        if ($contact instanceof Contact) {
424
            $contact = $contact->getId();
425
        } else {
426
            $contact = (int) $contact;
427
        }
428
        if ($field instanceof ContactField) {
429
            $field = $field->getId();
430
        } else {
431
            $field = (int) $field;
432
        }
433
        $fieldModel = new ContactField([
434
            'id' => $field,
435
            'contact_id' => $contact,
436
            'value' => $newValue,
437
        ]);
438
        $fieldModel->validate();
439
        return $this->saveContactField($fieldModel);
440
    }
441
442
    /**
443
     * @param Contact|int $contact
444
     * @param ContactField|int $field
445
     * @param string $newValue
446
     * @return ContactField
447
     * @throws exceptions\LPTrackerResponseException
448
     * @throws exceptions\LPTrackerServerException
449
     * @deprecated Use editContactField()
450
     */
451
    public function updateContactField($contact, $field, $newValue)
452
    {
453
        return $this->editContactField($contact, $field, $newValue);
454
    }
455
456
    /**
457
     * @param ContactField $field
458
     * @return ContactField
459
     * @throws LPTrackerSDKException
460
     * @throws exceptions\LPTrackerResponseException
461
     * @throws exceptions\LPTrackerServerException
462
     */
463
    public function saveContactField(ContactField $field)
464
    {
465
        if (!$field->validate() || empty($field->getContactId())) {
466
            throw new LPTrackerSDKException('Invalid field');
467
        }
468
469
        $url = '/contact/' . $field->getContactId() . '/field/' . $field->getId();
470
        if ($field->getValue() === null) {
471
            LPTrackerRequest::sendRequest($url, [], 'DELETE', $this->token, $this->address);
472
            $response = $field->toArray();
473
            $response['value'] = null;
474 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...
475
            $data = [
476
                'value' => $field->getValue(),
477
            ];
478
            $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
479
        }
480
        return new ContactField($response);
481
    }
482
483
    /**
484
     * @param Project|int $project
485
     * @param array $viewData
486
     * @return View
487
     * @throws LPTrackerSDKException
488
     */
489
    public function createView($project, array $viewData = [])
490
    {
491
        if ($project instanceof Project) {
492
            $viewData['project_id'] = $project->getId();
493
        } else {
494
            $viewData['project_id'] = (int) $project;
495
        }
496
        $view = new View($viewData);
497
        if (!$view->validate()) {
498
            throw new LPTrackerSDKException('Invalid view data');
499
        }
500
501
        $data = $view->toArray();
502
        $response = LPTrackerRequest::sendRequest('/view', $data, 'POST', $this->token, $this->address);
503
        return new View($response);
504
    }
505
506
    /**
507
     * @param View|int $view
508
     * @return View
509
     * @throws LPTrackerSDKException
510
     */
511 View Code Duplication
    public function getView($view)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
512
    {
513
        if ($view instanceof View) {
514
            $view = $view->getId();
515
        } else {
516
            $view = (int) $view;
517
        }
518
        if ($view <= 0) {
519
            throw new LPTrackerSDKException('Invalid view ID');
520
        }
521
522
        $url = '/view/' . $view;
523
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
524
        return new View($response);
525
    }
526
527
    /**
528
     * @param View $view
529
     * @return View
530
     * @throws LPTrackerSDKException
531
     */
532 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...
533
    {
534
        if (!$view->validate()) {
535
            throw new LPTrackerSDKException('Invalid view');
536
        }
537
538
        if ($view->getId() > 0) {
539
            $url = '/view/' . $view->getId();
540
            $response = LPTrackerRequest::sendRequest($url, $view->toArray(), 'PUT', $this->token, $this->address);
541
        } else {
542
            $response = LPTrackerRequest::sendRequest('/view', $view->toArray(), 'POST', $this->token, $this->address);
543
        }
544
        return new View($response);
545
    }
546
547
    /**
548
     * @param View|int $view
549
     * @param array $viewData
550
     * @return View
551
     * @throws LPTrackerSDKException
552
     */
553
    public function editView($view, array $viewData = [])
554
    {
555
        if ($view instanceof View) {
556
            $viewData['id'] = $view->getId();
557
        } else {
558
            $viewData['id'] = (int) $view;
559
        }
560
        $view = new View($viewData);
561
        $view->validate();
562
        return $this->saveView($view);
563
    }
564
565
    /**
566
     * @param Contact|int $contact
567
     * @param array $leadData
568
     * @param array $options
569
     * @return Lead
570
     * @throws LPTrackerSDKException
571
     */
572
    public function createLead($contact, array $leadData = [], array $options = [])
573
    {
574
        if ($contact instanceof Contact) {
575
            $leadData['contact_id'] = $contact->getId();
576
        } else {
577
            $leadData['contact_id'] = (int) $contact;
578
        }
579
        $lead = new Lead($leadData);
580
        if (!$lead->validate()) {
581
            throw new LPTrackerSDKException('Invalid lead data');
582
        }
583
584
        if (!empty($lead->getView()) && empty($lead->getView()->getId())) {
585
            $contactModel = $this->getContact($contact);
586
            $viewData = $lead->getView()->toArray();
587
            $lead->setView($this->createView($contactModel->getProjectId(), $viewData));
588
        }
589
        $data = $lead->toArray(true);
590
        $data = array_merge($data, $options);
591
        if (isset($leadData['view_id'])) {
592
            $data['view_id'] = (int) $leadData['view_id'];
593
        } elseif (!empty($lead->getView()) && !empty($lead->getView()->getId())) {
594
            $data['view_id'] = $lead->getView()->getId();
595
        }
596
        $response = LPTrackerRequest::sendRequest('/lead', $data, 'POST', $this->token, $this->address);
597
        return new Lead($response);
598
    }
599
600
    /**
601
     * @param Lead|int $lead
602
     * @return Lead
603
     * @throws LPTrackerSDKException
604
     */
605 View Code Duplication
    public function getLead($lead)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
606
    {
607
        if ($lead instanceof Lead) {
608
            $lead = $lead->getId();
609
        } else {
610
            $lead = (int) $lead;
611
        }
612
        if ($lead <= 0) {
613
            throw new LPTrackerSDKException('Invalid lead ID');
614
        }
615
616
        $url = '/lead/' . $lead;
617
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
618
        return new Lead($response);
619
    }
620
621
    /**
622
     * @param Lead|int $lead
623
     * @param Custom|int $custom
624
     * @return LeadFile
625
     * @throws LPTrackerSDKException
626
     */
627
    public function getCustomFile($lead, $custom, $file)
628
    {
629
        if ($lead instanceof Lead) {
630
            $lead = $lead->getId();
631
        } else {
632
            $lead = (int) $lead;
633
        }
634
        if ($custom instanceof Custom) {
635
            $custom = $custom->getId();
636
        } else {
637
            $custom = (int) $custom;
638
        }
639
        if ($lead <= 0) {
640
            throw new LPTrackerSDKException('Invalid lead ID');
641
        }
642
        if ($custom <= 0) {
643
            throw new LPTrackerSDKException('Invalid custom ID');
644
        }
645
        $file = (int)$file;
646
        if ($file <= 0) {
647
            throw new LPTrackerSDKException('Invalid file ID');
648
        }
649
650
        $url = '/lead/' . $lead . '/custom/' . $custom . '/file/' . $file;
651
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
652
        return new LeadFile($response);
653
    }
654
655
    /**
656
     * @param Lead $lead
657
     * @return Lead
658
     * @throws LPTrackerSDKException
659
     */
660 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...
661
    {
662
        if (!$lead->validate()) {
663
            throw new LPTrackerSDKException('Invalid lead');
664
        }
665
666
        if ($lead->getId() > 0) {
667
            $url = '/lead/' . $lead->getId();
668
            $response = LPTrackerRequest::sendRequest($url, $lead->toArray(true), 'PUT', $this->token, $this->address);
669
        } else {
670
            $response = LPTrackerRequest::sendRequest(
671
                '/lead',
672
                $lead->toArray(true),
673
                'POST',
674
                $this->token,
675
                $this->address
676
            );
677
        }
678
        return new Lead($response);
679
    }
680
681
    /**
682
     * @param Lead|int $lead
683
     * @param array $leadData
684
     * @return Lead
685
     * @throws LPTrackerSDKException
686
     */
687
    public function editLead($lead, array $leadData = [])
688
    {
689
        if ($lead instanceof Lead) {
690
            $leadData['id'] = $lead->getId();
691
        } else {
692
            $leadData['id'] = (int) $lead;
693
        }
694
        $lead = new Lead($leadData);
695
        $lead->validate();
696
        return $this->saveLead($lead);
697
    }
698
699
    /**
700
     * @param Lead|int $lead
701
     * @param string $category
702
     * @param string $purpose
703
     * @param float $sum
704
     * @return Lead
705
     * @throws LPTrackerSDKException
706
     */
707
    public function addPaymentToLead($lead, $category, $purpose, $sum)
708
    {
709
        if ($lead instanceof Lead) {
710
            $lead = $lead->getId();
711
        } else {
712
            $lead = (int) $lead;
713
        }
714
        if (empty($category)) {
715
            throw new LPTrackerSDKException('Category can not be empty');
716
        }
717
718
        if (empty($purpose)) {
719
            throw new LPTrackerSDKException('Purpose can not be empty');
720
        }
721
722
        $sum = (float) $sum;
723
        if ($sum <= 0) {
724
            throw new LPTrackerSDKException('Invalid sum');
725
        }
726
727
        if ($lead <= 0) {
728
            throw new LPTrackerSDKException('Invalid lead ID');
729
        }
730
731
        $url = '/lead/' . $lead . '/payment';
732
        $data = [
733
            'category' => $category,
734
            'purpose' => $purpose,
735
            'sum' => $sum,
736
        ];
737
        $response = LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address);
738
        return new Lead($response);
739
    }
740
741
    /**
742
     * @param Lead|int $lead
743
     * @param string $category
744
     * @param string $purpose
745
     * @param float $sum
746
     * @return Lead
747
     * @throws LPTrackerSDKException
748
     * @deprecated Use addPaymentToLead()
749
     */
750
    public function addLeadPayment($lead, $category, $purpose, $sum)
751
    {
752
        return $this->addPaymentToLead($lead, $category, $purpose, $sum);
753
    }
754
755
    /**
756
     * @param Lead|int $lead
757
     */
758 View Code Duplication
    public function callLead($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...
759
    {
760
        if ($lead instanceof Lead) {
761
            $lead = $lead->getId();
762
        } else {
763
            $lead = (int) $lead;
764
        }
765
        $url = '/lead/' . $lead . '/call';
766
        LPTrackerRequest::sendRequest($url, [], 'POST', $this->token, $this->address);
767
    }
768
769
    /**
770
     * @param Lead|int $lead
771
     * @param int $newStageId
772
     * @param array $options
773
     * @return Lead
774
     * @throws LPTrackerSDKException
775
     */
776 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...
777
    {
778
        if ($lead instanceof Lead) {
779
            $lead = $lead->getId();
780
        } else {
781
            $lead = (int) $lead;
782
        }
783
        $url = '/lead/' . $lead . '/funnel';
784
        $data = [
785
            'funnel' => $newStageId,
786
            'options' => $options,
787
        ];
788
        $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
789
        return new Lead($response);
790
    }
791
792
    /**
793
     * @param Lead|int $lead
794
     * @param int $newFunnelId
795
     * @return Lead
796
     * @throws LPTrackerSDKException
797
     * @deprecated Use editLeadStage()
798
     */
799
    public function changeLeadFunnel($lead, $newFunnelId)
800
    {
801
        return $this->editLeadStage($lead, $newFunnelId);
802
    }
803
804
    /**
805
     * @param Lead|int $lead
806
     * @param int $newOwnerId
807
     * @param array $options
808
     * @return Lead
809
     * @throws LPTrackerSDKException
810
     */
811 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...
812
    {
813
        if ($lead instanceof Lead) {
814
            $lead = $lead->getId();
815
        } else {
816
            $lead = (int) $lead;
817
        }
818
        $url = '/lead/' . $lead . '/owner';
819
        $data = [
820
            'owner' => $newOwnerId,
821
            'options' => $options,
822
        ];
823
        $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
824
        return new Lead($response);
825
    }
826
827
    /**
828
     * @param Lead|int $lead
829
     * @return Comment[]
830
     * @throws exceptions\LPTrackerResponseException
831
     * @throws exceptions\LPTrackerServerException
832
     */
833 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...
834
    {
835
        if ($lead instanceof Lead) {
836
            $lead = $lead->getId();
837
        } else {
838
            $lead = (int) $lead;
839
        }
840
        $url = '/lead/' . $lead . '/comments';
841
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
842
        $result = [];
843
        foreach ($response as $commentData) {
844
            $result[] = new Comment($commentData);
845
        }
846
        return $result;
847
    }
848
849
    /**
850
     * @param Lead|int $lead
851
     * @param string $text
852
     * @param array $options
853
     * @return Comment
854
     * @throws exceptions\LPTrackerResponseException
855
     * @throws exceptions\LPTrackerServerException
856
     */
857 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...
858
    {
859
        if ($lead instanceof Lead) {
860
            $lead = $lead->getId();
861
        } else {
862
            $lead = (int) $lead;
863
        }
864
        $url = '/lead/' . $lead . '/comment';
865
        $data = [
866
            'text' => $text,
867
            'options' => $options,
868
        ];
869
        $response = LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address);
870
        return new Comment($response);
871
    }
872
873
    /**
874
     * @param Lead|int $lead
875
     * @param Custom|int $custom
876
     * @param string $absolutePath
877
     * @throws exceptions\LPTrackerResponseException
878
     * @throws exceptions\LPTrackerServerException
879
     */
880
    public function addFileToLead($lead, $custom, $absolutePath)
881
    {
882
        if ($lead instanceof Lead) {
883
            $lead = $lead->getId();
884
        } else {
885
            $lead = (int) $lead;
886
        }
887
        if ($custom instanceof Custom) {
888
            $custom = $custom->getId();
889
        } else {
890
            $custom = (int) $custom;
891
        }
892
        $url = '/lead/' . $lead . '/file';
893
        $data = [
894
            'name' => pathinfo($absolutePath, PATHINFO_BASENAME),
895
            'mime' => mime_content_type($absolutePath),
896
            'data' => base64_encode(file_get_contents($absolutePath)),
897
            'custom_field_id' => $custom,
898
        ];
899
        LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address);
900
    }
901
902
    /**
903
     * @param Custom $custom
904
     * @return Custom
905
     * @throws LPTrackerSDKException
906
     */
907
    public function saveLeadCustom(Custom $custom, array $options = [])
908
    {
909
        if (!$custom->validate() || empty($custom->getLeadId())) {
910
            throw new LPTrackerSDKException('Invalid custom');
911
        }
912
913
        $url = '/lead/' . $custom->getLeadId() . '/custom/' . $custom->getId();
914
        if ($custom->getValue() === null) {
915
            $data = [
916
                'options' => $options,
917
            ];
918
            LPTrackerRequest::sendRequest($url, $data, 'DELETE', $this->token, $this->address);
919
            $response = $custom->toArray();
920
            $response['value'] = null;
921 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...
922
            $data = [
923
                'value' => $custom->getValue(),
924
                'options' => $options,
925
            ];
926
            $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
927
        }
928
        return new Custom($response, $custom->getLeadId());
929
    }
930
931
    /**
932
     * @param Lead|int $lead
933
     * @param Custom|int $custom
934
     * @param mixed $newValue
935
     * @param array $options
936
     * @return Custom
937
     * @throws LPTrackerSDKException
938
     */
939
    public function editLeadCustom($lead, $custom, $newValue, array $options = [])
940
    {
941
        if ($lead instanceof Lead) {
942
            $lead = $lead->getId();
943
        } else {
944
            $lead = (int) $lead;
945
        }
946
        if ($custom instanceof Custom) {
947
            if (empty($newValue)) {
948
                $newValue = $custom->getValue();
949
            }
950
            $custom = $custom->getId();
951
        } else {
952
            $custom = (int) $custom;
953
        }
954
        $customModel = new Custom([
955
            'id' => $custom,
956
            'value' => $newValue,
957
        ], $lead);
958
        $customModel->validate();
959
        return $this->saveLeadCustom($customModel, $options);
960
    }
961
962
    /**
963
     * @param Project|int $project
964
     * @param array $options
965
     * @return CustomField
966
     * @throws exceptions\LPTrackerResponseException
967
     * @throws exceptions\LPTrackerServerException
968
     */
969 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...
970
    {
971
        if ($project instanceof Project) {
972
            $project = $project->getId();
973
        } else {
974
            $project = (int) $project;
975
        }
976
        $actionUrl = '/custom/' . $project . '/create';
977
        $response = LPTrackerRequest::sendRequest($actionUrl, $options, 'POST', $this->token, $this->address);
978
        return new CustomField($response);
979
    }
980
981
    /**
982
     * @param Project|int $project
983
     * @param int $offset
984
     * @param int $limit
985
     * @param array $sort
986
     * @param bool $isDeal
987
     * @param array $filter
988
     * @return Lead[]
989
     * @throws exceptions\LPTrackerResponseException
990
     * @throws exceptions\LPTrackerServerException
991
     */
992
    public function getLeads($project, $offset = null, $limit = null, $sort = [], $isDeal = false, $filter = [])
993
    {
994
        if ($project instanceof Project) {
995
            $project = $project->getId();
996
        } else {
997
            $project = (int) $project;
998
        }
999
        $actionUrl = '/lead/' . $project . '/list?' . http_build_query([
1000
            'offset' => $offset,
1001
            'limit' => $limit,
1002
            'sort' => $sort,
1003
            'is_deal' => $isDeal,
1004
            'filter' => $filter,
1005
        ]);
1006
        $response = LPTrackerRequest::sendRequest($actionUrl, [], 'GET', $this->token, $this->address);
1007
        $result = [];
1008
        foreach ($response as $lead) {
1009
            $result[] = new Lead($lead);
1010
        }
1011
        return $result;
1012
    }
1013
1014
    /**
1015
     * @param Project|int $project
1016
     * @param int $offset
1017
     * @param int $limit
1018
     * @param array $sort
1019
     * @param bool $isDeal
1020
     * @param array $filter
1021
     * @return Lead[]
1022
     * @throws exceptions\LPTrackerResponseException
1023
     * @throws exceptions\LPTrackerServerException
1024
     * @deprecated Use getLeads()
1025
     */
1026
    public function getLeadsList($project, $offset = null, $limit = null, $sort = [], $isDeal = false, $filter = [])
1027
    {
1028
        return $this->getLeads($project, $offset, $limit, $sort, $isDeal, $filter);
1029
    }
1030
}
1031