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 ( 9c25ca...998aed )
by
unknown
01:29
created

LPTracker::saveContact()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
cc 5
eloc 17
nc 5
nop 1
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
use LPTracker\models\View;
14
15
/**
16
 * Class LPTracker
17
 * @package LPTracker
18
 */
19
class LPTracker extends LPTrackerBase
20
{
21
22
    /**
23
     * @param        $login
24
     * @param        $password
25
     * @param string $serviceName
26
     *
27
     * @return AccessToken
28
     * @throws LPTrackerSDKException
29
     */
30
    public function login($login, $password, $serviceName = '')
31
    {
32
        if (empty($login)) {
33
            throw new LPTrackerSDKException('Login is empty');
34
        }
35
        if (empty($password)) {
36
            throw new LPTrackerSDKException('Password is empty');
37
        }
38
        if (empty($serviceName)) {
39
            $serviceName = LPTrackerBase::DEFAULT_SERVICE_NAME;
40
        }
41
42
        $response = LPTrackerRequest::sendRequest('/login', [
43
            'login'    => $login,
44
            'password' => $password,
45
            'service'  => $serviceName,
46
            'version'  => LPTrackerBase::VERSION
47
        ], 'POST', null, $this->address);
48
49
        $accessToken = new AccessToken($response['token']);
50
51
        return $accessToken;
52
    }
53
54
55
    /**
56
     *
57
     */
58
    public function logout()
59
    {
60
        LPTrackerRequest::sendRequest('/logout', [], 'POST', $this->token, $this->address);
61
    }
62
63
64
    /**
65
     * @return Project[]
66
     */
67 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...
68
    {
69
        $response = LPTrackerRequest::sendRequest('/projects', [], 'GET', $this->token, $this->address);
70
71
        $result = [];
72
        foreach ($response as $projectData) {
73
            $result[] = new Project($projectData);
74
        }
75
76
        return $result;
77
    }
78
79
80
    /**
81
     * @param $id
82
     *
83
     * @return Project
84
     */
85 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...
86
    {
87
        $url = '/project/'.$id;
88
89
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
90
91
        $project = new Project($response);
92
93
        return $project;
94
    }
95
96
97
    /**
98
     * @param $project
99
     *
100
     * @return Custom[]
101
     */
102 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...
103
    {
104
        if ($project instanceof Project) {
105
            $project = $project->getId();
106
        } else {
107
            $project = intval($project);
108
        }
109
110
        $url = '/project/'.$project.'/customs';
111
112
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
113
114
        $result = [];
115
        foreach ($response as $customData) {
116
            $result[] = new Custom($customData);
117
        }
118
119
        return $result;
120
    }
121
122
123
    /**
124
     * @param $project
125
     *
126
     * @return ContactField[]
127
     */
128 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...
129
    {
130
        if ($project instanceof Project) {
131
            $project = $project->getId();
132
        } else {
133
            $project = intval($project);
134
        }
135
136
        $url = '/project/'.$project.'/fields';
137
138
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
139
140
        $result = [];
141
        foreach ($response as $customData) {
142
            $result[] = new ContactField($customData);
143
        }
144
145
        return $result;
146
    }
147
148
149
    /**
150
     * @param       $project
151
     * @param array $details
152
     * @param array $contactData
153
     * @param array $fields
154
     *
155
     * @return Contact
156
     * @throws LPTrackerSDKException
157
     */
158
    public function createContact(
159
        $project,
160
        array $details,
161
        array $contactData = [],
162
        array $fields = []
163
    ) {
164
        if (empty($details)) {
165
            throw new LPTrackerSDKException('Contact details can not be empty');
166
        }
167
168
        if ($project instanceof Project) {
169
            $project = $project->getId();
170
        } else {
171
            $project = intval($project);
172
        }
173
174
        $contactData['project_id'] = $project;
175
        $contactData['details'] = $details;
176
177
        $fieldsArr = [];
178
        foreach ($fields as $fieldId => $fieldValue) {
179
            if ($fieldValue instanceof ContactField) {
180
                $fieldId = $fieldValue->getId();
181
                $fieldValue = $fieldValue->getValue();
182
            }
183
184
            $fieldsArr[$fieldId] = $fieldValue;
185
        }
186
187
        $contact = new Contact($contactData);
188
        $contact->validate();
189
190
        $data = $contact->toArray();
191
        $data['fields'] = $fieldsArr;
192
193
        $response = LPTrackerRequest::sendRequest('/contact', $data, 'POST', $this->token, $this->address);
194
195
        $resultContact = new Contact($response);
196
197
        return $resultContact;
198
    }
199
200
201
    /**
202
     * @param $contact
203
     *
204
     * @return Contact
205
     * @throws LPTrackerSDKException
206
     */
207 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...
208
    {
209
        if ($contact instanceof Contact) {
210
            $contact = $contact->getId();
211
        } else {
212
            $contact = intval($contact);
213
        }
214
215
        if ($contact <= 0) {
216
            throw new LPTrackerSDKException('Invalid contact ID');
217
        }
218
219
        $url = '/contact/'.$contact;
220
221
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
222
223
        $resultContact = new Contact($response);
224
225
        return $resultContact;
226
    }
227
228
229
    /**
230
     * @param Contact $contact
231
     *
232
     * @return Contact
233
     * @throws LPTrackerSDKException
234
     */
235
    public function saveContact(Contact $contact)
236
    {
237
        if ( ! $contact->validate()) {
238
            throw new LPTrackerSDKException('Invalid contact');
239
        }
240
241
        $data = $contact->toArray();
242
        if ( ! empty($data['fields'])) {
243
            $fields = [];
244
            foreach ($data['fields'] as $field) {
245
                $fields[$field['id']] = $field['value'];
246
            }
247
            $data['fields'] = $fields;
248
        }
249
250
        if ($contact->getId() > 0) {
251
            $url = '/contact/'.$contact->getId();
252
253
            $response = LPTrackerRequest::sendRequest($url, $contact->toArray(), 'PUT', $this->token, $this->address);
254
        } else {
255
            $response = LPTrackerRequest::sendRequest('/contact', $contact->toArray(), 'POST', $this->token,
256
                $this->address);
257
        }
258
259
        $resultContact = new Contact($response);
260
261
        return $resultContact;
262
    }
263
264
265
    /**
266
     * @param       $contactId
267
     * @param array $details
268
     * @param array $contactData
269
     * @param array $fields
270
     *
271
     * @return Contact
272
     * @throws LPTrackerSDKException
273
     */
274
    public function editContact(
275
        $contactId,
276
        array $details,
277
        array $contactData = [],
278
        array $fields = []
279
    ) {
280
        if (empty($details)) {
281
            throw new LPTrackerSDKException('Contact details can not be empty');
282
        }
283
284
        $contactData['id'] = $contactId;
285
        $contactData['details'] = $details;
286
287
        foreach ($fields as $fieldId => $fieldValue) {
288
            if ($fieldValue instanceof ContactField) {
289
                $fieldId = $fieldValue->getId();
290
                $fieldValue = $fieldValue->getValue();
291
            }
292
293
            $contactData['fields'][] = [
294
                'id'    => $fieldId,
295
                'value' => $fieldValue
296
            ];
297
        }
298
299
        $contact = new Contact($contactData);
300
        $contact->validate();
301
302
        return $this->saveContact($contact);
303
    }
304
305
306
    /**
307
     * @param       $project
308
     * @param array $searchOptions
309
     *
310
     * @return array
311
     * @throws LPTrackerSDKException
312
     */
313
    public function searchContacts($project, array $searchOptions = [])
314
    {
315
        if ($project instanceof Project) {
316
            $project = $project->getId();
317
        }
318
        $project = intval($project);
319
        if ($project <= 0) {
320
            throw new LPTrackerSDKException('Invalid project id');
321
        }
322
323
        $url = '/contact/search?';
324
325
        $data = [
326
            'project_id' => $project
327
        ];
328
        if (isset($searchOptions['email'])) {
329
            $data['email'] = $searchOptions['email'];
330
        }
331
        if (isset($searchOptions['phone'])) {
332
            $data['phone'] = $searchOptions['phone'];
333
        }
334
335
        $url = $url.http_build_query($data);
336
337
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
338
339
        $result = [];
340
        foreach ($response as $contactData) {
341
            $result[] = new Contact($contactData);
342
        }
343
344
        return $result;
345
    }
346
347
348
    /**
349
     * @param $contact
350
     *
351
     * @return array
352
     * @throws LPTrackerSDKException
353
     */
354
    public function contactLeads($contact)
355
    {
356
        if ($contact instanceof Contact) {
357
            $contact = $contact->getId();
358
        }
359
        $contact = intval($contact);
360
        if ($contact <= 0) {
361
            throw new LPTrackerSDKException('Invalid contact id');
362
        }
363
364
        $url = '/contact/'.$contact.'/leads';
365
366
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
367
368
        $result = [];
369
        foreach ($response as $leadData) {
370
            $result[] = new Lead($leadData);
371
        }
372
373
        return $result;
374
    }
375
376
377
    /**
378
     * @param $contact
379
     * @param $field
380
     * @param $newValue
381
     *
382
     * @return ContactField
383
     */
384
    public function updateContactField($contact, $field, $newValue)
385
    {
386
        if ($contact instanceof Contact) {
387
            $contact = $contact->getId();
388
        }
389
        if ($field instanceof ContactField) {
390
            $field = $field->getId();
391
        }
392
393
        $url = '/contact/'.$contact.'/field/'.$field;
394
395
        $data = [
396
            'value' => $newValue
397
        ];
398
399
        $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
400
401
        $contactField = new ContactField($response);
402
403
        return $contactField;
404
    }
405
406
407
    /**
408
     * @param       $project
409
     * @param array $viewData
410
     *
411
     * @return View
412
     * @throws LPTrackerSDKException
413
     */
414
    public function createView($project, array $viewData = [])
415
    {
416
        if ($project instanceof Project) {
417
            $viewData['project_id'] = $project->getId();
418
        } else {
419
            $viewData['project_id'] = intval($project);
420
        }
421
422
        $view = new View($viewData);
423
        if ( ! $view->validate()) {
424
            throw new LPTrackerSDKException('Invalid view data');
425
        }
426
427
        $data = $view->toArray();
428
429
        $response = LPTrackerRequest::sendRequest('/view', $data, 'POST', $this->token, $this->address);
430
431
        $resultView = new View($response);
432
433
        return $resultView;
434
    }
435
436
437
    /**
438
     * @param $view
439
     *
440
     * @return View
441
     * @throws LPTrackerSDKException
442
     */
443 View Code Duplication
    public function getView($view)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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

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

Loading history...
444
    {
445
        if ($view instanceof View) {
446
            $view = $view->getId();
447
        } else {
448
            $view = intval($view);
449
        }
450
451
        if ($view <= 0) {
452
            throw new LPTrackerSDKException('Invalid view ID');
453
        }
454
455
        $url = '/view/'.$view;
456
457
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
458
459
        $resultView = new View($response);
460
461
        return $resultView;
462
    }
463
464
465
    /**
466
     * @param View $view
467
     *
468
     * @return View
469
     * @throws LPTrackerSDKException
470
     */
471 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...
472
    {
473
        if ( ! $view->validate()) {
474
            throw new LPTrackerSDKException('Invalid view');
475
        }
476
477
        if ($view->getId() > 0) {
478
            $url = '/view/'.$view->getId();
479
480
            $response = LPTrackerRequest::sendRequest($url, $view->toArray(), 'PUT', $this->token, $this->address);
481
        } else {
482
            $response = LPTrackerRequest::sendRequest('/view', $view->toArray(), 'POST', $this->token, $this->address);
483
        }
484
485
        $resultView = new View($response);
486
487
        return $resultView;
488
    }
489
490
491
    /**
492
     * @param       $viewId
493
     * @param array $viewData
494
     *
495
     * @return View
496
     */
497
    public function editView($viewId, array $viewData = [])
498
    {
499
        $viewData['id'] = $viewId;
500
501
        $view = new View($viewData);
502
        $view->validate();
503
504
        return $this->saveView($view);
505
    }
506
507
508
    /**
509
     * @param       $contact
510
     * @param array $leadData
511
     * @param array $options
512
     *
513
     * @return Lead
514
     * @throws LPTrackerSDKException
515
     */
516
    public function createLead($contact, array $leadData = [], array $options = [])
517
    {
518
        if ($contact instanceof Contact) {
519
            $leadData['contact_id'] = $contact->getId();
520
        } else {
521
            $leadData['contact_id'] = intval($contact);
522
        }
523
524
        $lead = new Lead($leadData);
525
        if ( ! $lead->validate()) {
526
            throw new LPTrackerSDKException('Invalid lead data');
527
        }
528
529
        $data = $lead->toArray();
530
        if (isset($options['callback'])) {
531
            $data['callback'] = $options['callback'] ? true : false;
532
        }
533
        if (isset($leadData['view_id'])) {
534
            $data['view_id'] = intval($leadData['view_id']) ;
535
        }
536
537
        $response = LPTrackerRequest::sendRequest('/lead', $data, 'POST', $this->token, $this->address);
538
539
        $resultLead = new Lead($response);
540
541
        return $resultLead;
542
    }
543
544
545
    /**
546
     * @param $lead
547
     *
548
     * @return Lead
549
     * @throws LPTrackerSDKException
550
     */
551 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...
552
    {
553
        if ($lead instanceof Lead) {
554
            $lead = $lead->getId();
555
        } else {
556
            $lead = intval($lead);
557
        }
558
559
        if ($lead <= 0) {
560
            throw new LPTrackerSDKException('Invalid lead ID');
561
        }
562
563
        $url = '/lead/'.$lead;
564
565
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
566
567
        $resultLead = new Lead($response);
568
569
        return $resultLead;
570
    }
571
572
573
    /**
574
     * @param Lead $lead
575
     *
576
     * @return Lead
577
     * @throws LPTrackerSDKException
578
     */
579 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...
580
    {
581
        if ( ! $lead->validate()) {
582
            throw new LPTrackerSDKException('Invalid lead');
583
        }
584
585
        if ($lead->getId() > 0) {
586
            $url = '/lead/'.$lead->getId();
587
588
            $response = LPTrackerRequest::sendRequest($url, $lead->toArray(), 'PUT', $this->token, $this->address);
589
        } else {
590
            $response = LPTrackerRequest::sendRequest('/lead', $lead->toArray(), 'POST', $this->token, $this->address);
591
        }
592
593
        $resultLead = new Lead($response);
594
595
        return $resultLead;
596
    }
597
598
599
    /**
600
     * @param       $leadId
601
     * @param array $leadData
602
     *
603
     * @return Lead
604
     */
605
    public function editLead($leadId, array $leadData = [])
606
    {
607
        $leadData['id'] = $leadId;
608
609
        $lead = new Lead($leadData);
610
        $lead->validate();
611
612
        return $this->saveLead($lead);
613
    }
614
615
616
    /**
617
     * @param        $lead
618
     * @param string $category
619
     * @param string $purpose
620
     * @param float  $sum
621
     *
622
     * @return Lead
623
     * @throws LPTrackerSDKException
624
     */
625
    public function addLeadPayment($lead, $category, $purpose, $sum)
626
    {
627
        if ($lead instanceof Lead) {
628
            $lead = $lead->getId();
629
        } else {
630
            $lead = intval($lead);
631
        }
632
633
        if (empty($category)) {
634
            throw new LPTrackerSDKException('Category can not be empty');
635
        }
636
        if (empty($purpose)) {
637
            throw new LPTrackerSDKException('Purpose can not be empty');
638
        }
639
        $sum = floatval($sum);
640
641
        if ($sum <= 0) {
642
            throw new LPTrackerSDKException('Invalid sum');
643
        }
644
645
        if ($lead <= 0) {
646
            throw new LPTrackerSDKException('Invalid lead ID');
647
        }
648
649
        $url = '/lead/'.$lead.'/payment';
650
651
        $data = [
652
            'category' => $category,
653
            'purpose'  => $purpose,
654
            'sum'      => $sum,
655
        ];
656
657
        $response = LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address);
658
659
        $resultLead = new Lead($response);
660
661
        return $resultLead;
662
    }
