Passed
Push — master ( 8fe73e...cbadeb )
by F
03:17
created

Localisation   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 89
rs 10
c 0
b 0
f 0
wmc 8
1
<?php
2
3
/**
4
 * PWWEB\Localisation.
5
 *
6
 * Localisation Master Class.
7
 *
8
 * @author    Frank Pillukeit <[email protected]>
9
 * @copyright 2020 pw-websolutions.com
10
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
11
 */
12
13
namespace PWWEB\Localisation;
14
15
use Illuminate\Contracts\Support\Renderable;
16
use Illuminate\Database\Eloquent\Collection;
17
use Illuminate\Support\Str;
18
use PWWEB\Localisation\Contracts\Language;
19
use PWWEB\Localisation\Models\Language;
0 ignored issues
show
Bug introduced by
A parse error occurred: Cannot use PWWEB\Localisation\Models\Language as Language because the name is already in use
Loading history...
20
21
class Localisation
22
{
23
    /**
24
     * The Laravel application instance.
25
     *
26
     * @var \Illuminate\Foundation\Application
27
     */
28
    protected $app;
29
30
    /**
31
     * Normalized Laravel Version.
32
     *
33
     * @var string
34
     */
35
    protected $version;
36
37
    /**
38
     * True when this is a Lumen application.
39
     *
40
     * @var boolean
41
     */
42
    protected $lumen = false;
43
44
    /**
45
     * Constructor.
46
     *
47
     * @param \Illuminate\Foundation\Application $app laravel application for further use
48
     */
49
    public function __construct($app = null)
50
    {
51
        if (null === $app) {
52
            $app = app();
53
            //Fallback when $app is not given
54
        }
55
56
        $this->app = $app;
57
        $this->version = $app->version();
58
        $this->lumen = Str::contains($this->version, 'Lumen');
59
    }
60
61
    /**
62
     * Retrieves all active languages.
63
     *
64
     * @return \Illuminate\Database\Eloquent\Collection a collection of active languages
65
     */
66
    public static function languages(): Collection
67
    {
68
        return Language::where('active', 1)->get();
69
    }
70
71
    /**
72
     * Determines the currently selected locale.
73
     *
74
     * @param string $locale (Optional) Locale to be used for language retrieval
75
     *
76
     * @return \PWWEB\Localisation\Contracts\Language A language object
77
     */
78
    public static function currentLanguage(string $locale = ''): LanguageContract
79
    {
80
        $fallbackLocale = config('app.fallback_locale');
81
82
        if ('' === $locale) {
83
            $locale = app()->getLocale();
84
        } elseif ($locale === $fallbackLocale) {
85
            $locale = 'en-GB';
86
        } else {
87
            $locale = $fallbackLocale.'-'.strtoupper($fallbackLocale);
88
        }
89
90
        try {
91
            $current = Language::findByLocale($locale);
92
        } catch (\InvalidArgumentException $e) {
93
            $current = self::currentLanguage($fallbackLocale);
94
        }
95
96
        return $current;
97
    }
98
99
    /**
100
     * Renders a language selector / switcher according to view file.
101
     *
102
     * @return \Illuminate\Contracts\Support\Renderable language selector / switcher markup
103
     */
104
    public static function languageSelector(): Renderable
105
    {
106
        $languages = self::languages();
107
        $current = self::currentLanguage();
108
109
        return view('localisation::atoms.languageselector', compact('languages', 'current'));
110
    }
111
}
112