AppLocaleUpdated::handle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 13
ccs 9
cts 9
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace AlexJoffroy\Localization\Listeners;
4
5
use Illuminate\Foundation\Events\LocaleUpdated;
6
use AlexJoffroy\Localization\Localization;
7
8
class AppLocaleUpdated
9
{
10
    /** @var \AlexJoffroy\Localization\Localization */
11
    protected $localization;
12
    
13 22
    public function __construct(Localization $localization)
14
    {
15 22
        $this->localization = $localization;
16 22
    }
17
18 22
    public function handle(LocaleUpdated $event)
19
    {
20 22
        $supportedLocale = $this->localization->getSupportedLocale($event->locale);
21 22
        $constants = collect($supportedLocale->get('constants', []));
22 22
        $regionalCode = $supportedLocale->get('regional_code');
23 22
        $charset = $supportedLocale->get('charset', false);
24
25 22
        $localeCode = $charset ? "$regionalCode.$charset" : $regionalCode;
26
27
        $constants->each(function ($constant) use ($localeCode) {
28 22
            setlocale(constant($constant), $localeCode);
29 22
        });
30 22
    }
31
}
32