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 ( 9b9269...5d06d3 )
by
unknown
02:28
created

LPTracker::saveLeadCustom()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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