Completed
Pull Request — master (#420)
by
unknown
08:25
created

UserResolver   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 40
ccs 11
cts 12
cp 0.9167
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 3 2
A resolveSingle() 0 3 2
A resolveMorphable() 0 15 4
1
<?php
2
/**
3
 * This file is part of the Laravel Auditing package.
4
 *
5
 * @author     Antério Vieira <[email protected]>
6
 * @author     Quetzy Garcia  <[email protected]>
7
 * @author     Raphael França <[email protected]>
8
 * @copyright  2015-2018
9
 *
10
 * For the full copyright and license information,
11
 * please view the LICENSE.md file that was distributed
12
 * with this source code.
13
 */
14
15
namespace OwenIt\Auditing\Resolvers;
16
17
use Illuminate\Support\Facades\Auth;
18
19
class UserResolver implements \OwenIt\Auditing\Contracts\UserResolver
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24 222
    public static function resolve()
25
    {
26 222
        return \Config::get('audit.morphable', false) ? static::resolveMorphable() : static::resolveSingle();
27
    }
28
29
    /**
30
     * Resolves the user when not morphable
31
     *
32
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
33
     */
34 141
    private static function resolveSingle()
35
    {
36 141
        return Auth::check() ? Auth::user() : null;
37
    }
38
39
    /**
40
     * Resolves the user when morphable
41
     *
42
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
43
     */
44 81
    private static function resolveMorphable()
45
    {
46 81
        $guards = \Config::get('audit.guards', ['web']);
47
48 81
        foreach ($guards as $guard) {
49 81
            if (Auth::guard($guard)->check()) {
50 81
                return Auth::guard($guard)->user();
51
            }
52
        }
53
54 81
        if (Auth::check()) {
55
            return Auth::user();
56
        }
57
58 81
        return null;
59
    }
60
}
61