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.

LPTracker::saveView()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

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