NegotiateLanguage   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 42
c 0
b 0
f 0
wmc 3
lcom 1
cbo 2
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 11 2
A __construct() 0 4 1
1
<?php
2
3
namespace RichanFongdasen\I18n\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use RichanFongdasen\I18n\I18nService;
8
9
class NegotiateLanguage
10
{
11
    /**
12
     * I18n service instance.
13
     *
14
     * @var I18nService
15
     */
16
    protected $i18n;
17
18
    /**
19
     * NegotiateLanguage constructor.
20
     *
21
     * @param I18nService $i18n
22
     */
23
    public function __construct(I18nService $i18n)
24
    {
25
        $this->i18n = $i18n;
26
    }
27
28
    /**
29
     * Handle an incoming request.
30
     *
31
     * @param \Illuminate\Http\Request $request
32
     * @param \Closure                 $next
33
     *
34
     * @throws \RichanFongdasen\I18n\Exceptions\InvalidLocaleException
35
     * @throws \RichanFongdasen\I18n\Exceptions\InvalidFallbackLanguageException
36
     *
37
     * @return mixed
38
     */
39
    public function handle(Request $request, Closure $next)
40
    {
41
        if ($this->i18n->routedLocale($request) === null) {
42
            $negotiator = $this->i18n->getConfig('negotiator');
43
            $locale = app($negotiator)->preferredLocale($request);
44
45
            return redirect($this->i18n->url($request->fullUrl(), $locale));
46
        }
47
48
        return $next($request);
49
    }
50
}
51