Completed
Push — master ( d310f6...f43027 )
by Antonio Carlos
04:22 queued 02:58
created

Authentication::executeAuthMethod()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.5866
c 0
b 0
f 0
cc 7
nc 16
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 ($guards != '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
51
        return false;
52
    }
53
54
    private function getAuthentication()
55
    {
56
        foreach ((array) $this->config->get('authentication_ioc_binding') as $binding) {
57
            $this->authentication[] = $this->app->make($binding);
58
        }
59
60
        return $this->authentication;
61
    }
62
63
    public function user()
64
    {
65
        return $this->executeAuthMethod($this->config->get('authenticated_user_method'));
66
    }
67
68
    public function getCurrentUserId()
69
    {
70
        if ($this->check()) {
71
            return $this->user()->{$this->config->get('authenticated_user_id_column')};
72
        }
73
    }
74
}
75