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

helpers.php ➔ app_url()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 4
nop 0
dl 0
loc 27
rs 8.5546
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
73
            $add_slash = '/';
74
75
            preg_match('/([^\/:])(\/|$)(.*)/', $app_url, $matches);
76
77
            if ($matches[3] == "") {
78
                $add_slash = '';
79
            }
80
81
            $app_url = preg_replace('/([^\/:])(\/|$)/', '\1:'.$_SERVER['SERVER_PORT'].$add_slash, $app_url, 1);
82
        }
83
84
        return $app_url;
85
    }
86
}
87
88 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...
89
90
    /**
91
     * Flash a toastr message bag to session.
92
     *
93
     * @param string $message
94
     * @param string $type
95
     * @param array  $options
96
     */
97
    function admin_toastr($message = '', $type = 'success', $options = [])
98
    {
99
        $toastr = new MessageBag(get_defined_vars());
100
101
        session()->flash('toastr', $toastr);
102
    }
103
}
104
105
if (!function_exists('admin_success')) {
106
107
    /**
108
     * Flash a success message bag to session.
109
     *
110
     * @param string $title
111
     * @param string $message
112
     */
113
    function admin_success($title, $message = '')
114
    {
115
        admin_info($title, $message, 'success');
116
    }
117
}
118
119
if (!function_exists('admin_error')) {
120
121
    /**
122
     * Flash a error message bag to session.
123
     *
124
     * @param string $title
125
     * @param string $message
126
     */
127
    function admin_error($title, $message = '')
128
    {
129
        admin_info($title, $message, 'error');
130
    }
131
}
132
133
if (!function_exists('admin_warning')) {
134
135
    /**
136
     * Flash a warning message bag to session.
137
     *
138
     * @param string $title
139
     * @param string $message
140
     */
141
    function admin_warning($title, $message = '')
142
    {
143
        admin_info($title, $message, 'warning');
144
    }
145
}
146
147 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...
148
149
    /**
150
     * Flash a message bag to session.
151
     *
152
     * @param string $title
153
     * @param string $message
154
     * @param string $type
155
     */
156
    function admin_info($title, $message = '', $type = 'info')
157
    {
158
        $message = new MessageBag(get_defined_vars());
159
160
        session()->flash($type, $message);
161
    }
162
}
163
164
if (!function_exists('admin_asset')) {
165
166
    /**
167
     * @param $path
168
     *
169
     * @return string
170
     */
171
    function admin_asset($path)
172
    {
173
        return asset($path, (config('admin.https') || config('admin.secure')));
174
    }
175
}
176
177
if (!function_exists('array_delete')) {
178
179
    /**
180
     * Delete from array by value.
181
     *
182
     * @param array $array
183
     * @param mixed $value
184
     */
185
    function array_delete(&$array, $value)
186
    {
187
        foreach ($array as $index => $item) {
188
            if ($value == $item) {
189
                unset($array[$index]);
190
            }
191
        }
192
    }
193
}
194