GitLabPush::arrangeCommit()   F
last analyzed

Complexity

Conditions 12
Paths 512

Size

Total Lines 35

Duplication

Lines 35
Ratio 100 %

Importance

Changes 0
Metric Value
dl 35
loc 35
rs 3.4777
c 0
b 0
f 0
cc 12
nc 512
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 Fabrica\WebHook;
4
5
use Fabrica\WebHook\GitPush;
6
use Cartalyst\Sentinel\Users\EloquentUser;
7
8
class GitLabPush extends GitPush
9
{
10
11
    /**
12
     * parse the data.
13
     *
14
     * @param  array $data
15
     * @return array
16
     */
17
    public function parse($data)
18
    {
19
        // set the repo
20
        $repo = [];
21
        $repo['name'] = $data['repository']['name'];
22
        $repo['homepage'] = $data['repository']['homepage'];
23
        $this->repo = $repo;
24
        // set the branch
25
        $this->branch = isset($data['ref']) ? substr($data['ref'], 11) : ''; // refs/heads/
26
        // get the pusher 
27
        $user_name = isset($data['user_name']) ? $data['user_name'] : '';
28
        $user_email = isset($data['user_email']) ? $data['user_email'] : '';
29
        $user = [ 'name' => $user_name, 'email' => $user_email ];
30 View Code Duplication
        if ($user_email) {
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...
31
            $user2 = EloquentUser::where('email', $user_email)->first();
32
            if ($user2) {
33
                $user['id']     = $user2->id;
34
                $user['name']   = $user2->first_name;
35
                $user['email']  = $user2->email;
36
            }
37
        }
38
        $this->pusher = $user;
39
        // set the commits
40
        $this->commits = isset($data['commits']) ? $data['commits'] : [];
41
        // get the after
42
        $this->after = isset($data['after']) ? $data['after'] : '';
43
        // get the before 
44
        $this->before = isset($data['before']) ? $data['before'] : '';
45
    }
46
47
    /**
48
     * arrange the data format.
49
     *
50
     * @param  array $commit
51
     * @return array
52
     */
53 View Code Duplication
    public function arrangeCommit($commit)
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...
54
    {
55
        $new_commit = [];
56
57
        $new_commit['repo'] = $this->getRepo();
58
        $new_commit['branch'] = $this->getBranch();
59
        $new_commit['pusher'] = $this->getPusher();
60
        $new_commit['pushed_at'] = time();
61
62
        $new_commit['sha']          = isset($commit['id']) ? $commit['id'] : '';
63
        $new_commit['url']          = isset($commit['url']) ? $commit['url'] : '';
64
        $new_commit['message']      = isset($commit['message']) ? trim($commit['message']) : '';
65
        $new_commit['added']        = isset($commit['added']) ? $commit['added'] : [];
66
        $new_commit['modified']     = isset($commit['modified']) ? $commit['modified'] : [];
67
        $new_commit['removed']      = isset($commit['removed']) ? $commit['removed'] : [];
68
        $new_commit['committed_at'] = isset($commit['timestamp']) ? strtotime($commit['timestamp']) : '';
69
70
        if (isset($commit['author'])) {
71
            $new_commit['author'] = $commit['author'];
72
            if (isset($commit['author']['email']) && $commit['author']['email']) {
73
                $new_author = EloquentUser::where('email', $commit['author']['email'])->first();
74
                if ($new_author) {
75
                    $new_commit['author']['id']     = $new_author->id;
76
                    $new_commit['author']['name']   = $new_author->first_name;
77
                    $new_commit['author']['email']  = $new_author->email;
78
                }
79
            }
80
        }
81
        else
82
        {
83
            $new_commit['author'] = [];
84
        }
85
86
        return $new_commit;
87
    }
88
}
89