Passed
Push — master ( cd9417...6e4265 )
by Gabriel
03:32 queued 12s
created

SectionDetector   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 52.38%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 61
ccs 11
cts 21
cp 0.5238
rs 10
c 1
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A detectFromConstant() 0 3 1
A getCurrentSubdomain() 0 3 1
A run() 0 4 1
A detectFromSubdomain() 0 10 3
A detect() 0 12 3
1
<?php
2
3
namespace Nip\Mvc\Sections;
4
5
/**
6
 * Class SectionDetector
7
 * @package Nip\Mvc\Sections
8
 */
9
class SectionDetector
10
{
11
    /**
12
     * @param SectionsCollection $collection
13
     * @return string
14
     */
15
    public static function run($collection)
16
    {
17
        $detector = new static();
18
        return $detector->detect($collection);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $detector->detect($collection) also could return the type boolean which is incompatible with the documented return type string.
Loading history...
19
    }
20
21
    /**
22
     * @param $collection
23
     * @return bool|int|mixed|string
24
     */
25 1
    public function detect($collection)
26
    {
27 1
        $current = $this->detectFromConstant();
28 1
        if ($current) {
0 ignored issues
show
introduced by
The condition $current is always false.
Loading history...
29
            return $current;
30
        }
31 1
        $current = $this->detectFromSubdomain($collection);
32 1
        if ($current) {
33 1
            return $current;
34
        }
35
36
        return 'main';
37
    }
38
39
    /**
40
     * @return bool|string
41
     */
42
    protected function detectFromConstant()
43
    {
44
        return false;
45
//        return (defined('SPORTIC_SECTION')) ? SPORTIC_SECTION : false;
46
    }
47
48
    /**
49
     * @param $collection
50
     * @return bool|mixed
51
     */
52 1
    protected function detectFromSubdomain($collection)
53
    {
54 1
        $subDomain = $this->getCurrentSubdomain();
55 1
        foreach ($collection as $key => $section) {
56 1
            if ($subDomain == $section->getSubdomain()) {
57 1
                return $key;
58
            }
59
        }
60
61
        return $subDomain;
62
    }
63
64
    /**
65
     * @return bool|mixed
66
     */
67
    protected function getCurrentSubdomain()
68
    {
69
        return request()->getHttp()->getSubdomain();
70
    }
71
}
72