663
664
665
    /**
666
     * @param $lead
667
     *
668
     * @return Comment[]
669
     */
670 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...
671
    {
672
        if ($lead instanceof Lead) {
673
            $lead = $lead->getId();
674
        } else {
675
            $lead = intval($lead);
676
        }
677
678
        $url = '/lead/'.$lead.'/comments';
679
680
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
681
682
        $result = [];
683
        foreach ($response as $commentData) {
684
            $result[] = new Comment($commentData);
685
        }
686
687
        return $result;
688
    }
689
690
691
    /**
692
     * @param $lead
693
     * @param $text
694
     *
695
     * @return Comment
696
     */
697 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...
698
    {
699
        if ($lead instanceof Lead) {
700
            $lead = $lead->getId();
701
        } else {
702
            $lead = intval($lead);
703
        }
704
705
        $url = '/lead/'.$lead.'/comment';
706
707
        $data = [
708
            'text' => $text
709
        ];
710
711
        $response = LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address);
712
713
        $comment = new Comment($response);
714
715
        return $comment;
716
    }
717
718
719
    /**
720
     * @param Custom $custom
721
     *
722
     * @return Custom
723
     * @throws LPTrackerSDKException
724
     */
725
    public function saveLeadCustom(Custom $custom)
726
    {
727
        if ( ! $custom->validate() || empty($custom->getLeadId())) {
728
            throw new LPTrackerSDKException('Invalid custom');
729
        }
730
731
        $url = '/lead/'.$custom->getLeadId().'/custom/'.$custom->getId();
732
733
        $data = [
734
            'value' => $custom->getValue()
735
        ];
736
737
        $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
738
739
        $resultCustom = new Custom($response);
740
741
        return $resultCustom;
742
    }
743
744
745
    /**
746
     * @param $lead
747
     * @param $custom
748
     * @param $newValue
749
     *
750
     * @return Custom
751
     */
752
    public function editLeadCustom($lead, $custom, $newValue)
753
    {
754
        if ($lead instanceof Lead) {
755
            $lead = $lead->getId();
756
        } else {
757
            $lead = intval($lead);
758
        }
759
760
        if ($custom instanceof Custom) {
761
            if (empty($newValue)) {
762
                $newValue = $custom->getValue();
763
            }
764
            $custom = $custom->getId();
765
        } else {
766
            $custom = intval($custom);
767
        }
768
769
        $customModel = new Custom([
770
            'id'      => $custom,
771
            'lead_id' => $lead,
772
            'value'   => $newValue
773
        ]);
774
        $customModel->validate();
775
776
        return $this->saveLeadCustom($customModel);
777
    }
778
}