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
![]() |
|||
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
|
|||
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 |