Completed
Pull Request — master (#2433)
by
unknown
02:32
created

helpers.php ➔ app_url()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 4
nop 0
dl 0
loc 26
rs 8.5706
c 0
b 0
f 0
1
<?php
2
3
use Illuminate\Support\MessageBag;
4
5
if (!function_exists('admin_path')) {
6
7
    /**
8
     * Get admin path.
9
     *
10
     * @param string $path
11
     *
12
     * @return string
13
     */
14
    function admin_path($path = '')
15
    {
16
        return ucfirst(config('admin.directory')).($path ? DIRECTORY_SEPARATOR.$path : $path);
17
    }
18
}
19
20
if (!function_exists('admin_url')) {
21
    /**
22
     * Get admin url.
23
     *
24
     * @param string $path
25
     * @param mixed  $parameters
26
     * @param bool   $secure
27
     *
28
     * @return string
29
     */
30
    function admin_url($path = '', $parameters = [], $secure = null)
31
    {
32
        if (\Illuminate\Support\Facades\URL::isValidUrl($path)) {
33
            return $path;
34
        }
35
36
        $secure = $secure ?: (config('admin.https') || config('admin.secure'));
37
38
        return url(admin_base_path($path), $parameters, $secure);
39
    }
40
}
41
42
if (!function_exists('admin_base_path')) {
43
    /**
44
     * Get admin url.
45
     *
46
     * @param string $path
47
     *
48
     * @return string
49
     */
50
    function admin_base_path($path = '')
51
    {
52
        $prefix = '/'.trim(config('admin.route.prefix'), '/');
53
54
        $prefix = ($prefix == '/') ? '' : $prefix;
55
56
        return $prefix.'/'.trim($path, '/');
57
    }
58
59
    function app_url()
60
    {
61
        $app_url = config('app.url');
62
63
        if (!isset($_SERVER['SERVER_PORT'])) {
64
            return $app_url;
65
        }
66
67
        $parsed_url = parse_url($app_url);
68
        if (!isset($parsed_url['port'])
69
            && ($_SERVER['SERVER_PORT'] != ''
70
                && $_SERVER['SERVER_PORT'] != 80
71
                && $_SERVER['SERVER_PORT'] != 443)) {
72
            $add_slash = '/';
73
74
            preg_match('/([^\/:])(\/|$)(.*)/', $app_url, $matches);
75
76
            if ($matches[3] == '') {
77
                $add_slash = '';
78
            }
79
80
            $app_url = preg_replace('/([^\/:])(\/|$)/', '\1:'.$_SERVER['SERVER_PORT'].$add_slash, $app_url, 1);
81
        }
82
83
        return $app_url;
84
    }
85
}
86
87 View Code Duplication
if (!function_exists('admin_toastr')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
89
    /**
90
     * Flash a toastr message bag to session.
91
     *
92
     * @param string $message
93
     * @param string $type
94
     * @param array  $options
95
     */
96
    function admin_toastr($message = '', $type = 'success', $options = [])
97
    {
98
        $toastr = new MessageBag(get_defined_vars());
99
100
        session()->flash('toastr', $toastr);
101
    }
102
}
103
104
if (!function_exists('admin_success')) {
105
106
    /**
107
     * Flash a success message bag to session.
108
     *
109
     * @param string $title
110
     * @param string $message
111
     */
112
    function admin_success($title, $message = '')
113
    {
114
        admin_info($title, $message, 'success');
115
    }
116
}
117
118
if (!function_exists('admin_error')) {
119
120
    /**
121
     * Flash a error message bag to session.
122
     *
123
     * @param string $title
124
     * @param string $message
125
     */
126
    function admin_error($title, $message = '')
127
    {
128
        admin_info($title, $message, 'error');
129
    }
130
}
131
132
if (!function_exists('admin_warning')) {
133
134
    /**
135
     * Flash a warning message bag to session.
136
     *
137
     * @param string $title
138
     * @param string $message
139
     */
140
    function admin_warning($title, $message = '')
141
    {
142
        admin_info($title, $message, 'warning');
143
    }
144
}
145
146 View Code Duplication
if (!function_exists('admin_info')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147
148
    /**
149
     * Flash a message bag to session.
150
     *
151
     * @param string $title
152
     * @param string $message
153
     * @param string $type
154
     */
155
    function admin_info($title, $message = '', $type = 'info')
156
    {
157
        $message = new MessageBag(get_defined_vars());
158
159
        session()->flash($type, $message);
160
    }
161
}
162
163
if (!function_exists('admin_asset')) {
164
165
    /**
166
     * @param $path
167
     *
168
     * @return string
169
     */
170
    function admin_asset($path)
171
    {
172
        return asset($path, (config('admin.https') || config('admin.secure')));
173
    }
174
}
175
176
if (!function_exists('array_delete')) {
177
178
    /**
179
     * Delete from array by value.
180
     *
181
     * @param array $array
182
     * @param mixed $value
183
     */
184
    function array_delete(&$array, $value)
185
    {
186
        foreach ($array as $index => $item) {
187
            if ($value == $item) {
188
                unset($array[$index]);
189
            }
190
        }
191
    }
192
}
193