Passed
Push — master ( 26152a...8fe73e )
by F
04:04
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

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\Models\Language;
19
20
class Localisation
21
{
22
    /**
23
     * The Laravel application instance.
24
     *
25
     * @var \Illuminate\Foundation\Application
26
     */
27
    protected $app;
28
29
    /**
30
     * Normalized Laravel Version.
31
     *
32
     * @var string
33
     */
34
    protected $version;
35
36
    /**
37
     * True when this is a Lumen application.
38
     *
39
     * @var boolean
40
     */
41
    protected $lumen = false;
42
43
    /**
44
     * Constructor.
45
     *
46
     * @param Application $app laravel application for further use
0 ignored issues
show
Bug introduced by
The type PWWEB\Localisation\Application was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
47
     */
48
    public function __construct($app = null)
49
    {
50
        if (null === $app) {
51
            $app = app();
52
            //Fallback when $app is not given
53
        }
54
55
        $this->app = $app;
56
        $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

56
        /** @scrutinizer ignore-call */ 
57
        $this->version = $app->version();
Loading history...
57
        $this->lumen = Str::contains($this->version, 'Lumen');
58
    }
59
60
    /**
61
     * Retrieves all active languages.
62
     *
63
     * @return Collection a collection of active languages
64
     */
65
    public static function languages(): Collection
66
    {
67
        return Language::where('active', 1)->get();
68
    }
69
70
    /**
71
     * Determines the currently selected locale.
72
     *
73
     * @param string $locale (Optional) Locale to be used for language retrieval
74
     *
75
     * @return Language A language object
76
     */
77
    public static function currentLanguage(string $locale = ''): Language
78
    {
79
        $fallbackLocale = config('app.fallback_locale');
80
81
        if ('' === $locale) {
82
            $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

82
            $locale = app()->/** @scrutinizer ignore-call */ getLocale();
Loading history...
83
        } else if ($locale === $fallbackLocale) {
84
            $locale = 'en-GB';
85
        } else {
86
            $locale = $fallbackLocale . '-' . strtoupper($fallbackLocale);
87
        }
88
89
        try {
90
            $current = Language::findByLocale($locale);
91
        } catch (\InvalidArgumentException $e) {
92
            $current = self::currentLanguage($fallbackLocale);
93
        }
94
95
        return $current;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $current could return the type PWWEB\Localisation\Contracts\Language which includes types incompatible with the type-hinted return PWWeb\Localisation\Models\Language. Consider adding an additional type-check to rule them out.
Loading history...
96
    }
97
98
    /**
99
     * Renders a language selector / switcher according to view file.
100
     *
101
     * @return Renderable language selector / switcher markup
102
     */
103
    public static function languageSelector(): Renderable
104
    {
105
        $languages = self::languages();
106
        $current = self::currentLanguage();
107
108
        return view('localisation::atoms.languageselector', compact('languages', 'current'));
109
    }
110
}
111