Completed
Push — master ( 5932ed...faf75e )
by Joseph
06:23
created

SocialiteAuth   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A beforeLogin() 0 4 1
A newUser() 0 4 1
A verifyBeforeLogin() 0 6 2
A handleNewUser() 0 13 3
1
<?php
2
3
namespace STS\SocialiteAuth;
4
5
use Closure;
6
use Illuminate\Contracts\Auth\Authenticatable;
7
use Laravel\Socialite\Contracts\User;
8
9
class SocialiteAuth
10
{
11
    /**
12
     * @var array
13
     */
14
    protected $config;
15
16
    /**
17
     * @var \Closure
18
     */
19
    protected $verifyUser = null;
20
21
    /**
22
     * @var \Closure
23
     */
24
    protected $handleNewUser = null;
25
26
    public function __construct(array $config)
27
    {
28
        $this->config = $config;
29
    }
30
31
    /**
32
     * Provide custom logic for verifying a user before login.
33
     *
34
     * @param \Closure
35
     */
36
    public function beforeLogin(Closure $beforeLogin)
37
    {
38
        $this->verifyUser = $beforeLogin;
39
    }
40
41
    /**
42
     * Provide custom handler for setting up a new user.
43
     *
44
     * @param Closure $handleNewUser
45
     */
46
    public function newUser(Closure $handleNewUser)
47
    {
48
        $this->handleNewUser = $handleNewUser;
49
    }
50
51
    /**
52
     * Verify that the user meets login requirements
53
     * according to the custom logic provided.
54
     *
55
     * @param Authenticatable
56
     *
57
     * @return bool
58
     */
59
    public function verifyBeforeLogin(Authenticatable $user): bool
60
    {
61
        return $this->verifyUser
62
            ? call_user_func($this->verifyUser, $user)
63
            : true;
64
    }
65
66
    /**
67
     * @param $user
68
     *
69
     * @return bool|Authenticatable
70
     */
71
    public function handleNewUser(User $user)
72
    {
73
        if ($this->handleNewUser) {
74
            return call_user_func($this->handleNewUser, $user);
75
        }
76
77
        if ($this->config['match'] == false) {
78
            $user = new Identity($user);
79
            session()->put('socialite-auth.' . $user->getAuthIdentifier(), $user);
80
81
            return $user;
82
        }
83
    }
84
}
85