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 ( 3999e5...623d84 )
by Sergey
01:31
created

LPTracker   F

Complexity

Total Complexity 131

Size/Duplication

Total Lines 1010
Duplicated Lines 18.81 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 0
Metric Value
wmc 131
lcom 1
cbo 15
dl 190
loc 1010
rs 1.56
c 0
b 0
f 0

43 Methods

Rating   Name   Duplication   Size   Complexity  
A logout() 0 4 1
A login() 0 27 4
A getProjects() 9 9 2
A getProject() 0 11 2
A getProjectList() 0 4 1
A getProjectCustoms() 15 15 3
A getProjectStages() 15 15 3
A getProjectFields() 15 15 3
A getEmployees() 9 9 2
A createContact() 0 32 5
A getContact() 0 15 3
A saveContact() 0 22 5
A editContact() 0 31 5
B searchContacts() 0 28 6
A getContactLeads() 0 19 4
A contactLeads() 0 4 1
A editContactField() 0 20 3
A updateContactField() 0 4 1
A saveContactField() 6 19 4
A createView() 0 16 3
A getView() 0 15 3
A saveView() 14 14 3
A editView() 0 11 2
B createLead() 0 29 10
A getLead() 0 15 3
B getCustomFile() 0 27 6
A saveLead() 20 20 3
A editLead() 0 11 2
B addPaymentToLead() 0 33 6
A addLeadPayment() 0 4 1
A setProjectCallbackUrl() 10 10 2
A callLead() 10 10 2
A editLeadStage() 15 15 2
A changeLeadFunnel() 0 4 1
A editLeadOwner() 15 15 2
A getLeadComments() 15 15 3
A addCommentToLead() 15 15 2
A addFileToLead() 0 21 3
A saveLeadCustom() 7 23 4
A editLeadCustom() 0 22 4
A createCustom() 0 11 2
A getLeads() 0 21 3
A getLeadsList() 0 4 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like LPTracker often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use LPTracker, and based on these observations, apply Extract Interface, too.

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
    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
     */
756 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...
757
    {
758
        if ($lead instanceof Lead) {
759
            $lead = $lead->getId();
760
        } else {
761
            $lead = (int) $lead;
762
        }
763
        $url = '/lead/' . $lead . '/call';
764
        LPTrackerRequest::sendRequest($url, [], 'POST', $this->token, $this->address);
765
    }
766
767
    /**
768
     * @param Lead|int $lead
769
     * @param int $newStageId
770
     * @param array $options
771
     * @return Lead
772
     * @throws LPTrackerSDKException
773
     */
774 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...
775
    {
776
        if ($lead instanceof Lead) {
777
            $lead = $lead->getId();
778
        } else {
779
            $lead = (int) $lead;
780
        }
781
        $url = '/lead/' . $lead . '/funnel';
782
        $data = [
783
            'funnel' => $newStageId,
784
            'options' => $options,
785
        ];
786
        $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
787
        return new Lead($response);
788
    }
789
790
    /**
791
     * @param Lead|int $lead
792
     * @param int $newFunnelId
793
     * @return Lead
794
     * @throws LPTrackerSDKException
795
     * @deprecated Use editLeadStage()
796
     */
797
    public function changeLeadFunnel($lead, $newFunnelId)
798
    {
799
        return $this->editLeadStage($lead, $newFunnelId);
800
    }
801
802
    /**
803
     * @param Lead|int $lead
804
     * @param int $newOwnerId
805
     * @param array $options
806
     * @return Lead
807
     * @throws LPTrackerSDKException
808
     */
809 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...
810
    {
811
        if ($lead instanceof Lead) {
812
            $lead = $lead->getId();
813
        } else {
814
            $lead = (int) $lead;
815
        }
816
        $url = '/lead/' . $lead . '/owner';
817
        $data = [
818
            'owner' => $newOwnerId,
819
            'options' => $options,
820
        ];
821
        $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
822
        return new Lead($response);
823
    }
824
825
    /**
826
     * @param Lead|int $lead
827
     * @return Comment[]
828
     * @throws exceptions\LPTrackerResponseException
829
     * @throws exceptions\LPTrackerServerException
830
     */
831 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...
832
    {
833
        if ($lead instanceof Lead) {
834
            $lead = $lead->getId();
835
        } else {
836
            $lead = (int) $lead;
837
        }
838
        $url = '/lead/' . $lead . '/comments';
839
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
840
        $result = [];
841
        foreach ($response as $commentData) {
842
            $result[] = new Comment($commentData);
843
        }
844
        return $result;
845
    }
846
847
    /**
848
     * @param Lead|int $lead
849
     * @param string $text
850
     * @param array $options
851
     * @return Comment
852
     * @throws exceptions\LPTrackerResponseException
853
     * @throws exceptions\LPTrackerServerException
854
     */
855 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...
856
    {
857
        if ($lead instanceof Lead) {
858
            $lead = $lead->getId();
859
        } else {
860
            $lead = (int) $lead;
861
        }
862
        $url = '/lead/' . $lead . '/comment';
863
        $data = [
864
            'text' => $text,
865
            'options' => $options,
866
        ];
867
        $response = LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address);
868
        return new Comment($response);
869
    }
