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 ( 755fa4...9b9269 )
by
unknown
02:29
created

LPTracker::getProjectList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
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 View Code Duplication
        foreach ($fields as $fieldId => $fieldValue) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
177
            if ($fieldValue instanceof ContactField) {
178
                $fieldId = $fieldValue->getId();
179
                $fieldValue = $fieldValue->getValue();
180
            }
181
182
            $contactData['fields'][$fieldId] = $fieldValue;
183
        }
184
185
        $contact = new Contact($contactData);
186
        $contact->validate();
187
188
        $response = LPTrackerRequest::sendRequest('/contact', $contact->toArray(), 'POST', $this->token,
189
            $this->address);
190
191
        $resultContact = new Contact($response);
192
193
        return $resultContact;
194
    }
195
196
197
    /**
198
     * @param Contact $contact
199
     *
200
     * @return Contact
201
     * @throws LPTrackerSDKException
202
     */
203 View Code Duplication
    public function saveContact(Contact $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...
204
    {
205
        if ( ! $contact->validate()) {
206
            throw new LPTrackerSDKException('Invalid contact');
207
        }
208
209
        if ($contact->getId() > 0) {
210
            $url = '/contact/'.$contact->getId();
211
212
            $response = LPTrackerRequest::sendRequest($url, $contact->toArray(), 'PUT', $this->token, $this->address);
213
        } else {
214
            $response = LPTrackerRequest::sendRequest('/contact', $contact->toArray(), 'POST', $this->token,
215
                $this->address);
216
        }
217
218
        $resultContact = new Contact($response);
219
220
        return $resultContact;
221
    }
222
223
224
    /**
225
     * @param       $contactId
226
     * @param array $details
227
     * @param array $contactData
228
     * @param array $fields
229
     *
230
     * @return Contact
231
     * @throws LPTrackerSDKException
232
     */
233
    public function editContact(
234
        $contactId,
235
        array $details,
236
        array $contactData = [],
237
        array $fields = []
238
    ) {
239
        if (empty($details)) {
240
            throw new LPTrackerSDKException('Contact details can not be empty');
241
        }
242
243
        $contactData['id'] = $contactId;
244
        $contactData['details'] = $details;
245
246 View Code Duplication
        foreach ($fields as $fieldId => $fieldValue) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
247
            if ($fieldValue instanceof ContactField) {
248
                $fieldId = $fieldValue->getId();
249
                $fieldValue = $fieldValue->getValue();
250
            }
251
252
            $contactData['fields'][$fieldId] = $fieldValue;
253
        }
254
255
        $contact = new Contact($contactData);
256
        $contact->validate();
257
258
        return $this->saveContact($contact);
259
    }
260
261
262
    /**
263
     * @param       $project
264
     * @param array $searchOptions
265
     *
266
     * @return array
267
     * @throws LPTrackerSDKException
268
     */
269
    public function searchContacts($project, array $searchOptions = [])
270
    {
271
        if ($project instanceof Project) {
272
            $project = $project->getId();
273
        }
274
        $project = intval($project);
275
        if ($project <= 0) {
276
            throw new LPTrackerSDKException('Invalid project id');
277
        }
278
279
        $url = '/contact/search?';
280
281
        $data = [
282
            'project_id' => $project
283
        ];
284
        if (isset($searchOptions['email'])) {
285
            $data['email'] = $searchOptions['email'];
286
        }
287
        if (isset($searchOptions['phone'])) {
288
            $data['phone'] = $searchOptions['phone'];
289
        }
290
291
        $url = $url.http_build_query($data);
292
293
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
294
295
        $result = [];
296
        foreach ($response as $contactData) {
297
            $result[] = new Contact($contactData);
298
        }
299
300
        return $result;
301
    }
302
303
304
    /**
305
     * @param $contact
306
     *
307
     * @return array
308
     * @throws LPTrackerSDKException
309
     */
310
    public function contactLeads($contact)
311
    {
312
        if ($contact instanceof Contact) {
313
            $contact = $contact->getId();
314
        }
315
        $contact = intval($contact);
316
        if ($contact <= 0) {
317
            throw new LPTrackerSDKException('Invalid contact id');
318
        }
319
320
        $url = '/contact/'.$contact.'/leads';
321
322
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
323
324
        $result = [];
325
        foreach ($response as $leadData) {
326
            $result[] = new Lead($leadData);
327
        }
328
329
        return $result;
330
    }
331
332
333
    /**
334
     * @param $contact
335
     * @param $field
336
     * @param $newValue
337
     *
338
     * @return ContactField
339
     */
340
    public function updateContactField($contact, $field, $newValue)
341
    {
342
        if ($contact instanceof Contact) {
343
            $contact = $contact->getId();
344
        }
345
        if ($field instanceof ContactField) {
346
            $field = $field->getId();
347
        }
348
349
        $url = '/contact/'.$contact.'/field/'.$field;
350
351
        $data = [
352
            'value' => $newValue
353
        ];
354
355
        $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
356
357
        $contactField = new ContactField($response);
358
359
        return $contactField;
360
    }
