build_url()   B
last analyzed

Complexity

Conditions 8
Paths 128

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 11
c 1
b 0
f 0
nc 128
nop 3
dl 0
loc 17
rs 8.2111
1
<?php
2
3
if (! function_exists('_get')) {
4
    /**
5
     * Get attribute value from a given object.
6
     *
7
     * @param  object  $object  The object
8
     * @param  array  $attribute  The attribute name must be an array to support nesting [EX: "profile.name"]
9
     * @param  mixed  $default  The fallback value if the attribute is not found
10
     * @return mixed
11
     */
12
    function _get(object $object, array $attribute, $default)
13
    {
14
        if (count($attribute) > 1) {
15
            $object2 = $object->{$attribute[0]} ?? [];
16
            array_shift($attribute);
17
            if (gettype($object2) !== 'object') {
18
                return $default;
19
            }
20
21
            return _get($object2, $attribute, $default);
22
        } else {
23
            return $object->{$attribute[0]} ?? $default;
24
        }
25
    }
26
}
27
28
if (! function_exists('build_url')) {
29
    /**
30
     * Build the url of the gateway from settings for the desired profile.
31
     *
32
     * @param  string  $path  The path
33
     * @param  string  $profile  The profile name
34
     * @param  string  $url  The new url
35
     * @return string
36
     */
37
    function build_url(string $path, string $profile, ?string $url = null): string
38
    {
39
        return (string) (
40
            $url ? (endsWith($url, '/') ? $url : ($path != '' ? $url.'/' : $url)) : config('p-connector.profiles.'.$profile.'.protocol', 'http')
41
            .'://'.config('p-connector.profiles.'.$profile.'.host', 'localhost')
42
            .':'.config('p-connector.profiles.'.$profile.'.port', 80)
43
            .(startsWith(config('p-connector.profiles.'.$profile.'.prefix', ''), '/') ? '' : '/')
44
            .(
45
                endsWith(config('p-connector.profiles.'.$profile.'.prefix', ''), '/') ?
46
                config('p-connector.profiles.'.$profile.'.prefix', '') :
47
                (
48
                    null != config('p-connector.profiles.'.$profile.'.prefix', '') ?
49
                    config('p-connector.profiles.'.$profile.'.prefix', '').'/' :
50
                    ''
51
                )
52
            )
53
        ).(startsWith($path, '/') ? substr($path, 1) : $path);
54
    }
55
}
56
if (! function_exists('startsWith')) {
57
    function startsWith($haystack, $needle)
58
    {
59
        return 0 === substr_compare($haystack, $needle, 0, strlen($needle));
60
    }
61
}
62
if (! function_exists('endsWith')) {
63
    function endsWith($haystack, $needle)
64
    {
65
        return 0 === substr_compare($haystack, $needle, -strlen($needle));
66
    }
67
}
68