Completed
Push — master ( b8b92f...cb32d6 )
by Renato
03:09
created

Github::createBranches()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 2
eloc 10
nc 2
nop 5
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
        {
62
            $user = User::where('username', $obj->user->login)
63
                ->where('provider', 'github')->first();
64
        }
65
66
        return [
67
            'provider_id' => $obj->id,
68
            '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...
69
            'product_backlog_id' => $productBracklogId,
70
            'effort' => 0,
71
            'config_issue_effort_id' => 1,
72
            'issue_type_id' => 1,
73
            'number' => $obj->number,
74
            'title' => $obj->title,
75
            'description' => $obj->body,
76
            'state' => $obj->state,
77
            'html_url' => $obj->html_url,
78
            'created_at' => $obj->created_at,
79
            'updated_at' => $obj->updated_at,
80
        ];
81
    }
82
83
    public function tplOrganization($obj)
84
    {
85
        return [
86
            'provider_id' => $obj->id,
87
            'username' => $obj->login,
88
            'url' => isset($obj->url) ? $obj->url : null,
89
            'repos_url' => isset($obj->repos_url) ? $obj->repos_url : null,
90
            'events_url' => isset($obj->events_url) ? $obj->events_url : null,
91
            'hooks_url' => isset($obj->hooks_url) ? $obj->hooks_url : null,
92
            'issues_url' => isset($obj->issues_url) ? $obj->issues_url : null,
93
            'members_url' => isset($obj->members_url) ? $obj->members_url : null,
94
            'public_members_url' => isset($obj->public_members_url) ? $obj->public_members_url : null,
95
            'avatar_url' => isset($obj->avatar_url) ? $obj->avatar_url : null,
96
            'description' => isset($obj->description) ? $obj->description : null,
97
            'title' => isset($obj->name) ? $obj->name : null,
98
            'blog' => isset($obj->blog) ? $obj->blog : null,
99
            'location' => isset($obj->location) ? $obj->location : null,
100
            'email' => isset($obj->email) ? $obj->email : null,
101
            'public_repos' => isset($obj->public_repos) ? $obj->public_repos : null,
102
            'html_url' => isset($obj->html_url) ? $obj->html_url : null,
103
            'total_private_repos' => isset($obj->total_private_repos) ? $obj->total_private_repos : null,
104
            'since' => Carbon::parse((isset($obj->created_at) ? $obj->created_at : Carbon::now()))->toDateTimeString(),
105
            'disk_usage' => isset($obj->disk_usage) ? $obj->disk_usage : null,
106
        ];
107
    }
108
109
    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...
110
    {
111
112
        $response = collect(Helper::request('https://api.github.com/user/repos?page='. $page))->map(function ($repo) {
113
            return $this->tplRepository($repo);
114
        });
115
116
        if(is_null($repos)){
117
            $repos = collect();
118
        }
119
120
        $repos->push($response);
121
122
        if ( $response->count() == 30){
123
            $this->readRepositories(++$page, $repos);
124
        }
125
126
        return $repos->flatten(1)->sortBy('title');
127
    }
128
129
    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...
130
    {
131
        $params = [
132
            'name' => str_slug($obj->title, '-'),
133
            'description' => $obj->description,
134
        ];
135
136
        if (is_null($oldTitle)) {
137
            $endpoint = 'https://api.github.com/orgs/'.$owner.'/repos';
138
139
            if (Auth::user()->username == $owner) {
140
                $endpoint = 'https://api.github.com/user/repos';
141
            }
142
143
            $response = Helper::request($endpoint, true, 'POST', $params);
144
        } else {
145
            $oldTitle = str_slug($oldTitle, '-');
146
            $response = Helper::request('https://api.github.com/repos/'.$owner.DIRECTORY_SEPARATOR.$oldTitle, true, 'POST', $params);
147
        }
148
149
        return (object) $response;
150
    }
151
152
    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...
