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.

Users::create()   B
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 2
nop 1
dl 0
loc 26
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace Afrittella\BackProject\Repositories;
4
5
use Afrittella\BackProject\Models\SocialAccount;
6
use Laravel\Socialite\Contracts\User as SocialProvider;
7
8
class Users extends Base
9
{
10
    public function model()
11
    {
12
        return config('back-project.user_model');
13
    }
14
15
    public function create(array $data)
16
    {
17
        $_defaults = [
18
            'email' => null,
19
            'is_social' => 0,
20
            'confirmed' => 0
21
        ];
22
23
        $data = array_merge($_defaults, $data);
24
25
        $user = $this->model->create([
0 ignored issues
show
Bug introduced by
The method create() does not exist on Illuminate\Database\Eloquent\Model. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
26
            'username' => $data['username'],
27
            'email' => $data['email'],
28
            'password' => bcrypt($data['password']),
29
            'confirmation_code' => (($data['is_social'] == 0) ? $this->model->generateConfirmationCode() : ''), //@TODO confirmation_code is dynamic
30
            'confirmed' => (($data['is_social'] == 0) ? $data['confirmed'] : 1),
31
            'is_social' => $data['is_social']
32
            //'role_id' => $data['role_id'],
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
33
        ]);
34
35
        if (!empty($data['roles'])) {
36
            $user->roles()->sync($data['roles']);
37
        }
38
39
        return $user;
40
    }
41
42
    public function update(array $data, $id, $attribute = 'id')
43
    {
44
        $model_data = $this->findBy($attribute, $id);
45
46
        if (isset($data['password'])) {
47
            $data['password'] = bcrypt($data['password']);
48
        }
49
50
        if (!empty($data['roles'])) {
51
            $model_data->roles()->sync($data['roles']);
52
        }
53
        return $model_data->update($data);
54
    }
55
56
    public function createOrGetFromSocial(SocialProvider $socialProvider, $provider = "facebook")
57
    {
58
        // try to get user
59
        $account = SocialAccount::whereProvider($provider)->whereProviderUserId($socialProvider->getId())->first();
60
61
        if ($account) {
62
            return $account->user;
63
        } else {
64
            $account = new SocialAccount([
65
                'provider' => $provider,
66
                'provider_user_id' => $socialProvider->getId(),
67
                'provider_user_info' => json_encode($socialProvider)
68
            ]);
69
70
            if (!empty($socialProvider->getEmail())) {
71
                $user = $this->findBy('email', $socialProvider->getEmail());
72
            }
73
74
            $username = snake_case($socialProvider->getName());
75
76
            while ($this->findBy('username', $username)) {
77
                $username = $this->getUniqueUsername($username);
78
            }
79
80
            if (empty($user)) {
81
                $user = $this->create([
82
                    'username' => $username,
83
                    'email' => $socialProvider->getEmail(),
84
                    'is_social' => 1,
85
                    'password' => 'no_password'
86
                ]);
87
88
                $user->assignRole('user');
89
            }
90
91
            $account->user()->associate($user);
92
            $account->save();
93
94
            return $user;
95
        }
96
    }
97
98
    /**
99
     * @param string $name
100
     */
101
    public function getUniqueUsername($name)
102
    {
103
        $nrRand = rand(0, 100);
104
105
        return $name.$nrRand;
106
    }
107
108
    /**
109
     * Transform data in a table array for view
110
     * @param $data
111
     * @param array $options
112
     * @return array
113
     */
114
    public function transform($data = [], $options = [])
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
115
    {
116
        if (empty($data)) {
117
            $data = $this->all();
118
        }
119
120
        // Table header
121
        $head = [
122
            'columns' => [
123
                "",
124
                //trans('back-project::users.active'),
125
                trans('back-project::users.username'),
126
                trans('back-project::users.email'),
127
                trans('back-project::users.roles'),
128
                trans('back-project::crud.actions'),
129
            ]
130
        ];
131
132
        $body = [];
133
134
        foreach ($data as $row):
135
            $body[] = [
136
                'columns' => [
137
                    ['content' => '<img src="'.\Avatar::create(strtoupper($row->username))->toBase64().'" alt="'.$row->username.'" width="40px" height="40px" /> '],
138
                    //['content' => false, 'action' => false, 'icon' => ($row->isConfirmed() ? "check" : 'times')],
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
139
                    ['content' => $row->username],
140
                    ['content' => $row->email],
141
                    ['content' => implode(',', $row->roles()->pluck('name')->toArray())],
142
                    ['content' => false, 'actions' => [
143
                        'edit' => ['url' => route('bp.users.edit', [$row['id']])], //url('/admin/users/edit', [$row['id']])],
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
144
                        'delete' => ['url' => route('bp.users.delete', [$row['id']])]
145
                    ]
146
                    ],
147
                ]
148
            ];
149
        endforeach;
150
151
        return [
152
            'head' => $head,
153
            'body' => $body
154
        ];
155
    }
156
157
}
158