361
362
363
    /**
364
     * @param       $contact
365
     * @param array $leadData
366
     * @param array $options
367
     *
368
     * @return Lead
369
     * @throws LPTrackerSDKException
370
     */
371
    public function createLead($contact, array $leadData = [], array $options = [])
372
    {
373
        if ($contact instanceof Contact) {
374
            $leadData['contact_id'] = $contact->getId();
375
        } else {
376
            $leadData['contact_id'] = intval($contact);
377
        }
378
379
        $lead = new Lead($leadData);
380
        if ( ! $lead->validate()) {
381
            throw new LPTrackerSDKException('Invalid lead data');
382
        }
383
384
        $data = $lead->toArray();
385
        if (isset($options['callback'])) {
386
            $data['callback'] = $options['callback'] ? true : false;
387
        }
388
389
        $response = LPTrackerRequest::sendRequest('/lead', $data, 'POST', $this->token, $this->address);
390
391
        $resultLead = new Lead($response);
392
393
        return $resultLead;
394
    }
395
396
397
    /**
398
     * @param Lead $lead
399
     *
400
     * @return Lead
401
     * @throws LPTrackerSDKException
402
     */
403 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...
404
    {
405
        if ( ! $lead->validate()) {
406
            throw new LPTrackerSDKException('Invalid lead');
407
        }
408
409
        if ($lead->getId() > 0) {
410
            $url = '/lead/'.$lead->getId();
411
412
            $response = LPTrackerRequest::sendRequest($url, $lead->toArray(), 'PUT', $this->token, $this->address);
413
        } else {
414
            $response = LPTrackerRequest::sendRequest('/lead', $lead->toArray(), 'POST', $this->token, $this->address);
415
        }
416
417
        $resultLead = new Lead($response);
418
419
        return $resultLead;
420
    }
421
422
423
    /**
424
     * @param       $leadId
425
     * @param array $leadData
426
     *
427
     * @return Lead
428
     */
429
    public function editLead($leadId, array $leadData = [])
430
    {
431
        $leadData['id'] = $leadId;
432
433
        $lead = new Lead($leadData);
434
        $lead->validate();
435
436
        return $this->saveLead($lead);
437
    }
438
439
440
    /**
441
     * @param $lead
442
     *
443
     * @return Comment[]
444
     */
445 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...
446
    {
447
        if ($lead instanceof Lead) {
448
            $lead = $lead->getId();
449
        } else {
450
            $lead = intval($lead);
451
        }
452
453
        $url = '/lead/'.$lead.'/comments';
454
455
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
456
457
        $result = [];
458
        foreach ($response as $commentData) {
459
            $result[] = new Comment($commentData);
460
        }
461
462
        return $result;
463
    }
464
465
466
    /**
467
     * @param $lead
468
     * @param $text
469
     *
470
     * @return Comment
471
     */
472 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...
473
    {
474
        if ($lead instanceof Lead) {
475
            $lead = $lead->getId();
476
        } else {
477
            $lead = intval($lead);
478
        }
479
480
        $url = '/lead/'.$lead.'/comment';
481
482
        $data = [
483
            'text' => $text
484
        ];
485
486
        $response = LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address);
487
488
        $comment = new Comment($response);
489
490
        return $comment;
491
    }
492
493
494
    /**
495
     * @param Custom $custom
496
     *
497
     * @return Custom
498
     * @throws LPTrackerSDKException
499
     */
500
    public function saveLeadCustom(Custom $custom)
501
    {
502
        if ( ! $custom->validate() || empty($custom->getLeadId())) {
503
            throw new LPTrackerSDKException('Invalid custom');
504
        }
505
506
        $url = '/lead/'.$custom->getLeadId().'/custom/'.$custom->getId();
507
508
        $data = [
509
            'value' => $custom->getValue()
510
        ];
511
512
        $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
513
514
        $resultCustom = new Custom($response);
515
516
        return $resultCustom;
517
    }
518
519
520
    /**
521
     * @param $lead
522
     * @param $custom
523
     * @param $newValue
524
     *
525
     * @return Custom
526
     */
527
    public function editLeadCustom($lead, $custom, $newValue)
528
    {
529
        if ($lead instanceof Lead) {
530
            $lead = $lead->getId();
531
        } else {
532
            $lead = intval($lead);
533
        }
534
535
        if ($custom instanceof Custom) {
536
            if (empty($newValue)) {
537
                $newValue = $custom->getValue();
538
            }
539
            $custom = $custom->getId();
540
        } else {
541
            $custom = intval($custom);
542
        }
543
544
        $customModel = new Custom([
545
            'id'      => $custom,
546
            'lead_id' => $lead,
547
            'value'   => $newValue
548
        ]);
549
        $customModel->validate();
550
551
        return $this->saveLeadCustom($customModel);
552
    }
553
}