GuardHelpers   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 42
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveSocialiteIdentifierName() 0 14 3
A attemptFromSocialite() 0 24 4
1
<?php
2
3
namespace STS\SocialiteAuth;
4
5
use Illuminate\Contracts\Auth\Authenticatable;
6
use Laravel\Socialite\Contracts\User;
7
use STS\SocialiteAuth\Contracts\SocialiteAuthenticatable;
8
use STS\SocialiteAuth\Facades\SocialiteAuth;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, STS\SocialiteAuth\SocialiteAuth.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
10
class GuardHelpers
11
{
12
    protected function attemptFromSocialite()
13
    {
14
        return function(User $user, $socialiteField) {
15
16
            $modelSocialiteField = $this->resolveSocialiteIdentifierName();
17
18
            // First try to find a match
19
            $userModel = $this->provider->retrieveByCredentials([
0 ignored issues
show
Bug introduced by
The property provider 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...
20
                $modelSocialiteField => $user[$socialiteField]
21
            ]);
22
23
            // No match? See if we have a custom handler for new users
24
            if(!$userModel) {
25
                $userModel = SocialiteAuth::handleNewUser($user);
26
            }
27
28
            if ($userModel && SocialiteAuth::verifyBeforeLogin($userModel)) {
29
                $this->login($userModel);
0 ignored issues
show
Bug introduced by
The method login() does not seem to exist on object<STS\SocialiteAuth\GuardHelpers>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
                return true;
31
            }
32
33
            return false;
34
        };
35
    }
36
37
    protected function resolveSocialiteIdentifierName()
38
    {
39
        return function() {
40
            $modelField = 'email';
41
            if (method_exists($this->provider, 'createModel')) {
42
                $new = $this->provider->createModel();
43
                if ($new instanceof SocialiteAuthenticatable) {
44
                    $modelField = $new->getSocialiteIdentifierName();
45
                }
46
            }
47
48
            return $modelField;
49
        };
50
    }
51
}
52