UserTracker::instantiateAuthentication()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 2
1
<?php namespace Arcanedev\LaravelTracker\Trackers;
2
3
use Arcanedev\LaravelTracker\Contracts\Trackers\UserTracker as UserTrackerContract;
4
use Arcanedev\LaravelTracker\Support\BindingManager;
5
use Illuminate\Contracts\Foundation\Application;
6
7
/**
8
 * Class     UserTracker
9
 *
10
 * @package  Arcanedev\LaravelTracker\Trackers
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class UserTracker extends AbstractTracker implements UserTrackerContract
14
{
15
    /* -----------------------------------------------------------------
16
     |  Properties
17
     | -----------------------------------------------------------------
18
     */
19
20
    /** @var array */
21
    private $auths = [];
22
23
    /* -----------------------------------------------------------------
24
     |  Constructor
25
     | -----------------------------------------------------------------
26
     */
27
28
    /**
29
     * UserTracker constructor.
30
     *
31
     * @param  \Illuminate\Contracts\Foundation\Application  $app
32
     */
33 12
    public function __construct(Application $app)
34
    {
35 12
        parent::__construct($app);
36
37 12
        $this->instantiateAuthentication();
38 12
    }
39
40
    /**
41
     * Grab all the authentication bindings.
42
     */
43 12
    private function instantiateAuthentication()
44
    {
45 12
        foreach ((array) $this->getConfig('auth.bindings', []) as $binding) {
46 12
            $this->auths[] = $this->make($binding);
47
        }
48 12
    }
49
50
    /* -----------------------------------------------------------------
51
     |  Getters and Setters
52
     | -----------------------------------------------------------------
53
     */
54
55
    /**
56
     * Get the model.
57
     *
58
     * @return \Arcanedev\LaravelTracker\Models\User
59
     */
60
    protected function getModel()
61
    {
62
        return $this->makeModel(BindingManager::MODEL_USER);
63
    }
64
65
    /* -----------------------------------------------------------------
66
     |  Main Methods
67
     | -----------------------------------------------------------------
68
     */
69
70
    /**
71
     * Track the current authenticated user id.
72
     *
73
     * @return int|null
74
     */
75 9
    public function track()
76
    {
77 9
        return $this->check()
78
            ? $this->user()->{$this->getConfig('auth.columns.id', 'id')}
79 9
            : null;
80
    }
81
82
    /* -----------------------------------------------------------------
83
     |  Other Methods
84
     | -----------------------------------------------------------------
85
     */
86
87
    /**
88
     * Check if the user is authenticated.
89
     *
90
     * @return bool
91
     */
92 9
    protected function check()
93
    {
94 9
        return $this->executeAuthMethod($this->getConfig('auth.methods.check'));
95
    }
96
97
    /**
98
     * Get the authenticated user.
99
     *
100
     * @return false|mixed
101
     */
102
    protected function user()
103
    {
104
        return $this->executeAuthMethod($this->getConfig('auth.methods.user'));
105
    }
106
107
    /**
108
     * Execute the auth method.
109
     *
110
     * @param  string  $method
111
     *
112
     * @return mixed|false
113
     */
114 9
    private function executeAuthMethod($method)
115
    {
116 9
        foreach ($this->auths as $auth) {
117 9
            if (is_callable([$auth, $method], true)) {
118 9
                if ($data = $auth->{$method}()) return $data;
119
            }
120
        }
121
122 9
        return false;
123
    }
124
}
125