Completed
Pull Request — master (#87)
by Cristian
08:06
created

helpers.php ➔ backpack_user()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
if (!function_exists('backpack_url')) {
4
    /**
5
     * Appends the configured backpack prefix and returns
6
     * the URL using the standard Laravel helpers.
7
     *
8
     * @param $path
9
     *
10
     * @return string
11
     */
12
    function backpack_url($path = null)
13
    {
14
        $path = !$path || (substr($path, 1, 1) == '/') ? $path : '/'.$path;
15
16
        return url(config('backpack.base.route_prefix', 'admin').$path);
17
    }
18
}
19
20
if (!function_exists('backpack_avatar')) {
21
    /**
22
     * Returns the avatar URL of a user.
23
     *
24
     * @param $user
25
     *
26
     * @return string
27
     */
28
    function backpack_avatar_url($user)
29
    {
30
        switch (config('backpack.base.avatar_type')) {
31
            case 'gravatar':
32
                return Gravatar::fallback('https://placehold.it/160x160/00a65a/ffffff/&text='.$user->name[0])->get($user->email);
33
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
34
35
            case 'placehold':
36
                return 'https://placehold.it/160x160/00a65a/ffffff/&text='.$user->name[0];
37
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
38
39
            default:
40
                return $user->{config('backpack.base.avatar_type')};
41
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
42
        }
43
    }
44
}
45
46
/*
47
 * Returns the name of the middleware
48
 * defined by the application config
49
 * if a param is passed in, it will chain
50
 * the backpack middelware to it.
51
 * e.g guest:backpack.admin.
52
 */
53
if (!function_exists('backpack_middleware')) {
54
    function backpack_middleware($chainedWith = null)
55
    {
56
        if (config('backpack.base.separate_admin_session')) {
57
            $middleware = config('backpack.base.admin_guard.name');
58
        } else {
59
            $middleware = 'backpack.auth';
60
        }
61
62
        if ($chainedWith && config('backpack.base.separate_admin_session')) {
63
            $middleware = $chainedWith.':'.$middleware;
64
        } elseif ($chainedWith) {
65
            $middleware = $chainedWith;
66
        }
67
68
        return $middleware;
69
    }
70
}
71
72
/*
73
 * Returns the name of the guard defined
74
 * by the application config
75
 */
76
if (!function_exists('backpack_guard')) {
77
    function backpack_guard()
78
    {
79
        if (config('backpack.base.separate_admin_session')) {
80
            $guard = config('backpack.base.admin_guard.name');
81
        } else {
82
            $guard = null;
83
        }
84
85
        return $guard;
86
    }
87
}
88
89
/*
90
 * Returns the user instance if it exists
91
 * of the currently authenticated admin
92
 * based off the defined guard.
93
 */
94
if (!function_exists('backpack_auth')) {
95
    function backpack_auth()
96
    {
97
        return \Auth::guard(backpack_guard())->user();
98
    }
99
}
100
101
/*
102
 * Returns back a user instance without
103
 * the admin guard, however allows you
104
 * to pass in a custom guard if you like.
105
 */
106
if (!function_exists('backpack_user')) {
107
    function backpack_user($guard = null)
108
    {
109
        return \Auth::guard($guard)->user();
110
    }
111
}
112