Url   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 71
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A configDomainIsSet() 0 8 2
A extractDomain() 0 5 2
A domain() 0 8 2
A host() 0 3 1
A subdomain() 0 3 1
A domainName() 0 5 1
A hasSubdomain() 0 3 1
1
<?php
2
3
namespace Pmochine\LaravelTongue\Misc;
4
5
use Illuminate\Support\Str;
6
7
class Url
8
{
9
    public static function domain(): string
10
    {
11
        //check if the hosted domain is the same, if not use it from get host
12
        if (self::configDomainIsSet()) {
13
            return Config::domain();
14
        }
15
16
        return self::extractDomain();
17
    }
18
19
    /**
20
     * This is actually not that important. Only if you have
21
     * complicated domains like '155ad73e.eu.ngrok.io', whe I just
22
     * cannot tell what the real domain is.
23
     *
24
     * It is true when e.g.: '155ad73e.eu.ngrok.io' contains in 'yoursubdomain.155ad73e.eu.ngrok.io'
25
     *
26
     * @return bool
27
     */
28
    protected static function configDomainIsSet(): bool
29
    {
30
        if (! $domain = Config::domain()) {
31
            return false;
32
        } // config was not set
33
34
        //the host could have a different domain, thats why we check it here
35
        return Str::contains(self::host(), $domain);
36
    }
37
38
    /**
39
     * Gets the registrable Domain of the website.
40
     *
41
     * https://github.com/jeremykendall/php-domain-parser
42
     *
43
     * @return string
44
     */
45
    protected static function extractDomain(): string
46
    {
47
        $result = (new DomainParser)->resolve(self::host());
48
49
        return $result->registrableDomain()->toString() ?: '';
50
    }
51
52
    public static function domainName(): string
53
    {
54
        $TLD = substr(self::domain(), strrpos(self::domain(), '.'));
55
56
        return Str::replaceLast($TLD, '', self::domain());
57
    }
58
59
    /**
60
     * @return string [like "de.domain.com"]
61
     */
62
    public static function host(): string
63
    {
64
        return request()->getHost();
65
    }
66
67
    /**
68
     * @return string [like "de" or when no subdomain "domain" of "domain.com"]
69
     */
70
    public static function subdomain(): string
71
    {
72
        return explode('.', self::host())[0];
73
    }
74
75
    public static function hasSubdomain(): bool
76
    {
77
        return explode('.', self::domain())[0] !== self::subdomain();
78
    }
79
}
80