Passed
Push — master ( 6b8e14...f27647 )
by Avtandil
03:15
created

MultiLang::handle()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 2
dl 0
loc 22
ccs 11
cts 11
cp 1
crap 4
rs 8.9197
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 Carbon\Carbon;
14
use Closure;
15
use Illuminate\Foundation\Application;
16
use Illuminate\Routing\Redirector;
17
use Longman\LaravelMultiLang\MultiLang as MultiLangLib;
18
19
class MultiLang
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
        if ($this->multilang->getConfig()->get('set_carbon_locale')) {
80 1
            Carbon::setLocale($locale);
81
        }
82
83 1
        return $next($request);
84
    }
85
}
86