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 ( 35342b...619c64 )
by
unknown
07:24
created

LPTracker::addLeadPayment()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.439
c 0
b 0
f 0
cc 6
eloc 22
nc 10
nop 4
1
<?php
2
3
namespace LPTracker;
4
5
use LPTracker\models\Comment;
6
use LPTracker\models\Contact;
7
use LPTracker\models\ContactField;
8
use LPTracker\models\Custom;
9
use LPTracker\models\Lead;
10
use LPTracker\models\Project;
11
use LPTracker\exceptions\LPTrackerSDKException;
12
use LPTracker\authentication\AccessToken;
13
14
/**
15
 * Class LPTracker
16
 * @package LPTracker
17
 */
18
class LPTracker extends LPTrackerBase
19
{
20
21
    /**
22
     * @param        $login
23
     * @param        $password
24
     * @param string $serviceName
25
     *
26
     * @return AccessToken
27
     * @throws LPTrackerSDKException
28
     */
29
    public function login($login, $password, $serviceName = '')
30
    {
31
        if (empty($login)) {
32
            throw new LPTrackerSDKException('Login is empty');
33
        }
34
        if (empty($password)) {
35
            throw new LPTrackerSDKException('Password is empty');
36
        }
37
        if (empty($serviceName)) {
38
            $serviceName = LPTrackerBase::DEFAULT_SERVICE_NAME;
39
        }
40
41
        $response = LPTrackerRequest::sendRequest('/login', [
42
            'login'    => $login,
43
            'password' => $password,
44
            'service'  => $serviceName,
45
            'version'  => LPTrackerBase::VERSION
46
        ], 'POST', null, $this->address);
47
48
        $accessToken = new AccessToken($response['token']);
49
50
        return $accessToken;
51
    }
52
53
54
    /**
55
     *
56
     */
57
    public function logout()
58
    {
59
        LPTrackerRequest::sendRequest('/logout', [], 'POST', $this->token, $this->address);
60
    }
61
62
63
    /**
64
     * @return Project[]
65
     */
66 View Code Duplication
    public function getProjectList()
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...
67
    {
68
        $response = LPTrackerRequest::sendRequest('/projects', [], 'GET', $this->token, $this->address);
69
70
        $result = [];
71
        foreach ($response as $projectData) {
72
            $result[] = new Project($projectData);
73
        }
74
75
        return $result;
76
    }
77
78
79
    /**
80
     * @param $id
81
     *
82
     * @return Project
83
     */
84 View Code Duplication
    public function getProject($id)
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...
85
    {
86
        $url = '/project/'.$id;
87
88
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
89
90
        $project = new Project($response);
91
92
        return $project;
93
    }
94
95
96
    /**
97
     * @param $project
98
     *
99
     * @return Custom[]
100
     */
101 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...
102
    {
103
        if ($project instanceof Project) {
104
            $project = $project->getId();
105
        } else {
106
            $project = intval($project);
107
        }
108
109
        $url = '/project/'.$project.'/customs';
110
111
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
112
113
        $result = [];
114
        foreach ($response as $customData) {
115
            $result[] = new Custom($customData);
116
        }
117
118
        return $result;
119
    }
120
121
122
    /**
123
     * @param $project
124
     *
125
     * @return ContactField[]
126
     */
127 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...
128
    {
129
        if ($project instanceof Project) {
130
            $project = $project->getId();
131
        } else {
132
            $project = intval($project);
133
        }
134
135
        $url = '/project/'.$project.'/fields';
136
137
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
138
139
        $result = [];
140
        foreach ($response as $customData) {
141
            $result[] = new ContactField($customData);
142
        }
143
144
        return $result;
145
    }
146
147
148
    /**
149
     * @param       $project
150
     * @param array $details
151
     * @param array $contactData
152
     * @param array $fields
153
     *
154
     * @return Contact
155
     * @throws LPTrackerSDKException
156
     */
157
    public function createContact(
158
        $project,
159
        array $details,
160
        array $contactData = [],
161
        array $fields = []
162
    ) {
163
        if (empty($details)) {
164
            throw new LPTrackerSDKException('Contact details can not be empty');
165
        }
166
167
        if ($project instanceof Project) {
168
            $project = $project->getId();
169
        } else {
170
            $project = intval($project);
171
        }
172
173
        $contactData['project_id'] = $project;
174
        $contactData['details'] = $details;
175
176
        $fieldsArr = [];
177
        foreach ($fields as $fieldId => $fieldValue) {
178
            if ($fieldValue instanceof ContactField) {
179
                $fieldId = $fieldValue->getId();
180
                $fieldValue = $fieldValue->getValue();
181
            }
182
183
            $fieldsArr[$fieldId] = $fieldValue;
184
        }
185
186
        $contact = new Contact($contactData);
187
        $contact->validate();
188
189
        $data = $contact->toArray();
190
        $data['fields'] = $fieldsArr;
191
192
        $response = LPTrackerRequest::sendRequest('/contact', $data, 'POST', $this->token, $this->address);
193
194
        $resultContact = new Contact($response);
195
196
        return $resultContact;
197
    }
198
199
200
    /**
201
     * @param $contact
202
     *
203
     * @return Contact
204
     * @throws LPTrackerSDKException
205
     */
206 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...
207
    {
208
        if ($contact instanceof Contact) {
209
            $contact = $contact->getId();
210
        } else {
211
            $contact = intval($contact);
212
        }
213
214
        if ($contact <= 0) {
215
            throw new LPTrackerSDKException('Invalid contact ID');
216
        }
217
218
        $url = '/contact/'.$contact;
219
220
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
221
222
        $resultContact = new Contact($response);
223
224
        return $resultContact;
225
    }
226
227
228
    /**
229
     * @param Contact $contact
230
     *
231
     * @return Contact
232
     * @throws LPTrackerSDKException
233
     */
234
    public function saveContact(Contact $contact)
235
    {
236
        if ( ! $contact->validate()) {
237
            throw new LPTrackerSDKException('Invalid contact');
238
        }
239
240
        $data = $contact->toArray();
241
        if ( ! empty($data['fields'])) {
242
            $fields = [];
243
            foreach ($data['fields'] as $field) {
244
                $fields[$field['id']] = $field['value'];
245
            }
246
            $data['fields'] = $fields;
247
        }
248
249
        if ($contact->getId() > 0) {
250
            $url = '/contact/'.$contact->getId();
251
252
            $response = LPTrackerRequest::sendRequest($url, $contact->toArray(), 'PUT', $this->token, $this->address);
253
        } else {
254
            $response = LPTrackerRequest::sendRequest('/contact', $contact->toArray(), 'POST', $this->token,
255
                $this->address);
256
        }
257
258
        $resultContact = new Contact($response);
259
260
        return $resultContact;
261
    }
262
263
264
    /**
265
     * @param       $contactId
266
     * @param array $details
267
     * @param array $contactData
268
     * @param array $fields
269
     *
270
     * @return Contact
271
     * @throws LPTrackerSDKException
272
     */
273
    public function editContact(
274
        $contactId,
275
        array $details,
276
        array $contactData = [],
277
        array $fields = []
278
    ) {
279
        if (empty($details)) {
280
            throw new LPTrackerSDKException('Contact details can not be empty');
281
        }
282
283
        $contactData['id'] = $contactId;
284
        $contactData['details'] = $details;
285
286
        foreach ($fields as $fieldId => $fieldValue) {
287
            if ($fieldValue instanceof ContactField) {
288
                $fieldId = $fieldValue->getId();
289
                $fieldValue = $fieldValue->getValue();
290
            }
291
292
            $contactData['fields'][] = [
293
                'id'    => $fieldId,
294
                'value' => $fieldValue
295
            ];
296
        }
297
298
        $contact = new Contact($contactData);
299
        $contact->validate();
300
301
        return $this->saveContact($contact);
302
    }
303
304
305
    /**
306
     * @param       $project
307
     * @param array $searchOptions
308
     *
309
     * @return array
310
     * @throws LPTrackerSDKException
311
     */
312
    public function searchContacts($project, array $searchOptions = [])
313
    {
314
        if ($project instanceof Project) {
315
            $project = $project->getId();
316
        }
317
        $project = intval($project);
318
        if ($project <= 0) {
319
            throw new LPTrackerSDKException('Invalid project id');
320
        }
321
322
        $url = '/contact/search?';
323
324
        $data = [
325
            'project_id' => $project
326
        ];
327
        if (isset($searchOptions['email'])) {
328
            $data['email'] = $searchOptions['email'];
329
        }
330
        if (isset($searchOptions['phone'])) {
331
            $data['phone'] = $searchOptions['phone'];
332
        }
333
334
        $url = $url.http_build_query($data);
335
336
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
337
338
        $result = [];
339
        foreach ($response as $contactData) {
340
            $result[] = new Contact($contactData);
341
        }
342
343
        return $result;
344
    }
345
346
347
    /**
348
     * @param $contact
349
     *
350
     * @return array
351
     * @throws LPTrackerSDKException
352
     */
353
    public function contactLeads($contact)
354
    {
355
        if ($contact instanceof Contact) {
356
            $contact = $contact->getId();
357
        }
358
        $contact = intval($contact);
359
        if ($contact <= 0) {
360
            throw new LPTrackerSDKException('Invalid contact id');
361
        }
362
363
        $url = '/contact/'.$contact.'/leads';
364
365
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
366
367
        $result = [];
368
        foreach ($response as $leadData) {
369
            $result[] = new Lead($leadData);
370
        }
371
372
        return $result;
373
    }
374
375
376
    /**
377
     * @param $contact
378
     * @param $field
379
     * @param $newValue
380
     *
381
     * @return ContactField
382
     */
383
    public function updateContactField($contact, $field, $newValue)
384
    {
385
        if ($contact instanceof Contact) {
386
            $contact = $contact->getId();
387
        }
388
        if ($field instanceof ContactField) {
389
            $field = $field->getId();
390
        }
391
392
        $url = '/contact/'.$contact.'/field/'.$field;
393
394
        $data = [
395
            'value' => $newValue
396
        ];
397
398
        $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
399
400
        $contactField = new ContactField($response);
401
402
        return $contactField;
403
    }
404
405
406
    /**
407
     * @param       $contact
408
     * @param array $leadData
409
     * @param array $options
410
     *
411
     * @return Lead
412
     * @throws LPTrackerSDKException
413
     */
414
    public function createLead($contact, array $leadData = [], array $options = [])
415
    {
416
        if ($contact instanceof Contact) {
417
            $leadData['contact_id'] = $contact->getId();
418
        } else {
419
            $leadData['contact_id'] = intval($contact);
420
        }
421
422
        $lead = new Lead($leadData);
423
        if ( ! $lead->validate()) {
424
            throw new LPTrackerSDKException('Invalid lead data');
425
        }
426
427
        $data = $lead->toArray();
428
        if (isset($options['callback'])) {
429
            $data['callback'] = $options['callback'] ? true : false;
430
        }
431
432
        $response = LPTrackerRequest::sendRequest('/lead', $data, 'POST', $this->token, $this->address);
433
434
        $resultLead = new Lead($response);
435
436
        return $resultLead;
437
    }
438
439
440
    /**
441
     * @param $lead
442
     *
443
     * @return Lead
444
     * @throws LPTrackerSDKException
445
     */
446 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...
447
    {
448
        if ($lead instanceof Lead) {
449
            $lead = $lead->getId();
450
        } else {
451
            $lead = intval($lead);
452
        }
453
454
        if ($lead <= 0) {
455
            throw new LPTrackerSDKException('Invalid lead ID');
456
        }
457
458
        $url = '/lead/'.$lead;
459
460
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
461
462
        $resultLead = new Lead($response);
463
464
        return $resultLead;
465
    }
466
467
468
    /**
469
     * @param Lead $lead
470
     *
471
     * @return Lead
472
     * @throws LPTrackerSDKException
473
     */
474
    public function saveLead(Lead $lead)
475
    {
476
        if ( ! $lead->validate()) {
477
            throw new LPTrackerSDKException('Invalid lead');
478
        }
479
480
        if ($lead->getId() > 0) {
481
            $url = '/lead/'.$lead->getId();
482
483
            $response = LPTrackerRequest::sendRequest($url, $lead->toArray(), 'PUT', $this->token, $this->address);
484
        } else {
485
            $response = LPTrackerRequest::sendRequest('/lead', $lead->toArray(), 'POST', $this->token, $this->address);
486
        }
487
488
        $resultLead = new Lead($response);
489
490
        return $resultLead;
491
    }
492
493
494
    /**
495
     * @param       $leadId
496
     * @param array $leadData
497
     *
498
     * @return Lead
499
     */
500
    public function editLead($leadId, array $leadData = [])
501
    {
502
        $leadData['id'] = $leadId;
503
504
        $lead = new Lead($leadData);
505
        $lead->validate();
506
507
        return $this->saveLead($lead);
508
    }
509
510
511
    /**
512
     * @param        $lead
513
     * @param string $category
514
     * @param string $purpose
515
     * @param float  $sum
516
     *
517
     * @return Lead
518
     * @throws LPTrackerSDKException
519
     */
520
    public function addLeadPayment($lead, $category, $purpose, $sum)
521
    {
522
        if ($lead instanceof Lead) {
523
            $lead = $lead->getId();
524
        } else {
525
            $lead = intval($lead);
526
        }
527
528
        if (empty($category)) {
529
            throw new LPTrackerSDKException('Category can not be empty');
530
        }
531
        if (empty($purpose)) {
532
            throw new LPTrackerSDKException('Purpose can not be empty');
533
        }
534
        $sum = floatval($sum);
535
536
        if ($sum <= 0) {
537
            throw new LPTrackerSDKException('Invalid sum');
538
        }
539
540
        if ($lead <= 0) {
541
            throw new LPTrackerSDKException('Invalid lead ID');
542
        }
543
544
        $url = '/lead/'.$lead.'/payment';
545
546
        $data = [
547
            'category' => $category,
548
            'purpose'  => $purpose,
549
            'sum'      => $sum,
550
        ];
551
552
        $response = LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address);
553
554
        $resultLead = new Lead($response);
555
556
        return $resultLead;
557
    }
