Completed
Push — master ( dc71a6...20bb8b )
by Avtandil
05:18
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 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 1
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
            if ($request->wantsJson()) {
64
                return response('Not found', 404);
65
            } else {
66 3
                return $this->redirector->to($url);
67
            }
68
        }
69
70 1
        $locale = $this->multilang->detectLocale($request);
71
72 1
        $this->app->setLocale($locale);
73
74 1
        return $next($request);
75
    }
76
77
}
78