Completed
Pull Request — master (#297)
by
unknown
02:12
created

helpers.php ➔ backpack_view()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 2
dl 0
loc 15
rs 9.7666
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_authentication_column')) {
21
    /**
22
     * Return the username column name.
23
     * The Laravel default (and Backpack default) is 'email'.
24
     *
25
     * @return string
26
     */
27
    function backpack_authentication_column()
28
    {
29
        return config('backpack.base.authentication_column', 'email');
30
    }
31
}
32
33
if (!function_exists('backpack_users_have_email')) {
34
    /**
35
     * Check if the email column is present on the user table.
36
     *
37
     * @return string
38
     */
39
    function backpack_users_have_email()
40
    {
41
        $user_model_fqn = config('backpack.base.user_model_fqn');
42
        $user = new $user_model_fqn();
43
44
        return \Schema::hasColumn($user->getTable(), 'email');
45
    }
46
}
47
48
if (!function_exists('backpack_avatar_url')) {
49
    /**
50
     * Returns the avatar URL of a user.
51
     *
52
     * @param $user
53
     *
54
     * @return string
55
     */
56
    function backpack_avatar_url($user)
57
    {
58
        $placeholder = 'https://placehold.it/160x160/00a65a/ffffff/&text='.$user->name[0];
59
60
        switch (config('backpack.base.avatar_type')) {
61
            case 'gravatar':
62
                if (backpack_users_have_email()) {
63
                    return Gravatar::fallback('https://placehold.it/160x160/00a65a/ffffff/&text='.$user->name[0])->get($user->email);
64
                } else {
65
                    return $placeholder;
66
                }
67
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
68
69
            case 'placehold':
70
                return $placeholder;
71
                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...
72
73
            default:
74
                return method_exists($user, config('backpack.base.avatar_type')) ? $user->{config('backpack.base.avatar_type')}() : $user->{config('backpack.base.avatar_type')};
75
                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...
76
        }
77
    }
78
}
79
80
if (!function_exists('backpack_middleware')) {
81
    /**
82
     * Return the key of the middleware used across Backpack.
83
     * That middleware checks if the visitor is an admin.
84
     *
85
     * @param $path
86
     *
87
     * @return string
88
     */
89
    function backpack_middleware()
90
    {
91
        return config('backpack.base.middleware_key', 'admin');
92
    }
93
}
94
95
if (!function_exists('backpack_guard_name')) {
96
    /*
97
     * Returns the name of the guard defined
98
     * by the application config
99
     */
100
    function backpack_guard_name()
101
    {
102
        return config('backpack.base.guard', config('auth.defaults.guard'));
103
    }
104
}
105
106
if (!function_exists('backpack_auth')) {
107
    /*
108
     * Returns the user instance if it exists
109
     * of the currently authenticated admin
110
     * based off the defined guard.
111
     */
112
    function backpack_auth()
113
    {
114
        return \Auth::guard(backpack_guard_name());
115
    }
116
}
117
118
if (!function_exists('backpack_user')) {
119
    /*
120
     * Returns back a user instance without
121
     * the admin guard, however allows you
122
     * to pass in a custom guard if you like.
123
     */
124
    function backpack_user()
125
    {
126
        return backpack_auth()->user();
127
    }
128
}
129
130
if (!function_exists('backpack_view')) {
131
    /**
132
     * Returns a new displayable view based on the current configured backpack
133
     *
134
     * @param string (see config/backpack/base.php)
135
     *
136
     * @return string
137
     */
138
    function backpack_view($view, $package = 'backpack')
139
    {
140
        $theme = config('backpack.base.theme');
141
142
        if(is_null($theme)) {
143
          $theme = 'adminlte';
144
        }
145
146
        $returnView = "{$package}::{$theme}.{$view}";
147
        if(!view()->exists($returnView)){
0 ignored issues
show
Bug introduced by
The method exists does only exist in Illuminate\Contracts\View\Factory, but not in Illuminate\View\View.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
148
            $returnView = "{$package}::{$view}";
149
        }
150
151
        return $returnView;
152
    }
153
}
154
155
if (!function_exists('backpack_theme')) {
156
    /**
157
     * Set the path of an extended/included view based on the configured backpack
158
     *
159
     * @param string (see config/backpack/base.php)
160
     *
161
     * @return string
162
     */
163
    function backpack_theme($view, $package = 'backpack')
164
    {
165
        $theme = config('backpack.base.theme');
166
167
        if(is_null($theme)) {
168
          $theme = 'adminlte';
169
        }
170
171
        return "{$package}::{$theme}.{$view}";
172
    }
173
}
174