Passed
Push — master ( 6ca2b3...f03760 )
by Gabriel
13:19 queued 11s
created

SectionDetector   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 52.38%

Importance

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

6 Methods

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