Completed
Push — master ( ec2990...7a5f39 )
by ARCANEDEV
07:59
created

UserTracker   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 93
ccs 22
cts 24
cp 0.9167
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A instantiateAuthentication() 0 6 2
A track() 0 6 2
A check() 0 4 1
A user() 0 4 1
A executeAuthMethod() 0 10 4
1
<?php namespace Arcanedev\LaravelTracker\Trackers;
2
3
use Arcanedev\LaravelTracker\Contracts\Trackers\UserTracker as UserTrackerContract;
4
use Illuminate\Contracts\Foundation\Application;
5
6
/**
7
 * Class     UserTracker
8
 *
9
 * @package  Arcanedev\LaravelTracker\Trackers
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class UserTracker extends AbstractTracker implements UserTrackerContract
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Properties
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /** @var array */
19
    private $auths = [];
20
21
    /* ------------------------------------------------------------------------------------------------
22
     |  Constructor
23
     | ------------------------------------------------------------------------------------------------
24
     */
25
    /**
26
     * UserTracker constructor.
27
     *
28
     * @param  \Illuminate\Contracts\Foundation\Application  $app
29
     */
30 18
    public function __construct(Application $app)
31
    {
32 18
        parent::__construct($app);
33
34 18
        $this->instantiateAuthentication();
35 18
    }
36
37
    /**
38
     * Grab all the authentication bindings.
39
     */
40 18
    private function instantiateAuthentication()
41
    {
42 18
        foreach ((array) $this->getConfig('auth.bindings', []) as $binding) {
43 18
            $this->auths[] = $this->make($binding);
44 9
        }
45 18
    }
46
47
    /* ------------------------------------------------------------------------------------------------
48
     |  Main Functions
49
     | ------------------------------------------------------------------------------------------------
50
     */
51
    /**
52
     * Track the current authenticated user id.
53
     *
54
     * @return int|null
55
     */
56 12
    public function track()
57
    {
58 12
        return $this->check()
59 6
            ? $this->user()->{$this->getConfig('auth.columns.id', 'id')}
60 12
            : null;
61
    }
62
63
    /* ------------------------------------------------------------------------------------------------
64
     |  Other Functions
65
     | ------------------------------------------------------------------------------------------------
66
     */
67
    /**
68
     * Check if the user is authenticated.
69
     *
70
     * @return bool
71
     */
72 12
    protected function check()
73
    {
74 12
        return $this->executeAuthMethod($this->getConfig('auth.methods.check'));
75
    }
76
77
    /**
78
     * Get the authenticated user.
79
     *
80
     * @return false|mixed
81
     */
82
    protected function user()
83
    {
84
        return $this->executeAuthMethod($this->getConfig('auth.methods.user'));
85
    }
86
87
    /**
88
     * Execute the auth method.
89
     *
90
     * @param  string  $method
91
     *
92
     * @return mixed|false
93
     */
94 12
    private function executeAuthMethod($method)
95
    {
96 12
        foreach ($this->auths as $auth) {
97 12
            if (is_callable([$auth, $method], true)) {
98 12
                if ($data = $auth->{$method}()) return $data;
99 6
            }
100 6
        }
101
102 12
        return false;
103
    }
104
}
105