Passed
Push — master ( bb924a...090072 )
by Vasyl
01:56
created

UrlAliasLocalization   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
c 0
b 0
f 0
dl 0
loc 80
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hideDefaultLocaleInURL() 0 3 1
A prepareLocalizePath() 0 28 6
A __construct() 0 17 3
1
<?php
2
3
namespace Fomvasss\UrlAliases;
4
5
use Fomvasss\UrlAliases\Traits\Localization;
6
7
class UrlAliasLocalization
8
{
9
    use Localization;
10
11
    protected $app;
12
    
13
    protected $config;
14
    
15
    protected $defaultLocale;
16
17
    protected $supportedLocales;
18
19
    protected $currentLocale;
20
21
    /**
22
     * Localization constructor.
23
     * @param $app
24
     */
25
    public function __construct($app)
26
    {
27
        if (!$app) {
28
            $app = app();   //Fallback when $app is not given
0 ignored issues
show
Bug introduced by
The function app 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

28
            $app = /** @scrutinizer ignore-call */ app();   //Fallback when $app is not given
Loading history...
29
        }
30
        $this->app = $app;
31
32
        $this->config = $this->app['config'];
33
34
        $this->defaultLocale = $this->getDefaultLocale();
35
36
        $this->currentLocale = $this->defaultLocale;
37
38
        $this->supportedLocales = $this->getSupportedLocales();
39
40
        if (empty($this->supportedLocales[$this->defaultLocale])) {
41
            throw new \Exception('Laravel default locale is not in the supportedLocales array.');
42
        }
43
    }
44
45
    /**
46
     * TODO: remove $segment1
47
     * @param $path
48
     * @param $segment1
49
     * @return \Illuminate\Http\RedirectResponse|string
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\RedirectResponse 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...
50
     */
51
    public function prepareLocalizePath(string $path): array
52
    {
53
//        session()->put('locale', $this->defaultLocale);
54
        $segment1 = url_path_segments($path, 1);
55
56
        if (key_exists($segment1, $this->supportedLocales)) {
57
//            session()->put('locale', $segment1);
58
59
            $path = preg_replace("/^$segment1\\//", "", "$path");
60
61
            if ($this->hideDefaultLocaleInURL() && $segment1 === $this->defaultLocale) {
62
                $path = ($path == $segment1) ? '/' : $path;
63
                return ['redirect' => $path];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('redirect' => $path) returns the type array<string,string> which is incompatible with the documented return type Illuminate\Http\RedirectResponse|string.
Loading history...
64
            }
65
66
            $this->currentLocale = $segment1;
67
            $this->app->setLocale($segment1);
68
        }
69
70
        // Regional locale such as de_DE, so formatLocalized works in Carbon
71
        $regional = $this->getCurrentLocaleRegional();
72
        $suffix = $this->config->get('url-aliases.laravellocalization.utf8suffix');
73
        if ($regional) {
74
            setlocale(LC_TIME, $regional . $suffix);
75
            setlocale(LC_MONETARY, $regional . $suffix);
76
        }
77
78
        return ['path' => $path];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('path' => $path) returns the type array<string,string> which is incompatible with the documented return type Illuminate\Http\RedirectResponse|string.
Loading history...
79
    }
80
81
    /**
82
     * @return bool
83
     */
84
    protected function hideDefaultLocaleInURL()
85
    {
86
        return $this->config->get('url-aliases-laravellocalization.hideDefaultLocaleInURL');
87
    }
88
}