870
871
    /**
872
     * @param Lead|int $lead
873
     * @param Custom|int $custom
874
     * @param string $absolutePath
875
     * @throws exceptions\LPTrackerResponseException
876
     * @throws exceptions\LPTrackerServerException
877
     */
878
    public function addFileToLead($lead, $custom, $absolutePath)
879
    {
880
        if ($lead instanceof Lead) {
881
            $lead = $lead->getId();
882
        } else {
883
            $lead = (int) $lead;
884
        }
885
        if ($custom instanceof Custom) {
886
            $custom = $custom->getId();
887
        } else {
888
            $custom = (int) $custom;
889
        }
890
        $url = '/lead/' . $lead . '/file';
891
        $data = [
892
            'name' => pathinfo($absolutePath, PATHINFO_BASENAME),
893
            'mime' => mime_content_type($absolutePath),
894
            'data' => base64_encode(file_get_contents($absolutePath)),
895
            'custom_field_id' => $custom,
896
        ];
897
        LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address);
898
    }
899
900
    /**
901
     * @param Custom $custom
902
     * @return Custom
903
     * @throws LPTrackerSDKException
904
     */
905
    public function saveLeadCustom(Custom $custom, array $options = [])
906
    {
907
        if (!$custom->validate() || empty($custom->getLeadId())) {
908
            throw new LPTrackerSDKException('Invalid custom');
909
        }
910
911
        $url = '/lead/' . $custom->getLeadId() . '/custom/' . $custom->getId();
912
        if ($custom->getValue() === null) {
913
            $data = [
914
                'options' => $options,
915
            ];
916
            LPTrackerRequest::sendRequest($url, $data, 'DELETE', $this->token, $this->address);
917
            $response = $custom->toArray();
918
            $response['value'] = null;
919 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...
920
            $data = [
921
                'value' => $custom->getValue(),
922
                'options' => $options,
923
            ];
924
            $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
925
        }
926
        return new Custom($response, $custom->getLeadId());
927
    }
928
929
    /**
930
     * @param Lead|int $lead
931
     * @param Custom|int $custom
932
     * @param mixed $newValue
933
     * @param array $options
934
     * @return Custom
935
     * @throws LPTrackerSDKException
936
     */
937
    public function editLeadCustom($lead, $custom, $newValue, array $options = [])
938
    {
939
        if ($lead instanceof Lead) {
940
            $lead = $lead->getId();
941
        } else {
942
            $lead = (int) $lead;
943
        }
944
        if ($custom instanceof Custom) {
945
            if (empty($newValue)) {
946
                $newValue = $custom->getValue();
947
            }
948
            $custom = $custom->getId();
949
        } else {
950
            $custom = (int) $custom;
951
        }
952
        $customModel = new Custom([
953
            'id' => $custom,
954
            'value' => $newValue,
955
        ], $lead);
956
        $customModel->validate();
957
        return $this->saveLeadCustom($customModel, $options);
958
    }
959
960
    /**
961
     * @param Project|int $project
962
     * @param array $options
963
     * @return CustomField
964
     * @throws exceptions\LPTrackerResponseException
965
     * @throws exceptions\LPTrackerServerException
966
     */
967
    public function createCustom($project, $options)
968
    {
969
        if ($project instanceof Project) {
970
            $project = $project->getId();
971
        } else {
972
            $project = (int) $project;
973
        }
974
        $actionUrl = '/custom/' . $project . '/create';
975
        $response = LPTrackerRequest::sendRequest($actionUrl, $options, 'POST', $this->token, $this->address);
976
        return new CustomField($response);
977
    }
978
979
    /**
980
     * @param Project|int $project
981
     * @param int $offset
982
     * @param int $limit
983
     * @param array $sort
984
     * @param bool $isDeal
985
     * @param array $filter
986
     * @return Lead[]
987
     * @throws exceptions\LPTrackerResponseException
988
     * @throws exceptions\LPTrackerServerException
989
     */
990
    public function getLeads($project, $offset = null, $limit = null, $sort = [], $isDeal = false, $filter = [])
991
    {
992
        if ($project instanceof Project) {
993
            $project = $project->getId();
994
        } else {
995
            $project = (int) $project;
996
        }
997
        $actionUrl = '/lead/' . $project . '/list?' . http_build_query([
998
            'offset' => $offset,
999
            'limit' => $limit,
1000
            'sort' => $sort,
1001
            'is_deal' => $isDeal,
1002
            'filter' => $filter,
1003
        ]);
1004
        $response = LPTrackerRequest::sendRequest($actionUrl, [], 'GET', $this->token, $this->address);
1005
        $result = [];
1006
        foreach ($response as $lead) {
1007
            $result[] = new Lead($lead);
1008
        }
1009
        return $result;
1010
    }
1011
1012
    /**
1013
     * @param Project|int $project
1014
     * @param int $offset
1015
     * @param int $limit
1016
     * @param array $sort
1017
     * @param bool $isDeal
1018
     * @param array $filter
1019
     * @return Lead[]
1020
     * @throws exceptions\LPTrackerResponseException
1021
     * @throws exceptions\LPTrackerServerException
1022
     * @deprecated Use getLeads()
1023
     */
1024
    public function getLeadsList($project, $offset = null, $limit = null, $sort = [], $isDeal = false, $filter = [])
1025
    {
1026
        return $this->getLeads($project, $offset, $limit, $sort, $isDeal, $filter);
1027
    }
1028
}
1029