Passed
Push — master ( 37c75f...5dc35c )
by F
02:52
created

Localisation   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 89
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A languageSelector() 0 6 1
A languages() 0 3 1
A __construct() 0 10 2
A currentLanguage() 0 19 4
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 as LanguageContract;
19
use PWWEB\Localisation\Models\Language;
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();
0 ignored issues
show
introduced by
The method version() 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

57
        /** @scrutinizer ignore-call */ 
58
        $this->version = $app->version();
Loading history...
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();
0 ignored issues
show
introduced by
The method getLocale() 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

83
            $locale = app()->/** @scrutinizer ignore-call */ getLocale();
Loading history...
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