LangRepository::get()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 16
rs 9.9666
1
<?php
2
3
namespace Translation\Repositories;
4
5
use Translation\Models\Language;
6
use Translation\Models\Locale;
7
use Translation\Models\Country;
8
use Translation;
9
10
class LangRepository
11
{
12
13
    /**
14
     * @var string
15
     */
16
    const COOKIENAME = 'language';
17
18
    /**
19
     * @return string
20
     */
21
    public static function getDefaultLocale()
22
    {
23
        return Translation::getAppLocale();
24
    }
25
26
    /**
27
     * @return array
28
     */
29
    public static function getLocale()
30
    {
31
        return config('translation.active_languages', [
32
            'en-GB',
33
            'fr-FR',
34
            'es-CO',
35
            'pt-BR',
36
        ]);
37
    }
38
39
    /**
40
     * @param  mixed  $locale   (optional)
41
     * @param  string $column   (optional)
42
     * @param  string $inLocale (optional)
43
     * @return array
44
     */
45
    public static function get($locale = null, $column = null, $inLocale = false)
46
    {
47
        if (!$inLocale) {
48
            $inLocale = self::getDefaultLocale();
49
        }
50
        $allLocale = self::getLocale();
51
52
        if ($locale) {
53
            $locale = (array) $locale;
54
            foreach ($locale as $value) {
55
                $bestLocale[] = \Locale::lookup($allLocale, $value, false, $inLocale);
56
            }
57
            $allLocale = $bestLocale;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $bestLocale seems to be defined by a foreach iteration on line 54. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
58
        }
59
60
        return self::configure($allLocale, $column, $inLocale);
61
    }
62
63
    /**
64
     * @param array  $locale
65
     * @param string $column
66
     * @param string $inLocale
67
     */
68
    protected static function configure($locale, $column, $inLocale)
69
    {
70
        foreach ($locale as $key) {
71
            $configured[$key] = [
72
                'locale' => $key,
73
                'name' => utf8_decode(\Locale::getDisplayName($key, $inLocale)),
74
                'region' => utf8_decode(\Locale::getDisplayRegion($key, $inLocale)),
75
                'language' => utf8_decode(\Locale::getDisplayLanguage($key, $inLocale)),
76
                'class' => 'flag-icon flag-icon-' . strtolower(\Locale::getRegion($key))
77
            ];
78
        }
79
80
        if ($column) {
81
            return array_column($configured, $column, 'locale');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $configured seems to be defined by a foreach iteration on line 70. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
82
        }
83
84
        return $configured;
85
    }
86
87
    /**
88
     * @return array
89
     */
90
    public static function getCurrent()
91
    {
92
        //        if ($cookieLocale = self::getCookie()) {
93
        //            return current(self::get($cookieLocale));
94
        //        }
95
        //
96
        //        if (!empty(Yii::app()->user->model()->language)) {
97
        //            $userLocale = Yii::app()->user->model()->language;
98
        //            return current(self::get($userLocale));
99
        //        }
100
101
102
        if (session('language')!==null) {
0 ignored issues
show
Bug introduced by
The function session was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

102
        if (/** @scrutinizer ignore-call */ session('language')!==null) {
Loading history...
103
            //Config::set('app.locale', session('language'));
104
            return current(self::get(session('language')));
105
        }
106
107
        if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
108
            $browserLocale = \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
109
            return current(self::get($browserLocale));
110
        }
111
112
        return current(self::get(self::getDefaultLocale()));
113
    }
114
115
    /**
116
     * @param  string $language
117
     * @return bool
118
     */
119
    public static function updateCookie($language)
120
    {
121
        return (!self::getCookie()) ?: self::setCookie($language);
0 ignored issues
show
Bug introduced by
The method setCookie() does not exist on Translation\Repositories\LangRepository. ( Ignorable by Annotation )

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

121
        return (!self::getCookie()) ?: self::/** @scrutinizer ignore-call */ setCookie($language);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
    }
123
124
    /**
125
     * @return bool
126
     */
127
    public static function getCookie()
128
    {
129
        if (empty($_COOKIE[self::COOKIENAME])) {
130
            return false;
131
        }
132
133
        return $_COOKIE[self::COOKIENAME];
134
    }
135
136
}
137