Completed
Push — master ( 76efee...bc6801 )
by Avtandil
13s
created

MultiLang::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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 5
    public function __construct(Application $app, Redirector $redirector, MultiLangLib $multilang)
50
    {
51 5
        $this->app = $app;
52 5
        $this->redirector = $redirector;
53 5
        $this->multilang = $multilang;
54 5
    }
55
56
    /**
57
     * Handle an incoming request.
58
     *
59
     * @param  \Illuminate\Http\Request $request
60
     * @param  \Closure $next
61
     * @return mixed
62
     */
63 5
    public function handle($request, Closure $next)
64
    {
65 5
        $url = $this->multilang->getRedirectUrl($request);
66
67 5
        if ($url !== null) {
68 4
            if ($request->expectsJson()) {
69 1
                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