SocialiteUser   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 241
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 4
dl 0
loc 241
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A init() 0 8 1
A register() 0 21 4
A getProvider() 0 4 1
A getUser() 0 4 1
A setUser() 0 6 1
A setProvider() 0 6 1
A user() 0 4 1
A getOrCreateEmptySocialUser() 0 9 2
A getSocialiteRepository() 0 4 1
A getUserRepository() 0 4 1
A associateSocialiteUser() 0 11 2
A checkEmail() 0 4 1
A tryToAssociateUser() 0 10 3
A createSocialUserWithEmail() 0 13 1
A getFirstAndLustNames() 0 7 2
A login() 0 4 1
A avatar() 0 6 1
1
<?php
2
3
namespace App\Services;
4
5
use App\Repositories\UserRepository;
6
use App\Socialite;
7
use Auth;
8
use Laravel\Socialite\Contracts\User as ProviderUser;
9
use App\Repositories\SocialiteRepository;
10
11
class SocialiteUser
12
{
13
    /**
14
     * @var ProviderUser
15
     */
16
    private $user;
17
18
    /**
19
     * @var string
20
     */
21
    private $provider;
22
23
    /**
24
     * SocialiteUser constructor.
25
     */
26
    public function __construct()
27
    {
28
        $this->socialRepository = $this->getSocialiteRepository();
0 ignored issues
show
Bug introduced by
The property socialRepository does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
29
    }
30
31
    /**
32
     * @param $provider
33
     * @param $callback
34
     * @return $this
35
     */
36
    public function init($provider, $callback)
37
    {
38
        $this
39
            ->setProvider($provider)
40
            ->setUser($callback);
41
42
        return $this;
43
    }
44
45
    /**
46
     * Register.
47
     *
48
     * @return mixed|null
49
     */
50
    public function register()
51
    {
52
        if ($this->socialRepository->checkProviderUser($this->getProvider(), $this->user()->getId()))
53
        {
54
            $s_user = $this->socialRepository->getUserByProvider(
55
                $this->getProvider(), $this->user()->getId()
56
            );
57
58
            if($user = $s_user->user)
59
                return $user;
60
61
            return $this;
62
        }
63
64
        $social = $this->getOrCreateEmptySocialUser();
65
66
        if($this->checkEmail())
67
            return $this->createSocialUserWithEmail($social);
0 ignored issues
show
Documentation introduced by
$social is of type null|object<App\Services\SocialiteUser>, but the function expects a object<App\Socialite>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
68
69
        return $this;
70
    }
71
72
    /**
73
     * @return mixed
74
     */
75
    public function getProvider()
76
    {
77
        return $this->provider;
78
    }
79
80
    /**
81
     * @return ProviderUser
82
     */
83
    public function getUser()
84
    {
85
        return $this->user;
86
    }
87
88
    /**
89
     * Set user.
90
     *
91
     * @param $user
92
     * @return $this
93
     */
94
    public function setUser($user)
95
    {
96
        $this->user = $user;
97
98
        return $this;
99
    }
100
101
    /**
102
     * Set provider.
103
     *
104
     * @param $provider
105
     * @return $this
106
     */
107
    public function setProvider($provider)
108
    {
109
        $this->provider = $provider;
110
111
        return $this;
112
    }
113
114
    /**
115
     * @return ProviderUser
116
     */
117
    protected function user()
118
    {
119
        return $this->getUser();
120
    }
121
122
    /**
123
     * Get or create social user
124
     *
125
     * @return null|static
126
     */
127
    protected function getOrCreateEmptySocialUser()
128
    {
129
        if($this->socialRepository->checkProviderUser($this->getProvider(), $this->user()->getId()))
130
            return $this->socialRepository->getUserByProvider($this->getProvider(), $this->user()->getId());
131
132
        return $this->socialRepository->createEmpty(
133
            $this->getProvider(), $this->user()
134
        );
135
    }
136
137
    /**
138
     * @return SocialiteRepository
139
     */
140
    private function getSocialiteRepository()
141
    {
142
        return new SocialiteRepository();
143
    }
144
145
    /**
146
     * @return UserRepository
147
     */
148
    private function getUserRepository()
149
    {
150
        return new UserRepository();
151
    }
152
153
    /**
154
     * Associate user.
155
     *
156
     * @param $account
157
     * @param null $email
158
     * @return mixed
159
     */
160
    private function associateSocialiteUser($account, $email = null)
161
    {
162
        if(! $email)
163
            $email = $this->user()->getEmail();
164
165
        $user = $this->getUserRepository()->getByEmail($email);
166
        $account->user()->associate($user);
167
        $account->save();
168
169
        return $user;
170
    }
171
172
    /**
173
     * Check if email exists;
174
     *
175
     * @return bool
176
     */
177
    private function checkEmail()
178
    {
179
        return (bool) $this->user()->getEmail();
180
    }
181
182
    /**
183
     * @param $social
184
     * @return bool
185
     */
186
    public function tryToAssociateUser($social, $email = null)
187
    {
188
        if(! $email)
189
            $email = $this->user()->getEmail();
190
191
        if($this->getUserRepository()->checkIfUserExists($email))
192
            return $this->associateSocialiteUser($social, $email);
193
194
        return false;
195
    }
196
197
    /**
198
     * Create social user.
199
     *
200
     * @param Socialite $social
201
     * @return mixed
202
     */
203
    private function createSocialUserWithEmail(Socialite $social)
204
    {
205
        $this->tryToAssociateUser($social);
206
        $names = $this->getFirstAndLustNames();
207
208
        return $this->getUserRepository()->create([
0 ignored issues
show
Bug introduced by
The method create() does not exist on App\Repositories\UserRepository. Did you maybe mean createSimpleUser()?

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...
209
            'email' => $this->user()->getEmail(),
210
            'name' => $this->user()->getName(),
211
            'password' => $this->users->hashPassword(str_random(45)),
0 ignored issues
show
Bug introduced by
The property users does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
212
            'firstname' => @$names[0],
213
            'lastname' => @$names[1]
214
        ]);
215
    }
216
217
    /**
218
     * @param null $name
219
     * @return array
220
     */
221
    public function getFirstAndLustNames($name = null)
222
    {
223
        if(! $name)
224
            $name = $this->user()->getName();
225
226
        return explode(' ', $name);
227
    }
228
229
    /**
230
     * Login the user.
231
     *
232
     * @param $user
233
     */
234
    public function login($user)
235
    {
236
        \Auth::login($user, true);
237
    }
238
239
    /**
240
     * Add avatar.
241
     *
242
     * @param $avatar
243
     * @return $this
244
     */
245
    public function avatar($avatar)
246
    {
247
        (new ImageProcessor())->changeAvatar($avatar);
248
249
        return $this;
250
    }
251
}