Completed
Push — master ( 0eceaa...aee3a6 )
by Ben
15:21 queued 05:47
created

Detect   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 102
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A detectLocale() 0 21 2
A getLocale() 0 8 2
A getScope() 0 8 2
A setScope() 0 8 2
A detectScope() 0 4 1
1
<?php
2
3
namespace Thinktomorrow\Locale;
4
5
use Illuminate\Http\Request;
6
use Thinktomorrow\Locale\Detectors\FallbackDetector;
7
use Thinktomorrow\Locale\Detectors\HiddenSegmentDetector;
8
use Thinktomorrow\Locale\Detectors\QueryDetector;
9
use Thinktomorrow\Locale\Detectors\SegmentDetector;
10
use Thinktomorrow\Locale\Values\Config;
11
use Thinktomorrow\Locale\Values\Locale;
12
13
final class Detect
14
{
15
    /**
16
     * @var Request
17
     */
18
    private $request;
19
20
    /**
21
     * @var Config
22
     */
23
    private $config;
24
25
    /**
26
     * @var Locale
27
     */
28
    private $locale;
29
30
    /**
31
     * Current scope of locales.
32
     *
33
     * @var Scope
34
     */
35
    private $scope;
36
37 64
    public function __construct(Request $request, Config $config)
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...
38
    {
39 64
        $this->request = $request;
40 64
        $this->config = $config;
41 64
    }
42
43
    /**
44
     * Detect the locale from current request url.
45
     * Once the locale has been determined, it will be set as the application locale.
46
     * A locale is only validated if it is present within the current locale scope.
47
     *
48
     * Detection honours following priority:
49
     * 1) If locale is found in request as query parameter e.g. ?locale=fr,
50
     * 2) If locale is found in request url, either from host or segment eg. nl.example.com, example.nl or example.com/nl
51
     * 3) Otherwise set locale to our fallback language (app.locale)
52
     */
53 64
    public function detectLocale(): self
54
    {
55 64
        $locale = null;
56
57
        $detectors = [
58 64
            FallbackDetector::class,
59
            HiddenSegmentDetector::class,
60
            SegmentDetector::class,
61
            QueryDetector::class,
62
        ];
63
64 64
        foreach ($detectors as $detector) {
65 64
            $locale = app($detector)->get($this->getScope(), $this->config) ?? $locale;
66
        }
67
68 64
        $this->locale = $locale;
69
70 64
        app()->setLocale($locale->get());
71
72 64
        return $this;
73
    }
74
75 64
    public function getLocale(): Locale
76
    {
77 64
        if (!$this->locale) {
78 64
            $this->detectLocale();
79
        }
80
81 64
        return $this->locale;
82
    }
83
84 64
    public function getScope(): Scope
85
    {
86 64
        if (!$this->scope) {
87 63
            $this->detectScope();
88
        }
89
90 64
        return $this->scope;
91
    }
92
93
    /**
94
     * This is handy for setting allowed scope via other source than config file.
95
     * this can be configurable from a cms.
96
     *
97
     * @param Scope|null $scope
98
     *
99
     * @return $this
100
     */
101 2
    public function setScope(Scope $scope = null)
102
    {
103 2
        if ($scope) {
104 2
            $this->scope = $scope;
105
        }
106
107 2
        return $this;
108
    }
109
110 63
    private function detectScope()
111
    {
112 63
        $this->scope = ScopeCollection::fromConfig($this->config)->findByRoot($this->request->root());
113 63
    }
114
}
115