HeaderConversion::convert()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 21

Duplication

Lines 14
Ratio 66.67 %

Code Coverage

Tests 13
CRAP Score 3.0032

Importance

Changes 0
Metric Value
dl 14
loc 21
ccs 13
cts 14
cp 0.9286
rs 9.584
c 0
b 0
f 0
cc 3
nc 1
nop 1
crap 3.0032
1
<?php
2
/**
3
 * CalamandreiLorenzo\LaravelBrowserLang
4
 */
5
6
namespace CalamandreiLorenzo\LaravelBrowserLang;
7
8
use Illuminate\Support\Facades\App;
9
10
/**
11
 * Class HeaderConversion
12
 * @package CalamandreiLorenzo\LaravelBrowserLang
13
 * @author Lorenzo Calamandrei <[email protected]>
14
 * @github https://github.com/CalamandreiLorenzo
15
 */
16
class HeaderConversion implements HeaderConversionInterface
17
{
18
    /**
19
     * convert
20
     * @param string $header
21
     * @return string
22
     */
23 3
    public function convert(string $header): string
24
    {
25 3
        $langs = collect(explode(',', $header))->map(static function (string $lang) {
26 3 View Code Duplication
            if (str_contains($lang, ';')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27 1
                $langWithQ = explode(';', $lang);
28 1
                return collect([
29 1
                    'locale' => $langWithQ[0],
30 1
                    'q-factor' => $langWithQ[1]
31
                ]);
32
            }
33 3 View Code Duplication
            if (str_contains($lang, '-')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34 3
                $langParam = explode('-', $lang)[0];
35 3
                return collect([
36 3
                    'locale' => $langParam,
37
                    'q-factor' => null
38
                ]);
39
            }
40
            return $lang;
41 3
        })->whereIn('locale', config('browser-lang.available_locales', ['en']));
42 3
        return $langs->first()->get('locale', App::getLocale());
43
    }
44
}
45