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 ( 5345ef...b3f137 )
by Sergey
01:27
created

LPTracker   F

Complexity

Total Complexity 124

Size/Duplication

Total Lines 951
Duplicated Lines 16.51 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 0
Metric Value
wmc 124
lcom 1
cbo 15
dl 157
loc 951
rs 1.649
c 0
b 0
f 0

40 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 setProjectCallbackUrl() 0 10 2
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 updateContactField() 0 19 3
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 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() 0 21 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
    public function setProjectCallbackUrl($project, $callbackUrl)
182
    {
183
        if ($project instanceof Project) {
184
            $project = $project->getId();
185
        } else {
186
            $project = (int) $project;
187
        }
188
        $url = '/project/' . $project . '/callback-url';
189
        LPTrackerRequest::sendRequest($url, ['url' => $callbackUrl], 'PUT', $this->token, $this->address);
190
    }
191
192
    /**
193
     * @return Employee[]
194
     * @throws exceptions\LPTrackerResponseException
195
     * @throws exceptions\LPTrackerServerException
196
     */
197 View Code Duplication
    public function getEmployees()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

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