SocialAuthController   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
c 2
b 0
f 0
lcom 1
cbo 5
dl 0
loc 130
ccs 0
cts 69
cp 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A redirectToAuthenticationServiceProvider() 0 4 1
A handleAuthenticationServiceProviderCallback() 0 13 2
A findOrCreateUser() 0 8 2
B createUser() 0 27 3
A newUser() 0 6 1
A userExistsByProviderUserId() 0 9 2
A userExistsByEmail() 0 8 2
A createUserApiKey() 0 5 1
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use App\Http\Controllers\Controller;
6
use App\OAuthIdentity;
7
use App\User;
8
use Chrisbjr\ApiGuard\Models\ApiKey;
9
use Exception;
10
use Illuminate\Http\Response;
11
use Illuminate\Support\Facades\Auth;
12
use Illuminate\Support\Facades\Config;
13
use Illuminate\Support\Facades\Redirect;
14
use Laravel\Socialite\Facades\Socialite;
15
16
/**
17
 * Class SocialAuthController.
18
 */
19
class SocialAuthController extends Controller
20
{
21
    /**
22
     * Redirect the user to the Provider authentication page.
23
     *
24
     * @return Response
25
     */
26
    public function redirectToAuthenticationServiceProvider($provider)
27
    {
28
        return Socialite::driver($provider)->redirect();
29
    }
30
31
    /**
32
     * Obtain the user information from authentication service provider.
33
     *
34
     * @return Response
35
     */
36
    public function handleAuthenticationServiceProviderCallback($provider)
37
    {
38
        try {
39
            $user = Socialite::driver($provider)->user();
40
        } catch (Exception $e) {
41
            return Redirect::to('auth/'.$provider);
42
        }
43
44
        $authUser = $this->findOrCreateUser($user, $provider);
45
        Auth::login($authUser, true);
46
47
        return Redirect::to('upload');
48
    }
49
50
    /**
51
     * @param $providerUser
52
     * @param $provider
53
     *
54
     * @return bool|mixed
55
     */
56
    private function findOrCreateUser($providerUser, $provider)
57
    {
58
        if ($authUser = $this->userExistsByProviderUserId($providerUser)) {
59
            return $authUser;
60
        }
61
62
        return $this->createUser($providerUser, $provider);
63
    }
64
65
    /**
66
     * @param $providerUser
67
     * @param $provider
68
     *
69
     * @return User
70
     */
71
    private function createUser($providerUser, $provider)
72
    {
73
        $user = $providerUser;
74
        $userExsists = $this->userExistsByEmail($providerUser);
75
76
        if (!$userExsists) {
77
            $user = $this->newUser();
78
            foreach (['name', 'email', 'avatar'] as $item) {
79
                $user->$item = $providerUser->$item;
80
            }
81
82
            $user->save();
83
        }
84
85
        $oAuthIdentity = new OAuthIdentity();
86
        $oAuthIdentity->provider_user_id = $providerUser->getId();
0 ignored issues
show
Documentation introduced by
The property provider_user_id does not exist on object<App\OAuthIdentity>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
87
        $oAuthIdentity->provider = $provider;
0 ignored issues
show
Documentation introduced by
The property provider does not exist on object<App\OAuthIdentity>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
88
        $oAuthIdentity->access_token = $providerUser->token;
0 ignored issues
show
Documentation introduced by
The property access_token does not exist on object<App\OAuthIdentity>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
89
        $oAuthIdentity->user_id = $user->id;
0 ignored issues
show
Documentation introduced by
The property user_id does not exist on object<App\OAuthIdentity>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
90
        $oAuthIdentity->avatar = $providerUser->getAvatar();
0 ignored issues
show
Documentation introduced by
The property avatar does not exist on object<App\OAuthIdentity>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
91
        $oAuthIdentity->name = $providerUser->getName();
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<App\OAuthIdentity>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
92
        $oAuthIdentity->nickname = $providerUser->getNickname();
0 ignored issues
show
Documentation introduced by
The property nickname does not exist on object<App\OAuthIdentity>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
93
        $this->createUserApiKey($user);
94
        $oAuthIdentity->save();
95
96
        return $user;
97
    }
98
99
    /**
100
     * @return mixed
101
     */
102
    private function newUser()
103
    {
104
        $user_model = Config::get('laraveltube-socialite.model');
105
106
        return new $user_model();
107
    }
108
109
    /**
110
     * @param $providerUser
111
     *
112
     * @return bool|mixed
113
     */
114
    private function userExistsByProviderUserId($providerUser)
115
    {
116
        /** @var OAuthIdentity $provUser */
117
        if ($provUser = OAuthIdentity::where('provider_user_id', $providerUser->id)->first()) {
118
            return $provUser->user;
119
        }
120
121
        return false;
122
    }
123
124
    /**
125
     * @param $providerUser
126
     *
127
     * @return bool
128
     */
129
    private function userExistsByEmail($providerUser)
130
    {
131
        if ($user = User::where('email', $providerUser->email)->first()) {
132
            return $user;
133
        }
134
135
        return false;
136
    }
137
138
    /**
139
     * @param User $user
140
     *
141
     * @return mixed
142
     */
143
    private function createUserApiKey(User $user)
144
    {
145
        $apiKey = ApiKey::make($user->id);
146
        $user->apiKey()->save($apiKey);
147
    }
148
}
149