Completed
Push — master ( 02f92d...9c9507 )
by Sebastian
15:38
created

CurrentLocale::determine()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 15
rs 9.2
cc 4
eloc 7
nc 4
nop 0
1
<?php
2
3
namespace App\Services\Locale;
4
5
use App\Services\Navigation\Section;
6
7
class CurrentLocale
8
{
9
    public static function determine() : string
10
    {
11
        $default = app()->getLocale();
12
13
        if (app(Section::class)->isBack()) {
14
            // User might not be set yet if called in a service provider so a fallback is provided
15
            if (auth()->check()) {
0 ignored issues
show
Bug introduced by
The method check() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

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...
16
                return app()->auth->user()->locale;
17
            }
18
19
            return $default;
20
        }
21
22
        return static::isValidLocale(app()->request->segment(1)) ? app()->request->segment(1) : $default;
23
    }
24
25
    public static function getContentLocale() : string
26
    {
27
        if (! static::isValidLocale(locale())) {
28
            return config('app.locales')[0];
29
        }
30
31
        return locale();
32
    }
33
34
    protected static function isValidLocale($locale) : bool
35
    {
36
        if (! is_string($locale)) {
37
            return false;
38
        }
39
40
        $locales = config('app.locales');
41
42
        return in_array($locale, $locales);
43
    }
44
45
    protected static function isValidBackLocale(string $locale) : bool
46
    {
47
        $backLocales = config('app.backLocales');
48
49
        return in_array($locale, $backLocales);
50
    }
51
}
52