Passed
Push — master ( 404fbc...b6aed6 )
by Elf
02:17
created

AppIdentifier::getForUrl()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 17
ccs 11
cts 11
cp 1
crap 5
rs 8.8571
1
<?php
2
3
namespace ElfSundae\Laravel\Apps;
4
5
class AppIdentifier
6
{
7
    /**
8
     * The key registered in the container for the application identifier.
9
     */
10
    const IDENTIFIER_KEY = 'apps.identifier';
11
12
    /**
13
     * Get the current application identifier.
14
     *
15
     * @return string
16
     */
17 3
    public static function get()
18
    {
19 3
        if (app()->bound(static::IDENTIFIER_KEY)) {
20 2
            return app()->make(static::IDENTIFIER_KEY);
21
        }
22
23 1
        return app()->instance(
24 1
            static::IDENTIFIER_KEY,
25 1
            static::getForUrl(app('request')->getUri())
26
        );
27
    }
28
29
    /**
30
     * Refresh the current application identifier.
31
     *
32
     * @return void
33
     */
34 1
    public static function refresh()
35
    {
36 1
        app()->forgetInstance(static::IDENTIFIER_KEY);
37 1
    }
38
39
    /**
40
     * Get application identifier for the given URL.
41
     *
42
     * @param  string  $url
43
     * @return string
44
     */
45 1
    protected static function getForUrl($url)
46
    {
47 1
        $identifier = null;
48
49 1
        foreach (config('apps.url') as $id => $root) {
50 1
            $root = preg_replace('#^https?://#', '', $root);
51 1
            $pattern = '#^https?://'.preg_quote($root, '#').'([\?/].*)?$#';
52 1
            if (preg_match($pattern, $url)) {
53 1
                $len = strlen($root);
54 1
                if (! isset($length) || $length < $len) {
55 1
                    $length = $len;
56 1
                    $identifier = $id;
57
                }
58
            }
59
        }
60
61 1
        return $identifier;
62
    }
63
}
64