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 ( f9d0ca...755fa4 )
by
unknown
02:25
created

LPTracker::logout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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 $contact
264
     * @param $field
265
     * @param $newValue
266
     *
267
     * @return ContactField
268
     */
269
    public function updateContactField($contact, $field, $newValue)
270
    {
271
        if ($contact instanceof Contact) {
272
            $contact = $contact->getId();
273
        }
274
        if ($field instanceof ContactField) {
275
            $field = $field->getId();
276
        }
277
278
        $url = '/contact/'.$contact.'/field/'.$field;
279
280
        $data = [
281
            'value' => $newValue
282
        ];
283
284
        $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
285
286
        $contactField = new ContactField($response);
287
288
        return $contactField;
289
    }
290
291
292
    /**
293
     * @param       $contact
294
     * @param array $leadData
295
     * @param array $options
296
     *
297
     * @return Lead
298
     * @throws LPTrackerSDKException
299
     */
300
    public function createLead($contact, array $leadData = [], array $options = [])
301
    {
302
        if ($contact instanceof Contact) {
303
            $leadData['contact_id'] = $contact->getId();
304
        } else {
305
            $leadData['contact_id'] = intval($contact);
306
        }
307
308
        $lead = new Lead($leadData);
309
        if ( ! $lead->validate()) {
310
            throw new LPTrackerSDKException('Invalid lead data');
311
        }
312
313
        $data = $lead->toArray();
314
        if (isset($options['callback'])) {
315
            $data['callback'] = $options['callback'] ? true : false;
316
        }
317
318
        $response = LPTrackerRequest::sendRequest('/lead', $data, 'POST', $this->token, $this->address);
319
320
        $resultLead = new Lead($response);
321
322
        return $resultLead;
323
    }
324
325
326
    /**
327
     * @param Lead $lead
328
     *
329
     * @return Lead
330
     * @throws LPTrackerSDKException
331
     */
332 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...
333
    {
334
        if ( ! $lead->validate()) {
335
            throw new LPTrackerSDKException('Invalid lead');
336
        }
337
338
        if ($lead->getId() > 0) {
339
            $url = '/lead/'.$lead->getId();
340
341
            $response = LPTrackerRequest::sendRequest($url, $lead->toArray(), 'PUT', $this->token, $this->address);
342
        } else {
343
            $response = LPTrackerRequest::sendRequest('/lead', $lead->toArray(), 'POST', $this->token, $this->address);
344
        }
345
346
        $resultLead = new Lead($response);
347
348
        return $resultLead;
349
    }
350
351
352
    /**
353
     * @param       $leadId
354
     * @param array $leadData
355
     *
356
     * @return Lead
357
     */
358
    public function editLead($leadId, array $leadData = [])
359
    {
360
        $leadData['id'] = $leadId;
361
362
        $lead = new Lead($leadData);
363
        $lead->validate();
364
365
        return $this->saveLead($lead);
366
    }
367
368
369
    /**
370
     * @param $lead
371
     *
372
     * @return Comment[]
373
     */
374 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...
375
    {
376
        if ($lead instanceof Lead) {
377
            $lead = $lead->getId();
378
        } else {
379
            $lead = intval($lead);
380
        }
381
382
        $url = '/lead/'.$lead.'/comments';
383
384
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
385
386
        $result = [];
387
        foreach ($response as $commentData) {
388
            $result[] = new Comment($commentData);
389
        }
390
391
        return $result;
392
    }
393
394
395
    /**
396
     * @param $lead
397
     * @param $text
398
     *
399
     * @return Comment
400
     */
401 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...
402
    {
403
        if ($lead instanceof Lead) {
404
            $lead = $lead->getId();
405
        } else {
406
            $lead = intval($lead);
407
        }
408
409
        $url = '/lead/'.$lead.'/comment';
410
411
        $data = [
412
            'text' => $text
413
        ];
414
415
        $response = LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address);
416
417
        $comment = new Comment($response);
418
419
        return $comment;
420
    }
421
422
423
    /**
424
     * @param Custom $custom
425
     *
426
     * @return Custom
427
     * @throws LPTrackerSDKException
428
     */
429
    public function saveLeadCustom(Custom $custom)
430
    {
431
        if ( ! $custom->validate() || empty($custom->getLeadId())) {
432
            throw new LPTrackerSDKException('Invalid custom');
433
        }
434
435
        $url = '/lead/'.$custom->getLeadId().'/custom/'.$custom->getId();
436
437
        $data = [
438
            'value' => $custom->getValue()
439
        ];
440
441
        $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
442
443
        $resultCustom = new Custom($response);
444
445
        return $resultCustom;
446
    }
447
448
449
    /**
450
     * @param $lead
451
     * @param $custom
452
     * @param $newValue
453
     *
454
     * @return Custom
455
     */
456
    public function editLeadCustom($lead, $custom, $newValue)
457
    {
458
        if ($lead instanceof Lead) {
459
            $lead = $lead->getId();
460
        } else {
461
            $lead = intval($lead);
462
        }
463
464
        if ($custom instanceof Custom) {
465
            if (empty($newValue)) {
466
                $newValue = $custom->getValue();
467
            }
468
            $custom = $custom->getId();
469
        } else {
470
            $custom = intval($custom);
471
        }
472
473
        $customModel = new Custom([
474
            'id'      => $custom,
475
            'lead_id' => $lead,
476
            'value'   => $newValue
477
        ]);
478
        $customModel->validate();
479
480
        return $this->saveLeadCustom($customModel);
481
    }
482
}