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 ( b741fc...458622 )
by
unknown
02:05
created

LPTracker   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 387
Duplicated Lines 29.97 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 40
lcom 1
cbo 9
dl 116
loc 387
rs 8.2608
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
B login() 0 23 4
A logout() 0 4 1
A getProjectList() 11 11 2
A getProject() 10 10 1
A getProjectCustoms() 19 19 3
B createContact() 0 28 3
A saveContact() 19 19 3
A editContact() 0 17 2
B createLead() 0 24 5
A saveLead() 18 18 3
A editLead() 0 9 1
A getLeadComments() 19 19 3
A addCommentToLead() 20 20 2
A saveLeadCustom() 0 18 3
B editLeadCustom() 0 26 4

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like LPTracker often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use LPTracker, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace LPTracker;
4
5
use LPTracker\models\Comment;
6
use LPTracker\models\Contact;
7
use LPTracker\models\Custom;
8
use LPTracker\models\Lead;
9
use LPTracker\models\Project;
10
use LPTracker\exceptions\LPTrackerSDKException;
11
use LPTracker\authentication\AccessToken;
12
13
/**
14
 * Class LPTracker
15
 * @package LPTracker
16
 */
17
class LPTracker extends LPTrackerBase
18
{
19
20
    /**
21
     * @param        $login
22
     * @param        $password
23
     * @param string $serviceName
24
     *
25
     * @return AccessToken
26
     * @throws LPTrackerSDKException
27
     */
28
    public function login($login, $password, $serviceName = '')
29
    {
30
        if (empty($login)) {
31
            throw new LPTrackerSDKException('Login is empty');
32
        }
33
        if (empty($password)) {
34
            throw new LPTrackerSDKException('Password is empty');
35
        }
36
        if (empty($serviceName)) {
37
            $serviceName = LPTrackerBase::DEFAULT_SERVICE_NAME;
38
        }
39
40
        $response = LPTrackerRequest::sendRequest('/login', [
41
            'login'    => $login,
42
            'password' => $password,
43
            'service'  => $serviceName,
44
            'version'  => LPTrackerBase::VERSION
45
        ], 'POST', null, $this->address);
46
47
        $accessToken = new AccessToken($response['token']);
48
49
        return $accessToken;
50
    }
51
52
53
    /**
54
     *
55
     */
56
    public function logout()
57
    {
58
        LPTrackerRequest::sendRequest('/logout', [], 'POST', $this->token, $this->address);
59
    }
60
61
62
    /**
63
     * @return Project[]
64
     */
65 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...
66
    {
67
        $response = LPTrackerRequest::sendRequest('/projects', [], 'GET', $this->token, $this->address);
68
69
        $result = [];
70
        foreach ($response as $projectData) {
71
            $result[] = new Project($projectData);
72
        }
73
74
        return $result;
75
    }
76
77
78
    /**
79
     * @param $id
80
     *
81
     * @return Project
82
     */
83 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...
84
    {
85
        $url = '/project/'.$id;
86
87
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
88
89
        $project = new Project($response);
90
91
        return $project;
92
    }
93
94
95
    /**
96
     * @param $project
97
     *
98
     * @return Custom[]
99
     */
100 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...
101
    {
102
        if ($project instanceof Project) {
103
            $project = $project->getId();
104
        } else {
105
            $project = intval($project);
106
        }
107
108
        $url = '/project/'.$project.'/customs';
109
110
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
111
112
        $result = [];
113
        foreach ($response as $customData) {
114
            $result[] = new Custom($customData);
115
        }
116
117
        return $result;
118
    }
119
120
121
    /**
122
     * @param       $project
123
     * @param array $details
124
     * @param array $contactData
125
     *
126
     * @return Contact
127
     * @throws LPTrackerSDKException
128
     */
129
    public function createContact(
130
        $project,
131
        array $details,
132
        array $contactData = []
133
    ) {
134
        if (empty($details)) {
135
            throw new LPTrackerSDKException('Contact details can not be empty');
136
        }
137
138
        if ($project instanceof Project) {
139
            $project = $project->getId();
140
        } else {
141
            $project = intval($project);
142
        }
143
144
        $contactData['project_id'] = $project;
145
        $contactData['details'] = $details;
146
147
        $contact = new Contact($contactData);
148
        $contact->validate();
149
150
        $response = LPTrackerRequest::sendRequest('/contact', $contact->toArray(), 'POST', $this->token,
151
            $this->address);
152
153
        $resultContact = new Contact($response);
154
155
        return $resultContact;
156
    }
157
158
159
    /**
160
     * @param Contact $contact
161
     *
162
     * @return Contact
163
     * @throws LPTrackerSDKException
164
     */
165 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...
166
    {
167
        if ( ! $contact->validate()) {
168
            throw new LPTrackerSDKException('Invalid contact');
169
        }
170
171
        if ($contact->getId() > 0) {
172
            $url = '/contact/'.$contact->getId();
173
174
            $response = LPTrackerRequest::sendRequest($url, $contact->toArray(), 'PUT', $this->token, $this->address);
175
        } else {
176
            $response = LPTrackerRequest::sendRequest('/contact', $contact->toArray(), 'POST', $this->token,
177
                $this->address);
178
        }
179
180
        $resultContact = new Contact($response);
181
182
        return $resultContact;
183
    }
184
185
186
    /**
187
     * @param       $contactId
188
     * @param array $details
189
     * @param array $contactData
190
     *
191
     * @return Contact
192
     * @throws LPTrackerSDKException
193
     */
194
    public function editContact(
195
        $contactId,
196
        array $details,
197
        array $contactData = []
198
    ) {
199
        if (empty($details)) {
200
            throw new LPTrackerSDKException('Contact details can not be empty');
201
        }
202
203
        $contactData['id'] = $contactId;
204
        $contactData['details'] = $details;
205
206
        $contact = new Contact($contactData);
207
        $contact->validate();
208
209
        return $this->saveContact($contact);
210
    }
211
212
213
    /**
214
     * @param       $contact
215
     * @param array $leadData
216
     * @param array $options
217
     *
218
     * @return Lead
219
     * @throws LPTrackerSDKException
220
     */
221
    public function createLead($contact, array $leadData = [], array $options = [])
222
    {
223
        if ($contact instanceof Contact) {
224
            $leadData['contact_id'] = $contact->getId();
225
        } else {
226
            $leadData['contact_id'] = intval($contact);
227
        }
228
229
        $lead = new Lead($leadData);
230
        if ( ! $lead->validate()) {
231
            throw new LPTrackerSDKException('Invalid lead data');
232
        }
233
234
        $data = $lead->toArray();
235
        if (isset($options['callback'])) {
236
            $data['callback'] = $options['callback'] ? true : false;
237
        }
238
239
        $response = LPTrackerRequest::sendRequest('/lead', $data, 'POST', $this->token, $this->address);
240
241
        $resultLead = new Lead($response);
242
243
        return $resultLead;
244
    }
245
246
247
    /**
248
     * @param Lead $lead
249
     *
250
     * @return Lead
251
     * @throws LPTrackerSDKException
252
     */
253 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...
254
    {
255
        if ( ! $lead->validate()) {
256
            throw new LPTrackerSDKException('Invalid lead');
257
        }
258
259
        if ($lead->getId() > 0) {
260
            $url = '/lead/'.$lead->getId();
261
262
            $response = LPTrackerRequest::sendRequest($url, $lead->toArray(), 'PUT', $this->token, $this->address);
263
        } else {
264
            $response = LPTrackerRequest::sendRequest('/lead', $lead->toArray(), 'POST', $this->token, $this->address);
265
        }
266
267
        $resultLead = new Lead($response);
268
269
        return $resultLead;
270
    }
271
272
273
    /**
274
     * @param       $leadId
275
     * @param array $leadData
276
     *
277
     * @return Lead
278
     */
279
    public function editLead($leadId, array $leadData = [])
280
    {
281
        $leadData['id'] = $leadId;
282
283
        $lead = new Lead($leadData);
284
        $lead->validate();
285
286
        return $this->saveLead($lead);
287
    }
288
289
290
    /**
291
     * @param $lead
292
     *
293
     * @return Comment[]
294
     */
295 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...
296
    {
297
        if ($lead instanceof Lead) {
298
            $lead = $lead->getId();
299
        } else {
300
            $lead = intval($lead);
301
        }
302
303
        $url = '/lead/'.$lead.'/comments';
304
305
        $response = LPTrackerRequest::sendRequest($url, [], 'GET', $this->token, $this->address);
306
307
        $result = [];
308
        foreach ($response as $commentData) {
309
            $result[] = new Comment($commentData);
310
        }
311
312
        return $result;
313
    }
314
315
316
    /**
317
     * @param $lead
318
     * @param $text
319
     *
320
     * @return Comment
321
     */
322 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...
323
    {
324
        if ($lead instanceof Lead) {
325
            $lead = $lead->getId();
326
        } else {
327
            $lead = intval($lead);
328
        }
329
330
        $url = '/lead/'.$lead.'/comment';
331
332
        $data = [
333
            'text' => $text
334
        ];
335
336
        $response = LPTrackerRequest::sendRequest($url, $data, 'POST', $this->token, $this->address);
337
338
        $comment = new Comment($response);
339
340
        return $comment;
341
    }
342
343
344
    /**
345
     * @param Custom $custom
346
     *
347
     * @return Custom
348
     * @throws LPTrackerSDKException
349
     */
350
    public function saveLeadCustom(Custom $custom)
351
    {
352
        if ( ! $custom->validate() || empty($custom->getLeadId())) {
353
            throw new LPTrackerSDKException('Invalid custom');
354
        }
355
356
        $url = '/lead/'.$custom->getLeadId().'/custom/'.$custom->getId();
357
358
        $data = [
359
            'value' => $custom->getValue()
360
        ];
361
362
        $response = LPTrackerRequest::sendRequest($url, $data, 'PUT', $this->token, $this->address);
363
364
        $resultCustom = new Custom($response);
365
366
        return $resultCustom;
367
    }
368
369
370
    /**
371
     * @param $lead
372
     * @param $custom
373
     * @param $newValue
374
     *
375
     * @return Custom
376
     */
377
    public function editLeadCustom($lead, $custom, $newValue)
378
    {
379
        if ($lead instanceof Lead) {
380
            $lead = $lead->getId();
381
        } else {
382
            $lead = intval($lead);
383
        }
384
385
        if ($custom instanceof Custom) {
386
            if (empty($newValue)) {
387
                $newValue = $custom->getValue();
388
            }
389
            $custom = $custom->getId();
390
        } else {
391
            $custom = intval($custom);
392
        }
393
394
        $customModel = new Custom([
395
            'id'      => $custom,
396
            'lead_id' => $lead,
397
            'value'   => $newValue
398
        ]);
399
        $customModel->validate();
400
401
        return $this->saveLeadCustom($customModel);
402
    }
403
}