Authentication::executeAuthMethod()   B
last analyzed

Complexity

Conditions 8
Paths 20

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.4444
c 0
b 0
f 0
cc 8
nc 20
nop 1
1
<?php
2
3
namespace PragmaRX\Tracker\Services;
4
5
use Illuminate\Foundation\Application;
6
use PragmaRX\Support\Config as Config;
7
8
class Authentication
9
{
10
    private $config;
11
12
    private $authentication = [];
13
14
    private $app;
15
16
    public function __construct(Config $config, Application $app)
17
    {
18
        $this->app = $app;
19
20
        $this->config = $config;
21
    }
22
23
    public function check()
24
    {
25
        return $this->executeAuthMethod($this->config->get('authenticated_check_method'));
26
    }
27
28
    private function executeAuthMethod($method)
29
    {
30
        $guards = $this->config->get('authentication_guards');
31
        // Make sure authentication_guards at least contains a null value to DRY code
32
        if (empty($guards)) {
33
            $guards[] = null;
34
        }
35
36
        foreach ($this->getAuthentication() as $auth) {
37
            foreach ($guards as $guard) {
38
                // Call guard() if not null
39
                if ($guard && $guard != 'null') {
40
                    $auth = $auth->guard($guard);
41
                }
42
            }
43
            if (is_callable([$auth, $method], true, $callable_name)) {
44
                if ($data = $auth->$method()) {
45
                    return $data;
46
                }
47
            }
48
        }
49
50
        return false;
51
    }
52
53
    private function getAuthentication()
54
    {
55
        foreach ((array) $this->config->get('authentication_ioc_binding') as $binding) {
56
            $this->authentication[] = $this->app->make($binding);
57
        }
58
59
        return $this->authentication;
60
    }
61
62
    public function user()
63
    {
64
        return $this->executeAuthMethod($this->config->get('authenticated_user_method'));
65
    }
66
67
    public function getCurrentUserId()
68
    {
69
        if ($this->check()) {
70
            return $this->user()->{$this->config->get('authenticated_user_id_column')};
71
        }
72
    }
73
}
74