558
559
560
    /**
561
     * @param $lead
562
     *
563
     * @return Comment[]
564
     */
565 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...
566
    {
567
        if ($lead instanceof Lead) {
568
            $lead = $lead->getId();
569
        } else {
570
            $lead = intval($lead);
571
        }
572
573
        $url = '/lead/'.$lead.'/comments';
574
575
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
576
577
        $result = [];
578
        foreach ($response as $commentData) {
579
            $result[] = new Comment($commentData);
580
        }
581
582
        return $result;
583
    }
584
585
586
    /**
587
     * @param $lead
588
     * @param $text
589
     *
590
     * @return Comment
591
     */
592 View Code Duplication
    public function addCommentToLead($lead, $text)
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...
593
    {
594
        if ($lead instanceof Lead) {
595
            $lead = $lead->getId();
596
        } else {
597
            $lead = intval($lead);
598
        }
599
600
        $url = '/lead/'.$lead.'/comment';
601
602
        $data = [
603
            'text' => $text
604
        ];
605
606
        $response = LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address);
607
608
        $comment = new Comment($response);
609
610
        return $comment;
611
    }
612
613
614
    /**
615
     * @param Custom $custom
616
     *
617
     * @return Custom
618
     * @throws LPTrackerSDKException
619
     */
620
    public function saveLeadCustom(Custom $custom)
