Completed
Push — master ( baf319...ed3b10 )
by Alex
01:19
created

SetLocaleFromCurrentRoute   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 12 2
1
<?php
2
3
namespace AlexJoffroy\Localization\Http\Middlewares;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use AlexJoffroy\Localization\Localization;
8
use Symfony\Component\HttpFoundation\Response;
9
10
class SetLocaleFromCurrentRoute
11
{
12
    /** @var \AlexJoffroy\Localization\Localization */
13
    protected $localization;
14
15
    public function __construct(Localization $localization)
16
    {
17
        $this->localization = $localization;
18
    }
19
20
    public function handle(Request $request, Closure $next): Response
21
    {
22
        $locale = $request->segment(1, '');
23
        
24
        if (!$this->localization->isSupportedLocale($locale)) {
25
            $locale = $this->localization->getDefaultLocale();
26
        }
27
28
        $this->localization->setLocale($locale);
29
30
        return $next($request);
31
    }
32
}
33