Passed
Branch 2.0 (72e182)
by Philippe
02:18
created

QueryDetector::get()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 4
nop 2
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Locale\Detectors;
4
5
use Illuminate\Http\Request;
6
use Thinktomorrow\Locale\Values\Locale;
7
use Thinktomorrow\Locale\Values\Config;
8
use Thinktomorrow\Locale\Scope;
9
10
class QueryDetector implements Detector
11
{
12
    /**
13
     * @var Request
14
     */
15
    private $request;
16
17 28
    public function __construct(Request $request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
18
    {
19 28
        $this->request = $request;
20 28
    }
21
22 28
    public function get(Scope $scope, Config $config): ?Locale
23
    {
24 28
        if(!isset($config['query_key'])) return null;
25
26 15
        if( ! $queryValue = $this->request->get($config['query_key']) ) return null;
27
28 3
        return ($scope->validateLocale(Locale::from($queryValue))) ? Locale::from($queryValue) : null;
29
    }
30
}