UserRepository   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 141
ccs 0
cts 81
cp 0
rs 10
c 0
b 0
f 0
wmc 13

1 Method

Rating   Name   Duplication   Size   Complexity  
A model() 0 3 1
1
<?php
2
3
namespace App\Repositories\Eloquent;
4
5
use App\User;
6
use Illuminate\Support\Collection;
7
8
class UserRepository extends BaseRepository
9
{
10
11
    public function findByUserNameOrCreate($userData, $provider)
12
    {
13
14
        $user = User::where('provider_id', '=', $userData->id)->first();
15
        if (!$user) {
16
            // Check if there is no other user with same email
17
            $user = User::where('email', '=', $userData->email)->first();
18
            if (!$user) {
19
20
                // get Large avatar
21
                $avatar = str_replace('sz=50', 'sz=200', $userData->avatar);
22
                $avatar = str_replace('type=normal', 'type=large ', $avatar);
23
                $user = User::create([
24
                    'provider' => $provider,
25
                    'provider_id' => $userData->id,
26
                    'name' => $userData->name,
27
                    'firstname' => $userData->name,
28
                    'slug' => str_slug($userData->name),
29
                    'email' => $userData->email,
30
                    'avatar' => $avatar,
31
                    'role_id' => config('constants.ROLE_USER'),
32
                    'verified' => 1,
33
                ]);
34
            } else {
35
                return null;
36
            }
37
        }
38
39
        $this->checkIfUserNeedsUpdating($userData, $user);
40
        return $user;
41
    }
42
43
    public function checkIfUserNeedsUpdating($userData, $user)
44
    {
45
46
        $socialData = [
47
            'avatar' => $userData->avatar,
48
            'email' => $userData->email,
49
            'firstname' => $userData->name,
50
            'name' => $userData->nickname,
51
        ];
52
        $dbData = [
53
            'avatar' => $user->avatar,
54
            'email' => $user->email,
55
            'firstname' => $user->name,
56
            'name' => $user->username,
57
        ];
58
59
        if (!empty(array_diff($socialData, $dbData))) {
60
61
            $avatar = str_replace('sz=50', 'sz=200', $userData->avatar);
62
            $avatar = str_replace('type=normal', 'type=large', $avatar);
63
64
            $user->avatar = $avatar;
65
            $user->slug = str_slug($userData->name);
66
            $user->email = $userData->email;
67
            $user->firstname = $userData->name;
68
            if (strlen($userData->nickname) != 0)
69
                $user->name = $userData->nickname;
70
            else
71
                $user->name = $userData->email;
72
            $user->save();
73
        }
74
    }
75
76
    /**
77
     * Specify Model class name
78
     *
79
     * @return mixed
80
     */
81
    function model()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
82
    {
83
        return User::class;
84
    }
85
86
    /**
87
     * Find data by field and value
88
     *
89
     * @param       $field
90
     * @param       $value
91
     * @param array $columns
92
     *
93
     * @return Collection
94
     */
95
96
    public function firstByField($field = null, $value = null, $columns = ['*'])
97
    {
98
        return User::where($field, '=', $value)->first($columns);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\User::where($...value)->first($columns) also could return the type App\User which is incompatible with the documented return type Illuminate\Support\Collection.
Loading history...
99
100
    }
101
102
    /**
103
     * @param null $userSlug
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $userSlug is correct as it would always require null to be passed?
Loading history...
104
     * @return \Illuminate\Database\Eloquent\Model
105
     */
106
    public function findBySlug($userSlug = null)
107
    {
108
        return User::findBySlug($userSlug);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\User::findBySlug($userSlug) also could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the documented return type Illuminate\Database\Eloquent\Model.
Loading history...
109
110
    }
111
112
    /**
113
     * @param null $userSlug
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $userSlug is correct as it would always require null to be passed?
Loading history...
114
     * @param array $columns
115
     * @return Collection
116
     */
117
    public function findBySlugWithTrashed($userSlug = null, $columns = ['*'])
118
    {
119
        return User::withTrashed()->whereSlug($userSlug)->get($columns);
120
121
    }
122
123
124
    /**
125
     * @return User|\Illuminate\Database\Eloquent\Builder
126
     */
127
    public function getUsersWithCountriesAndRoles()
128
    {
129
        return User::with('country', 'role');
130
    }
131
132
    /**
133
     * @param $slug
134
     * @return Collection
135
     */
136
    public function getSoftDeletedUserBySlug($slug)
137
    {
138
        return User::onlyTrashed()->where('slug', '=', $slug)->first();
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\User::onlyTra...', '=', $slug)->first() also could return the type App\User which is incompatible with the documented return type Illuminate\Support\Collection.
Loading history...
139
140
    }
141
142
    /**
143
     * @param User $user
144
     * @return Collection
145
     */
146
    public function getSoftDeletedUser(User $user)
147
    {
148
        return User::onlyTrashed()->where('email', '=', $user->email)->first();
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\User::onlyTra... $user->email)->first() also could return the type App\User which is incompatible with the documented return type Illuminate\Support\Collection.
Loading history...
149
    }
150
}