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

HiddenSegmentDetector   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 10 2
A segment() 0 6 1
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 HiddenSegmentDetector 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
        // If a locale segment is found, it means locale is not hidden so we won't bother going further.
25 28
        if ($this->segment($scope)) return null;
26
27
        // At this point if no locale segment is given and if in config the hidden_locale is set,
28
        // we assume the locale is hidden.
29
        // TODO account for domain specific hidden (default) locales
30 13
        return $scope->defaultLocale();
31
    }
32
33 28
    public function segment(Scope $scope): ?Locale
34
    {
35 28
        $segment = $this->request->segment(1);
36
37 28
        return $scope->findLocale($segment);
38
    }
39
}