153
    {
154
        $organization = Organization::where('username', $login)
155
            ->where('provider', 'github')->first();
156
157
        if (!isset($organization)) {
158
            $response = Helper::request('https://api.github.com/orgs/'.$login);
159
160
            if (!isset($response->id)) {
161
                $response = Helper::request('https://api.github.com/users/'.$login);
162
            }
163
164
            if(isset($response->id)) {
165
                $organization = Organization::create($this->tplOrganization($response));
166
            }
167
        }
168
169
        if(is_null($organization->users()->where('users_has_organizations.user_id', Auth::id())
170
            ->where('users_has_organizations.organization_id', $organization->id)->first())){
171
                $organization->users()->attach(Auth::id());
172
        }
173
174
        return $organization->id;
175
    }
176
177
    public function readCollaborators($owner, $repo, $providerId = null)
178
    {
179
        $ids = collect();
180
181
        collect(Helper::request('https://api.github.com/repos/'.$owner.'/'.$repo.'/collaborators'))
182
            ->map(function($collaborator) use ($ids){
183
184
            $user = User::where('username', $collaborator->login)
185
                ->where('provider', 'github')->first();
186
187
            if (!isset($user)) {
188
                $user = User::create($this->tplUser($collaborator));
189
            }
190
191
            $ids->push($user->id);
192
        });
193
194
        $organization = Organization::where('username', $owner)
195
            ->where('provider', 'github')->first()->users();
196
197
        $organization->syncWithoutDetaching($ids->diff($organization->pluck('user_id')->toArray()));
198
199
    }
200
201
    public function createBranches($owner, $productBacklogId, $repo, $providerId = null, $page = 1)
202
    {
203
        $branches = collect(Helper::request('https://api.github.com/repos/'.$owner.DIRECTORY_SEPARATOR.$repo.'/branches?page='.$page));
204
205
        $branches->map(function($branch) use ($productBacklogId){
206
            $data = [
207
                'product_backlog_id' => $productBacklogId,
208
                'title' => $branch->name,
209
                'sha' => $branch->commit->sha,
210
            ];
211
            Branch::create($data);
212
        });
213
214
        if($branches->count()==30){
215
            $this->createBranches($owner, $productBacklogId, $repo, $providerId, ++$page);
216
        }
217
    }
218
219
    public function readIssues($productBacklogId = null)
220
    {
221
222
        if ( is_null($productBacklogId) ) {
223
            $productBacklog = ProductBacklog::all();
224
        } else {
225
            $productBacklog = ProductBacklog::find($productBacklogId);
226
        }
227
228
        $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...
229
230
            $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...
231
                DIRECTORY_SEPARATOR.$repo->title.'/issues?state=all'))->map(function($issue) use($repo){
232
233
                $data = $this->tplIssue($issue, $repo->id);
234
235
                if (!Issue::where('provider_id', $issue->id)->where('provider', 'github')->first()) {
236
                    Issue::create($data)->users()->attach($data['user_id']);
237
                }
238
239
            });
240
        });
241
    }
242
243
    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...
244
    {
245
        $params = [
246
            'title' => $obj->title,
247
            'body' => $obj->description,
248
        ];
249
250
        $response = Helper::request('https://api.github.com/repos/'.
251
            $obj->productBacklog->organization->username.DIRECTORY_SEPARATOR.
252
            $obj->productBacklog->title.'/issues'.(isset($obj->number) ? DIRECTORY_SEPARATOR.$obj->number : ''),
253
            true, 'POST', $params);
254
255
        return (object) $response;
256
    }
257
258
    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...
259
    {
260
        $params = [
261
            'body' => $obj->comment,
262
        ];
263
264
        $response = Helper::request('https://api.github.com/repos/'.
265
            $obj->issue->productBacklog->organization->username.DIRECTORY_SEPARATOR.
266
            $obj->issue->productBacklog->title.'/issues'.(isset($obj->provider_id) ? '' : DIRECTORY_SEPARATOR.$obj->issue->number).'/comments'.
267
            (isset($obj->provider_id) ? DIRECTORY_SEPARATOR.$obj->provider_id : ''),
268
            true, $verb, $params);
269
270
        return (object) $response;
271
    }
272
273
    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...
274
    {
275
        return $this->createOrUpdateIssueComment($obj, 'DELETE');
276
    }
277
}
278