621
    {
622
        if ( ! $custom->validate() || empty($custom->getLeadId())) {
623
            throw new LPTrackerSDKException('Invalid custom');
624
        }
625
626
        $url = '/lead/'.$custom->getLeadId().'/custom/'.$custom->getId();
627
628
        $data = [
629
            'value' => $custom->getValue()
630
        ];
631
632
        $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
633
634
        $resultCustom = new Custom($response);
635
636
        return $resultCustom;
637
    }
638
639
640
    /**
641
     * @param $lead
642
     * @param $custom
643
     * @param $newValue
644
     *
645
     * @return Custom
646
     */
647
    public function editLeadCustom($lead, $custom, $newValue)
648
    {
649
        if ($lead instanceof Lead) {
650
            $lead = $lead->getId();
651
        } else {
652
            $lead = intval($lead);
653
        }
654
655
        if ($custom instanceof Custom) {
656
            if (empty($newValue)) {
657
                $newValue = $custom->getValue();
658
            }
659
            $custom = $custom->getId();
660
        } else {
661
            $custom = intval($custom);
662
        }
663
664
        $customModel = new Custom([
665
            'id'      => $custom,
666
            'lead_id' => $lead,
667
            'value'   => $newValue
668
        ]);
669
        $customModel->validate();
670
671
        return $this->saveLeadCustom($customModel);
672
    }
673
}