SubdomainDetectorDriver   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A detect() 0 12 3
A getSegments() 0 4 1
A isPartsValid() 0 4 1
1
<?php
2
3
namespace Vluzrmos\LanguageDetector\Drivers;
4
5
/**
6
 * Class SubdomainDetectorDriver.
7
 */
8
class SubdomainDetectorDriver extends AbstractDetector
9
{
10
    /**
11
     * Minimun parts of the subdomain.
12
     *
13
     * @var int
14
     */
15
    protected $minParts = 3;
16
17
    /**
18
     * Return detected language.
19
     *
20
     * @return string
21
     */
22
    public function detect()
23
    {
24
        $parts = $this->getSegments();
25
26
        $locale = null;
27
28
        if ($this->isPartsValid($parts)) {
29
            $locale = $parts[$this->getDefaultSegment()];
30
        }
31
32
        return $locale ? $this->getAliasedLocale($locale) : null;
33
    }
34
35
    /**
36
     * Get parts of the subdomain.
37
     *
38
     * @return array
39
     */
40
    public function getSegments()
41
    {
42
        return preg_split('/\./', $this->request->getHost());
43
    }
44
45
    /**
46
     * Check if parts of the url are valid.
47
     *
48
     * @param array $parts
49
     * @return bool
50
     */
51
    public function isPartsValid(array $parts)
52
    {
53
        return count($parts) >= $this->minParts;
54
    }
55
}
56