Completed
Push — master ( 4e8d1d...8d6f18 )
by Philipp
03:00
created

Localization::domain()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Pmochine\LaravelTongue\Localization;
4
5
use Illuminate\Support\Arr;
6
use Pmochine\LaravelTongue\Misc\Config;
7
use Pmochine\LaravelTongue\Misc\Cookie;
8
use Pmochine\LaravelTongue\Misc\Url;
9
10
class Localization
11
{
12
13
    /**
14
     * The goal is to find the language (the locale).
15
     * We look at the subdomain or in the cookies or browser language.
16
     *
17
     * @return string [Finds the locale of the url]
18
     */
19
    public static function decipherTongue()
20
    {
21
        $locale = self::fromUrl();
22
23
        // there is no subdomain found
24
        if ($locale === false) {
25
            // this could be a future bug
26
            // when no middleware is active the language is not set right
27
            // domain.com could be in german etc...
28
            if (!Config::beautify()) {
29
                // if the middleware is active we should be redirected to en.domain.com
30
                // if not the fallback language is going to be used
31
                return Config::fallbackLocale();
32
            }
33
            // we are checking if we have languages set in cookies or in the browser
34
            return self::currentTongue();
35
        }
36
37
        // could be a custom subdomain
38
        if (!tongue()->isSpeaking($locale)) {
39
            // check if it is a white listed domain
40
            if (tongue()->speaking('subdomains', $locale)) {
41
                return self::currentTongue();
42
            }
43
            // check if we have a custom locale subdomain, if not it returns a null
44
            $locale = tongue()->speaking('aliases', $locale);
45
        }
46
47
        return $locale;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $locale also could return the type array which is incompatible with the documented return type string.
Loading history...
48
    }
49
50
    /**
51
     * Gets the locale from the url.
52
     * @return string|bool false [when hostname===locale]
53
     */
54
    public static function fromUrl()
55
    {
56
        return  Url::hasSubdomain() ? Url::subdomain() : false;
57
    }
58
59
    /**
60
     * Tries to get the current locale of the user.
61
     * Via the Browser or the fallback language.
62
     *
63
     * @return string
64
     */
65
    protected static function currentTongue(): string
66
    {
67
        if (Config::cookieLocalization() && $locale = self::cookie()) {
68
            return $locale;
69
        }
70
71
        if (Config::acceptLanguage() && self::languageIsSet()) {
72
            $detector = new TongueDetector(Config::fallbackLocale(), Config::supportedLocales(), request());
73
74
            return $detector->negotiateLanguage();
75
        }
76
77
        return Config::fallbackLocale();
78
    }
79
80
    /**
81
     * Only for testing the TongueDetector.
82
     *
83
     * @return bool
84
     */
85
    protected static function languageIsSet(): bool
86
    {
87
        return !app()->runningInConsole() || Arr::has(request()->server(), 'HTTP_ACCEPT_LANGUAGE');
0 ignored issues
show
introduced by
The method runningInConsole() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
        return !app()->/** @scrutinizer ignore-call */ runningInConsole() || Arr::has(request()->server(), 'HTTP_ACCEPT_LANGUAGE');
Loading history...
Bug introduced by
It seems like request()->server() can also be of type string; however, parameter $array of Illuminate\Support\Arr::has() does only seem to accept ArrayAccess|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
        return !app()->runningInConsole() || Arr::has(/** @scrutinizer ignore-type */ request()->server(), 'HTTP_ACCEPT_LANGUAGE');
Loading history...
88
    }
89
90
    /**
91
     * Sets or gets the cookie of the locale.
92
     * Important to set config/session domain to .exmaple.com
93
     * https://gistlog.co/JacobBennett/15558410de2a394373ac.
94
     *
95
     * @param  string $locale
96
     * @return string|null
97
     */
98
    public static function cookie($locale = null)
99
    {
100
        $cookie = new Cookie('tongue-locale'); //Name of the cookie
101
102
        if ($locale !== null) {
103
            $cookie->save($locale);
104
            return;
105
        }
106
107
        return $cookie->get();
108
    }
109
}
110