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

Authentication   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 0
dl 0
loc 67
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A check() 0 4 1
B executeAuthMethod() 0 25 7
A getAuthentication() 0 8 2
A user() 0 4 1
A getCurrentUserId() 0 6 2
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