Passed
Push — master ( 58c7e8...6329ae )
by Vasyl
01:51
created

UrlAliasLocalization::getCurrentBound()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 7
nop 2
dl 0
loc 20
rs 9.5222
c 0
b 0
f 0
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->config->set('url-aliases-laravellocalization.origin_default_locale', $this->defaultLocale); // virtual temporary config
37
38
        $this->currentLocale = $this->defaultLocale;
39
40
        $this->supportedLocales = $this->getSupportedLocales();
41
42
        if (empty($this->supportedLocales[$this->defaultLocale])) {
43
            throw new \Exception('Laravel default locale is not in the supportedLocales array.');
44
        }
45
    }
46
47
    /**
48
     * TODO: remove $segment1
49
     * @param $path
50
     * @param $segment1
51
     * @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...
52
     */
53
    public function prepareLocalizePath(string $path): array
54
    {
55
//        session()->put('locale', $this->defaultLocale);
56
        $segment1 = url_path_segments($path, 1);
57
58
        if (key_exists($segment1, $this->supportedLocales)) {
59
//            session()->put('locale', $segment1);
60
61
            $path = preg_replace("/^$segment1\\//", "", "$path");
62
63
            if ($this->hideDefaultLocaleInURL() && $segment1 === $this->defaultLocale) {
64
                $path = ($path == $segment1) ? '/' : $path;
65
                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...
66
            }
67
68
            $this->currentLocale = $segment1;
69
            $this->app->setLocale($segment1);
70
        }
71
72
        // Regional locale such as de_DE, so formatLocalized works in Carbon
73
        $regional = $this->getCurrentLocaleRegional();
74
        $suffix = $this->config->get('url-aliases.laravellocalization.utf8suffix');
75
        if ($regional) {
76
            setlocale(LC_TIME, $regional . $suffix);
77
            setlocale(LC_MONETARY, $regional . $suffix);
78
        }
79
80
        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...
81
    }
82
83
    /**
84
     * @return bool
85
     */
86
    protected function hideDefaultLocaleInURL()
87
    {
88
        return $this->config->get('url-aliases-laravellocalization.hideDefaultLocaleInURL');
89
    }
90
91
    /**
92
     * @return \Illuminate\Contracts\Routing\UrlGenerator|string
93
     */
94
    public function getRoot()
95
    {
96
        return url($this->currentLocale);
0 ignored issues
show
Bug introduced by
The function url 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

96
        return /** @scrutinizer ignore-call */ url($this->currentLocale);
Loading history...
97
    }
98
99
    /**
100
     * TODO: DEPRECATED!!!
101
     * @param string $default
102
     * @param bool $absolute
103
     * @return array
104
     */
105
    public function getLocalesBound(string $default = '', $absolute = true)
106
    {
107
        return $this->getCurrentBound($default, $absolute);
108
    }
109
    /**
110
     * @param string $default
111
     * @param bool $absolute
112
     * @return array
113
     */
114
    public function getCurrentBound(string $default = '', $absolute = true)
115
    {
116
        $bound = request()->server('ALIAS_LOCALE_BOUND');
0 ignored issues
show
Bug introduced by
The function request 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

116
        $bound = /** @scrutinizer ignore-call */ request()->server('ALIAS_LOCALE_BOUND');
Loading history...
117
118
        $modelClass = $this->config->get('url-aliases.model', \Fomvasss\UrlAliases\Models\UrlAlias::class);
119
        $aliasModels = $modelClass::whereNotNull('locale_bound')->where('locale_bound', $bound)->get();
120
121
        $res = $this->supportedLocales;
122
        foreach ($this->supportedLocales as $key => $item) {
123
            if ($modelClass = $aliasModels->where('locale', $key)->first()) {
124
                $link = $modelClass->localeAlias;
125
            } elseif($default) {
126
                $link = $default;
127
            } else {
128
                $link = $key;
129
            }
130
            $res[$key]['url'] = $absolute ? url($link) : $link;
0 ignored issues
show
Bug introduced by
The function url 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

130
            $res[$key]['url'] = $absolute ? /** @scrutinizer ignore-call */ url($link) : $link;
Loading history...
131
        }
132
133
        return $res;
134
    }
135
136
    /**
137
     * @param null $bound
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $bound is correct as it would always require null to be passed?
Loading history...
138
     * @param bool $absolute
139
     * @return array
140
     */
141
    public function getLocalesModelsBound($bound = null, $absolute = true)
142
    {
143
        $modelClass = $this->config->get('url-aliases.model', \Fomvasss\UrlAliases\Models\UrlAlias::class);
144
        $aliasModels = $modelClass::whereNotNull('locale_bound')->where('locale_bound', $bound)->get();
145
146
        $res = $this->supportedLocales;
147
        foreach ($this->supportedLocales as $key => $item) {
148
            $res[$key]['model'] = null;
149
            if ($model = $aliasModels->where('locale', $key)->first()) {
150
                $link = $model->localeAlias;
151
                $res[$key]['model'] = $model->aliasable;
152
            } else {
153
                $link = $key;
154
            }
155
            $res[$key]['url'] = $absolute ? url($link) : $link;
0 ignored issues
show
Bug introduced by
The function url 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

155
            $res[$key]['url'] = $absolute ? /** @scrutinizer ignore-call */ url($link) : $link;
Loading history...
156
        }
157
158
        return $res;
159
    }
160
}