Completed
Push — master ( 7d660e...c11331 )
by Avtandil
02:52
created

MultiLang   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 3 Features 1
Metric Value
wmc 3
c 5
b 3
f 1
lcom 1
cbo 3
dl 0
loc 55
ccs 12
cts 12
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 14 2
1
<?php
2
/*
3
 * This file is part of the Laravel MultiLang package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\LaravelMultiLang\Middleware;
12
13
use Closure;
14
use Illuminate\Foundation\Application;
15
use Illuminate\Http\Request;
16
use Illuminate\Routing\Redirector;
17
use Longman\LaravelMultiLang\MultiLang as MultiLangLib;
18
19
class MultiLang
20
{
21
22
    /**
23
     * Application.
24
     *
25
     * @var \Illuminate\Foundation\Application
26
     */
27
    protected $app;
28
29
    /**
30
     * Redirector.
31
     *
32
     * @var \Illuminate\Routing\Redirector
33
     */
34
    protected $redirector;
35
36
    /**
37
     * Multilang.
38
     *
39
     * @var \Longman\LaravelMultiLang\Multilang
40
     */
41
    protected $multilang;
42
43
44 4
    public function __construct(Application $app, Redirector $redirector, MultiLangLib $multilang)
45
    {
46 4
        $this->app        = $app;
47 4
        $this->redirector = $redirector;
48 4
        $this->multilang  = $multilang;
49 4
    }
50
51
    /**
52
     * Handle an incoming request.
53
     *
54
     * @param  \Illuminate\Http\Request $request
55
     * @param  \Closure                 $next
56
     * @return mixed
57
     */
58 4
    public function handle($request, Closure $next)
59
    {
60 4
        $url = $this->multilang->getRedirectUrl($request);
61
62 4
        if ($url !== null) {
63 3
            return $this->redirector->to($url);
64
        }
65
66 1
        $locale = $this->multilang->detectLocale($request);
67
68 1
        $this->app->setLocale($locale);
69
70 1
        return $next($request);
71
    }
72
73
}
74