Test Setup Failed
Push — master ( 31e3b8...9c038b )
by Avtandil
07:13
created

MultiLang   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
dl 0
loc 65
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 18 3
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\Routing\Redirector;
16
use Longman\LaravelMultiLang\MultiLang as MultiLangLib;
17
18
class MultiLang
19
{
20
21
    /**
22
     * Application.
23
     *
24
     * @var \Illuminate\Foundation\Application
25
     */
26
    protected $app;
27
28
    /**
29
     * Redirector.
30
     *
31
     * @var \Illuminate\Routing\Redirector
32
     */
33
    protected $redirector;
34
35
    /**
36
     * Multilang.
37
     *
38
     * @var \Longman\LaravelMultiLang\Multilang
39
     */
40
    protected $multilang;
41
42
    /**
43
     * MultiLang constructor.
44
     *
45
     * @param \Illuminate\Foundation\Application  $app
46
     * @param \Illuminate\Routing\Redirector      $redirector
47
     * @param \Longman\LaravelMultiLang\MultiLang $multilang
48
     */
49 4
    public function __construct(Application $app, Redirector $redirector, MultiLangLib $multilang)
50
    {
51 4
        $this->app        = $app;
52 4
        $this->redirector = $redirector;
53 4
        $this->multilang  = $multilang;
54 4
    }
55
56
    /**
57
     * Handle an incoming request.
58
     *
59
     * @param  \Illuminate\Http\Request $request
60
     * @param  \Closure                 $next
61
     * @return mixed
62
     */
63 4
    public function handle($request, Closure $next)
64
    {
65 4
        $url = $this->multilang->getRedirectUrl($request);
66
67 4
        if ($url !== null) {
68 3
            if ($request->wantsJson()) {
69
                return response('Not found', 404);
70
            } else {
71 3
                return $this->redirector->to($url);
72
            }
73
        }
74
75 1
        $locale = $this->multilang->detectLocale($request);
76
77 1
        $this->app->setLocale($locale);
78
79 1
        return $next($request);
80
    }
81
82
}
83