Passed
Push — master ( f23efe...b78b4c )
by Elf
03:51
created

app_url()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 7
nc 12
nop 3
dl 0
loc 15
ccs 7
cts 7
cp 1
crap 6
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
if (! function_exists('is_app')) {
4
    /**
5
     * Check the current application identifier.
6
     *
7
     * @param  string  $identifiers
8
     * @return bool
9
     */
10
    function is_app(...$identifiers)
11
    {
12 1
        $currentUrl = app('request')->getUri();
13
14 1
        foreach ($identifiers as $identifier) {
15 1
            if ($url = config("apps.url.$identifier")) {
16 1
                $url = preg_replace('#^https?://#', '', $url);
17 1
                $pattern = '#^https?://'.preg_quote($url, '#').'([\?/].*)?$#';
18 1
                if (preg_match($pattern, $currentUrl)) {
19 1
                    return true;
20
                }
21
            }
22
        }
23
24 1
        return false;
25
    }
26
}
27
28
if (! function_exists('app_url')) {
29
    /**
30
     * Generate an absolute URL to the given path.
31
     *
32
     * @param  string  $path
33
     * @param  mixed  $query
34
     * @param  mixed  $identifier
35
     * @return string
36
     */
37
    function app_url($path = '', $query = [], $identifier = '')
38
    {
39 1
        if (is_string($query)) {
40 1
            list($query, $identifier) = [$identifier, $query];
41
        }
42
43 1
        if ($path = ltrim($path, '/')) {
44 1
            $path = '/'.$path;
45
        }
46
47 1
        if ($query && $query = http_build_query($query)) {
48 1
            $path .= (str_contains($path, ['?', '&', '#']) ? '&' : '?').$query;
49
        }
50
51 1
        return config("apps.url.$identifier", config('app.url')).$path;
52
    }
53
}
54