Completed
Push — master ( f01045...b18803 )
by ARCANEDEV
07:38
created

UserTracker::user()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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 24
    public function __construct(Application $app)
34
    {
35 24
        $this->app = $app;
36
37 24
        $this->instantiateAuthentication();
38 24
    }
39
40
    /**
41
     * Grab all the authentication bindings.
42
     */
43 24
    private function instantiateAuthentication()
44
    {
45 24
        foreach ((array) $this->getConfig('auth.bindings', []) as $binding) {
46 24
            $this->auths[] = $this->app->make($binding);
47 12
        }
48 24
    }
49
50
    /* ------------------------------------------------------------------------------------------------
51
     |  Getters & Setters
52
     | ------------------------------------------------------------------------------------------------
53
     */
54
    /**
55
     * Get the config instance.
56
     *
57
     * @return \Illuminate\Contracts\Config\Repository
58
     */
59 24
    private function config()
60
    {
61 24
        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 24
    private function getConfig($key, $default = null)
73
    {
74 24
        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 6
    public function track()
87
    {
88 6
        return $this->check()
89 3
            ? $this->user()->{$this->getConfig('auth.columns.id', 'id')}
90 6
            : null;
91
    }
92
93
    /* ------------------------------------------------------------------------------------------------
94
     |  Other Functions
95
     | ------------------------------------------------------------------------------------------------
96
     */
97
    /**
98
     * Check if the user is authenticated.
99
     *
100
     * @return bool
101
     */
102 6
    protected function check()
103
    {
104 6
        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 6
    private function executeAuthMethod($method)
125
    {
126 6
        foreach ($this->auths as $auth) {
127 6
            if (is_callable([$auth, $method], true)) {
128 6
                if ($data = $auth->{$method}()) return $data;
129 3
            }
130 3
        }
131
132 6
        return false;
133
    }
134
}
135