Completed
Push — master ( 9792a6...aaaf86 )
by recca
10:22
created

AuthPanel::identifier()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
nc 5
nop 1
dl 0
loc 22
rs 8.9457
c 0
b 0
f 0
ccs 13
cts 13
cp 1
crap 6
1
<?php
2
3
namespace Recca0120\LaravelTracy\Panels;
4
5
use Closure;
6
use Illuminate\Support\Arr;
7
use Recca0120\LaravelTracy\Contracts\IAjaxPanel;
8
9
class AuthPanel extends AbstractPanel implements IAjaxPanel
10
{
11
    /**
12
     * The user resolver callable.
13
     *
14
     * @var callable|null
15
     */
16
    protected $userResolver = null;
17
18
    /**
19
     * setUserResolver.
20
     *
21
     * @param \Closure $userResolver
22
     * @return $this
23
     */
24 3
    public function setUserResolver(Closure $userResolver)
25
    {
26 3
        $this->userResolver = $userResolver;
27
28 3
        return $this;
29
    }
30
31
    /**
32
     * getAttributes.
33
     *
34
     ** @return array
35
     */
36 5
    protected function getAttributes()
37
    {
38 5
        $attributes = [];
39 5
        if (is_null($this->userResolver) === false) {
40 3
            $attributes['rows'] = call_user_func($this->userResolver);
41 2
        } elseif ($this->hasLaravel() === true) {
42 2
            $attributes = isset($this->laravel['sentinel']) === true ?
43 2
                $this->fromSentinel() : $this->fromGuard();
44
        }
45
46 5
        return $this->identifier($attributes);
47
    }
48
49
    /**
50
     * fromGuard.
51
     *
52
     * @return array
53
     */
54 1
    protected function fromGuard()
55
    {
56 1
        $auth = $this->laravel['auth'];
57 1
        $user = $auth->user();
58
59 1
        return is_null($user) === true ? [] : [
60 1
            'id' => $user->getAuthIdentifier(),
61 1
            'rows' => $user->toArray(),
62
        ];
63
    }
64
65
    /**
66
     * fromSentinel.
67
     *
68
     * @return array
69
     */
70 1
    protected function fromSentinel()
71
    {
72 1
        $user = $this->laravel['sentinel']->check();
73
74 1
        return empty($user) === true ? [] : [
75 1
            'id' => null,
76 1
            'rows' => $user->toArray(),
77
        ];
78
    }
79
80
    /**
81
     * identifier.
82
     *
83
     * @param array $attributes
84
     * @return array
85
     */
86 5
    protected function identifier($attributes = [])
87
    {
88 5
        $id = Arr::get($attributes, 'id');
89 5
        $rows = Arr::get($attributes, 'rows', []);
90
91 5
        if (empty($rows) === true) {
92 1
            $id = 'Guest';
93 4
        } elseif (is_numeric($id) === true || empty($id) === true) {
94 4
            $id = 'UnKnown';
95 4
            foreach (['username', 'account', 'email', 'name', 'id'] as $key) {
96 4
                if (isset($rows[$key]) === true) {
97 4
                    $id = $rows[$key];
98 4
                    break;
99
                }
100
            }
101
        }
102
103
        return [
104 5
            'id' => $id,
105 5
            'rows' => $rows,
106
        ];
107
    }
108
}
109