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 ( e61095...35342b )
by
unknown
01:40
created

LPTracker::getProjectFields()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 4
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
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
     *
514
     * @return Comment[]
515
     */
516 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...
517
    {
518
        if ($lead instanceof Lead) {
519
            $lead = $lead->getId();
520
        } else {
521
            $lead = intval($lead);
522
        }
523
524
        $url = '/lead/'.$lead.'/comments';
525
526
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
527
528
        $result = [];
529
        foreach ($response as $commentData) {
530
            $result[] = new Comment($commentData);
531
        }
532
533
        return $result;
534
    }
535
536
537
    /**
538
     * @param $lead
539
     * @param $text
540
     *
541
     * @return Comment
542
     */
543 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...
544
    {
545
        if ($lead instanceof Lead) {
546
            $lead = $lead->getId();
547
        } else {
548
            $lead = intval($lead);
549
        }
550
551
        $url = '/lead/'.$lead.'/comment';
552
553
        $data = [
554
            'text' => $text
555
        ];
556
557
        $response = LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address);
558
559
        $comment = new Comment($response);
560
561
        return $comment;
562
    }
563
564
565
    /**
566
     * @param Custom $custom
567
     *
568
     * @return Custom
569
     * @throws LPTrackerSDKException
570
     */
571
    public function saveLeadCustom(Custom $custom)
572
    {
573
        if ( ! $custom->validate() || empty($custom->getLeadId())) {
574
            throw new LPTrackerSDKException('Invalid custom');
575
        }
576
577
        $url = '/lead/'.$custom->getLeadId().'/custom/'.$custom->getId();
578
579
        $data = [
580
            'value' => $custom->getValue()
581
        ];
582
583
        $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
584
585
        $resultCustom = new Custom($response);
586
587
        return $resultCustom;
588
    }
589
590
591
    /**
592
     * @param $lead
593
     * @param $custom
594
     * @param $newValue
595
     *
596
     * @return Custom
597
     */
598
    public function editLeadCustom($lead, $custom, $newValue)
599
    {
600
        if ($lead instanceof Lead) {
601
            $lead = $lead->getId();
602
        } else {
603
            $lead = intval($lead);
604
        }
605
606
        if ($custom instanceof Custom) {
607
            if (empty($newValue)) {
608
                $newValue = $custom->getValue();
609
            }
610
            $custom = $custom->getId();
611
        } else {
612
            $custom = intval($custom);
613
        }
614
615
        $customModel = new Custom([
616
            'id'      => $custom,
617
            'lead_id' => $lead,
618
            'value'   => $newValue
619
        ]);
620
        $customModel->validate();
621
622
        return $this->saveLeadCustom($customModel);
623
    }
624
}