Passed
Push — master ( 399fd0...af4f7a )
by Elf
02:32
created

AppIdentifier   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 10.53%

Importance

Changes 0
Metric Value
dl 0
loc 57
ccs 2
cts 19
cp 0.1053
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getIdentifierForUrl() 0 17 5
A get() 0 9 2
A refresh() 0 3 1
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
    public static function get()
18
    {
19
        if (app()->bound(static::IDENTIFIER_KEY)) {
20
            return app()->make(static::IDENTIFIER_KEY);
21
        }
22
23
        return app()->instance(
24
            static::IDENTIFIER_KEY,
25
            static::getIdentifierForUrl(app('request')->getUri())
26
        );
27
    }
28
29
    /**
30
     * Get application identifier for the given URL.
31
     *
32
     * @param  string  $url
33
     * @return string
34
     */
35
    protected static function getIdentifierForUrl($url)
36
    {
37
        $identifier = null;
38
39
        foreach (config('apps.url') as $id => $root) {
40
            $root = preg_replace('#^https?://#', '', $root);
41
            $pattern = '#^https?://'.preg_quote($root, '#').'([\?/].*)?$#';
42
            if (preg_match($pattern, $url)) {
43
                $len = strlen($root);
44
                if (! isset($length) || $length < $len) {
45
                    $length = $len;
46
                    $identifier = $id;
47
                }
48
            }
49
        }
50
51
        return $identifier;
52
    }
53
54
    /**
55
     * Refresh the current application identifier.
56
     *
57
     * @return void
58
     */
59 1
    public static function refresh()
60
    {
61 1
        app()->forgetInstance(static::IDENTIFIER_KEY);
62 1
    }
63
}
64