SocialUserResolver   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 44
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveUserByProviderCredentials() 0 10 2
A googleAuth() 0 12 3
A __construct() 0 3 1
1
<?php
2
3
namespace App\Resolvers;
4
5
use App\Models\User;
6
use App\Services\TenantManager;
7
use Coderello\SocialGrant\Resolvers\SocialUserResolverInterface;
8
use Illuminate\Contracts\Auth\Authenticatable;
9
10
class SocialUserResolver implements SocialUserResolverInterface
11
{
12
    /**
13
     * @var App\Services\TenantManager
0 ignored issues
show
Bug introduced by
The type App\Resolvers\App\Services\TenantManager was not found. Did you mean App\Services\TenantManager? If so, make sure to prefix the type with \.
Loading history...
14
     */
15
    protected $tenantManager;
16
17
    public function __construct(TenantManager $tenantManager)
18
    {
19
        $this->tenantManager = $tenantManager;
0 ignored issues
show
Documentation Bug introduced by
It seems like $tenantManager of type App\Services\TenantManager is incompatible with the declared type App\Resolvers\App\Services\TenantManager of property $tenantManager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
20
    }
21
22
    /**
23
     * Resolve user by provider credentials.
24
     *
25
     * @param string $provider
26
     * @param string $accessToken
27
     *
28
     * @return Authenticatable|null
29
     */
30
    public function resolveUserByProviderCredentials(string $provider, string $accessToken): ?Authenticatable
31
    {
32
        // Return the user that corresponds to provided credentials.
33
        // If the credentials are invalid, then return NULL.
34
        switch ($provider) {
35
            case 'google':
36
                return $this->googleAuth($accessToken);
37
        }
38
39
        return null;
40
    }
41
42
    protected function googleAuth(string $accessToken): ?Authenticatable
43
    {
44
        $client = new \Google_Client(['client_id' => config('auth.google_oauth_client_id')]);
45
        $payload = $client->verifyIdToken($accessToken);
46
        if ($payload) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $payload of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
47
            $userName = $payload['email'];
48
            if ($this->tenantManager->loadTenantByUsername($userName)) {
49
                return User::where('email', $userName)->first();
50
            }
51
        }
52
53
        return null;
54
    }
55
}
56