Github::tplOrganization()   F
last analyzed

Complexity

Conditions 19
Paths > 20000

Size

Total Lines 25
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
rs 2.6972
cc 19
eloc 22
nc 131072
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace GitScrum\Classes;
4
5
use Auth;
6
use GitScrum\Models\Branch;
7
use GitScrum\Models\Commit;
8
use GitScrum\Models\User;
9
use GitScrum\Models\Issue;
10
use GitScrum\Models\Organization;
11
use GitScrum\Models\ProductBacklog;
12
use Carbon\Carbon;
13
use GitScrum\Contracts\ProviderInterface;
14
15
class Github implements ProviderInterface
16
{
17
    public function tplUser($obj)
18
    {
19
        return [
20
            'provider_id' => $obj->id,
21
            'provider' => 'github',
22
            'username' => isset($obj->login) ? $obj->login : $obj->nickname,
23
            'name' => isset($obj->name) ? $obj->name : null,
24
            'token' => isset($obj->token) ? $obj->token : null,
25
            'avatar' => isset($obj->user['avatar_url']) ? $obj->user['avatar_url'] : $obj->avatar_url,
26
            'html_url' => isset($obj->user['html_url']) ? $obj->user['html_url'] : $obj->html_url,
27
            'bio' => isset($obj->user['bio']) ? $obj->user['bio'] : null,
28
            'since' => isset($obj->user['created_at']) ? Carbon::parse($obj->user['created_at'])->toDateTimeString() : Carbon::now(),
29
            'location' => isset($obj->user['location']) ? $obj->user['location'] : null,
30
            'blog' => isset($obj->user['blog']) ? $obj->user['blog'] : null,
31
            'email' => isset($obj->email) ? $obj->email : null,
32
        ];
33
    }
34
35
    public function tplRepository($repo, $slug = false)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
36
    {
37
        return (object) [
38
            'provider_id' => $repo->id,
39
            'organization_id' => $this->organization($repo->owner->login),
40
            'organization_title' => $repo->owner->login,
41
            'slug' => $slug ? $slug : Helper::slug($repo->name),
42
            'title' => $repo->name,
43
            'fullname' => $repo->full_name,
44
            'is_private' => $repo->private,
45
            'html_url' => $repo->html_url,
46
            'description' => $repo->description,
47
            'fork' => $repo->fork,
48
            'url' => $repo->url,
49
            'since' => Carbon::parse($repo->created_at)->toDateTimeString(),
50
            'pushed_at' => Carbon::parse($repo->pushed_at)->toDateTimeString(),
51
            'ssh_url' => $repo->ssh_url,
52
            'clone_url' => $repo->clone_url,
53
            'homepage' => $repo->homepage,
54
            'default_branch' => $repo->default_branch,
55
        ];
56
    }
57
58
    public function tplIssue($obj, $productBracklogId)
59
    {
60
        if (isset($obj->user->login)) {
61
            $user = User::where('username', $obj->user->login)
62
                ->where('provider', 'github')->first();
63
        }
64
65
        return [
66
            'provider_id' => $obj->id,
67
            'user_id' => isset($user->id) ? $user->id : Auth::user()->id,
0 ignored issues
show
Bug introduced by
The variable $user does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
68
            'product_backlog_id' => $productBracklogId,
69
            'effort' => 0,
70
            'config_issue_effort_id' => 1,
71
            'issue_type_id' => 1,
72
            'number' => $obj->number,
73
            'title' => $obj->title,
74
            'description' => $obj->body,
75
            'state' => $obj->state,
76
            'html_url' => $obj->html_url,
77
            'created_at' => $obj->created_at,
78
            'updated_at' => $obj->updated_at,
79
        ];
80
    }
81
82
    public function tplOrganization($obj)
83
    {
84
        return [
85
            'provider_id' => $obj->id,
86
            'username' => $obj->login,
87
            'url' => isset($obj->url) ? $obj->url : null,
88
            'repos_url' => isset($obj->repos_url) ? $obj->repos_url : null,
89
            'events_url' => isset($obj->events_url) ? $obj->events_url : null,
90
            'hooks_url' => isset($obj->hooks_url) ? $obj->hooks_url : null,
91
            'issues_url' => isset($obj->issues_url) ? $obj->issues_url : null,
92
            'members_url' => isset($obj->members_url) ? $obj->members_url : null,
93
            'public_members_url' => isset($obj->public_members_url) ? $obj->public_members_url : null,
94
            'avatar_url' => isset($obj->avatar_url) ? $obj->avatar_url : null,
95
            'description' => isset($obj->description) ? $obj->description : null,
96
            'title' => isset($obj->name) ? $obj->name : null,
97
            'blog' => isset($obj->blog) ? $obj->blog : null,
98
            'location' => isset($obj->location) ? $obj->location : null,
99
            'email' => isset($obj->email) ? $obj->email : null,
100
            'public_repos' => isset($obj->public_repos) ? $obj->public_repos : null,
101
            'html_url' => isset($obj->html_url) ? $obj->html_url : null,
102
            'total_private_repos' => isset($obj->total_private_repos) ? $obj->total_private_repos : null,
103
            'since' => Carbon::parse((isset($obj->created_at) ? $obj->created_at : Carbon::now()))->toDateTimeString(),
104
            'disk_usage' => isset($obj->disk_usage) ? $obj->disk_usage : null,
105
        ];
106
    }
107
108
    public function readRepositories($page = 1, &$repos = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
109
    {
110
        $response = collect(Helper::request('https://api.github.com/user/repos?page='. $page))->map(function ($repo) {
111
            return $this->tplRepository($repo);
112
        });
113
114
        if (is_null($repos)) {
115
            $repos = collect();
116
        }
117
118
        $repos->push($response);
119
120
        if ($response->count() == 30) {
121
            $this->readRepositories(++$page, $repos);
122
        }
123
124
        return $repos->flatten(1)->sortBy('title');
125
    }
126
127
    public function createOrUpdateRepository($owner, $obj, $oldTitle = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
128
    {
129
        $params = [
130
            'name' => str_slug($obj->title, '-'),
131
            'description' => $obj->description,
132
        ];
133
134
        if (is_null($oldTitle)) {
135
            $endpoint = 'https://api.github.com/orgs/'.$owner.'/repos';
136
137
            if (Auth::user()->username == $owner) {
138
                $endpoint = 'https://api.github.com/user/repos';
139
            }
140
141
            $response = Helper::request($endpoint, true, 'POST', $params);
142
        } else {
143
            $oldTitle = str_slug($oldTitle, '-');
144
            $response = Helper::request('https://api.github.com/repos/'.$owner.DIRECTORY_SEPARATOR.$oldTitle, true, 'POST', $params);
145
        }
146
147
        return (object) $response;
148
    }
149
150
    public function organization($login)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
151
    {
152
        $organization = Organization::where('username', $login)
153
            ->where('provider', 'github')->first();
154
155
        if (!isset($organization)) {
156
            $response = Helper::request('https://api.github.com/orgs/'.$login);
157
158
            if (!isset($response->id)) {
159
                $response = Helper::request('https://api.github.com/users/'.$login);
160
            }
161
162
            if (isset($response->id)) {
163
                $organization = Organization::create($this->tplOrganization($response));
164
            }
165
        }
166
167
        if (is_null($organization->users()->where('users_has_organizations.user_id', Auth::id())
168
            ->where('users_has_organizations.organization_id', $organization->id)->first())) {
169
            $organization->users()->attach(Auth::id());
170
        }
171
172
        return $organization->id;
173
    }
174
175
    public function readCollaborators($owner, $repo, $providerId = null)
176
    {
177
        $ids = collect();
178
179
        collect(Helper::request('https://api.github.com/repos/'.$owner.'/'.$repo.'/collaborators'))
180
            ->map(function ($collaborator) use ($ids) {
181
                $user = User::where('username', $collaborator->login)
182
                ->where('provider', 'github')->first();
183
184
                if (!isset($user)) {
185
                    $user = User::create($this->tplUser($collaborator));
186
                }
187
188
                $ids->push($user->id);
189
            });
190
191
        $organization = Organization::where('username', $owner)
192
            ->where('provider', 'github')->first()->users();
193
194
        $organization->syncWithoutDetaching($ids->diff($organization->pluck('user_id')->toArray()));
195
    }
196
197
    public function createBranches($owner, $productBacklogId, $repo, $providerId = null, $page = 1)
198
    {
199
        $branches = collect(Helper::request('https://api.github.com/repos/'.$owner.DIRECTORY_SEPARATOR.$repo.'/branches?page='.$page));
200
201
        $branches->map(function ($branch) use ($productBacklogId) {
202
            $data = [
203
                'product_backlog_id' => $productBacklogId,
204
                'title' => $branch->name,
205
                'sha' => $branch->commit->sha,
206
            ];
207
            Branch::create($data);
208
        });
209
210
        if ($branches->count()==30) {
211
            $this->createBranches($owner, $productBacklogId, $repo, $providerId, ++$page);
212
        }
213
    }
214
215
    public function readIssues($productBacklogId = null)
216
    {
217
        if (is_null($productBacklogId)) {
218
            $productBacklog = ProductBacklog::all();
219
        } else {
220
            $productBacklog = ProductBacklog::find($productBacklogId);
221
        }
222
223
        $repos = $productBacklog->map(function ($repo) {
0 ignored issues
show
Unused Code introduced by
$repos is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
224
            $issues = collect(Helper::request('https://api.github.com/repos/'.$repo->organization->username.
0 ignored issues
show
Unused Code introduced by
$issues is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
225
                DIRECTORY_SEPARATOR.$repo->title.'/issues?state=all'))->map(function ($issue) use ($repo) {
226
                    if (isset($issue->id)) {
227
                        $data = $this->tplIssue($issue, $repo->id);
228
229
                        if (!Issue::where('provider_id', $issue->id)->where('provider', 'github')->first()) {
230
                            Issue::create($data)->users()->attach($data['user_id']);
231
                        }
232
                    }
233
                });
234
        });
235
    }
236
237
    public function createOrUpdateIssue($obj)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
238
    {
239
        $params = [
240
            'title' => $obj->title,
241
            'body' => $obj->description,
242
        ];
243
244
        $response = Helper::request('https://api.github.com/repos/'.
245
            $obj->productBacklog->organization->username.DIRECTORY_SEPARATOR.
246
            $obj->productBacklog->title.'/issues'.(isset($obj->number) ? DIRECTORY_SEPARATOR.$obj->number : ''),
247
            true, 'POST', $params);
248
249
        return (object) $response;
250
    }
251
252
    public function createOrUpdateIssueComment($obj, $verb = 'POST')
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
253
    {
254
        $params = [
255
            'body' => $obj->comment,
256
        ];
257
258
        $response = Helper::request('https://api.github.com/repos/'.
259
            $obj->issue->productBacklog->organization->username.DIRECTORY_SEPARATOR.
260
            $obj->issue->productBacklog->title.'/issues'.(isset($obj->provider_id) ? '' : DIRECTORY_SEPARATOR.$obj->issue->number).'/comments'.
261
            (isset($obj->provider_id) ? DIRECTORY_SEPARATOR.$obj->provider_id : ''),
262
            true, $verb, $params);
263
264
        return (object) $response;
265
    }
266
267
    public function deleteIssueComment($obj)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
268
    {
269
        return $this->createOrUpdateIssueComment($obj, 'DELETE');
270
    }
271
}
272