Completed
Pull Request — master (#1)
by ARCANEDEV
08:42
created

UserTracker   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 123
ccs 26
cts 28
cp 0.9286
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A instantiateAuthentication() 0 6 2
A config() 0 4 1
A getConfig() 0 4 1
A user() 0 4 1
A track() 0 6 2
A check() 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 implements UserTrackerContract
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Properties
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /** @var \Illuminate\Contracts\Foundation\Application */
19
    private $app;
20
21
    /** @var array */
22
    private $auths = [];
23
24
    /* ------------------------------------------------------------------------------------------------
25
     |  Constructor
26
     | ------------------------------------------------------------------------------------------------
27
     */
28
    /**
29
     * UserTracker constructor.
30
     *
31
     * @param  \Illuminate\Contracts\Foundation\Application  $app
32
     */
33 18
    public function __construct(Application $app)
34
    {
35 18
        $this->app = $app;
36
37 18
        $this->instantiateAuthentication();
38 18
    }
39
40
    /**
41
     * Grab all the authentication bindings.
42
     */
43 18
    private function instantiateAuthentication()
44
    {
45 18
        foreach ((array) $this->getConfig('auth.bindings', []) as $binding) {
46 18
            $this->auths[] = $this->app->make($binding);
47 9
        }
48 18
    }
49
50
    /* ------------------------------------------------------------------------------------------------
51
     |  Getters & Setters
52
     | ------------------------------------------------------------------------------------------------
53
     */
54
    /**
55
     * Get the config instance.
56
     *
57
     * @return \Illuminate\Contracts\Config\Repository
58
     */
59 18
    private function config()
60
    {
61 18
        return $this->app['config'];
62
    }
63
64
    /**
65
     * Get the tracker config.
66
     *
67
     * @param  string      $key
68
     * @param  mixed|null  $default
69
     *
70
     * @return mixed
71
     */
72 18
    private function getConfig($key, $default = null)
73
    {
74 18
        return $this->config()->get("laravel-tracker.$key", $default);
75
    }
76
77
    /* ------------------------------------------------------------------------------------------------
78
     |  Main Functions
79
     | ------------------------------------------------------------------------------------------------
80
     */
81
    /**
82
     * Track the current authenticated user id.
83
     *
84
     * @return int|null
85
     */
86 12
    public function track()
87
    {
88 12
        return $this->check()
89 6
            ? $this->user()->{$this->getConfig('auth.columns.id', 'id')}
90 12
            : null;
91
    }
92
93
    /* ------------------------------------------------------------------------------------------------
94
     |  Other Functions
95
     | ------------------------------------------------------------------------------------------------
96
     */
97
    /**
98
     * Check if the user is authenticated.
99
     *
100
     * @return bool
101
     */
102 12
    protected function check()
103
    {
104 12
        return $this->executeAuthMethod($this->getConfig('auth.methods.check'));
105
    }
106
107
    /**
108
     * Get the authenticated user.
109
     *
110
     * @return false|mixed
111
     */
112
    protected function user()
113
    {
114
        return $this->executeAuthMethod($this->getConfig('auth.methods.user'));
115
    }
116
117
    /**
118
     * Execute the auth method.
119
     *
120
     * @param  string  $method
121
     *
122
     * @return mixed|false
123
     */
124 12
    private function executeAuthMethod($method)
125
    {
126 12
        foreach ($this->auths as $auth) {
127 12
            if (is_callable([$auth, $method], true)) {
128 12
                if ($data = $auth->{$method}()) return $data;
129 6
            }
130 6
        }
131
132 12
        return false;
133
    }
134
}
135