Passed
Push — master ( 3be634...c73837 )
by Gabriel
01:57
created

Section::getManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace Nip\Mvc\Sections;
4
5
use Nip\Utility\Traits\DynamicPropertiesTrait;
6
7
/**
8
 * Class Section
9
 * @package Nip\Mvc\Sections
10
 *
11
 * @property $menu
12
 * @property $folder
13
 */
14
class Section
15
{
16
    use DynamicPropertiesTrait;
17
18
    /**
19
     * @var string
20
     */
21
    protected $name;
22
23
    /**
24
     * @var string
25
     */
26
    protected $subdomain;
27
28
    /**
29
     * @var string
30
     */
31
    protected $path = null;
32
33
    protected $icon_svg = null;
34
35
    protected $baseUrl = null;
36
37
    protected $visibleIn = [];
38
39
    /**
40
     * Section constructor.
41
     * @param array $data
42
     */
43 4
    public function __construct($data = [])
44
    {
45 4
        $this->writeData($data);
46 4
    }
47
48
    /**
49
     * @param $data
50
     */
51 4
    public function writeData($data)
52
    {
53 4
        foreach ($data as $key => $value) {
54 3
            $this->{$key} = $value;
55
        }
56 4
    }
57
58
    /**
59
     * @return string
60
     */
61
    public function getSubdomain()
62
    {
63
        return $this->subdomain;
64
    }
65
66
    /**
67
     * @param bool $url
68
     * @return string
69
     */
70 1
    public function getURL($url = false)
71
    {
72 1
        $url = $url ? $url : request()->root();
73 1
        $http = request()->getHttp();
74 1
        return str_replace(
75 1
            '://' . $http->getSubdomain() . '.' . $http->getRootDomain(),
76 1
            '://' . $this->subdomain . '.' . $http->getRootDomain(),
77 1
            $url
78
        );
79
    }
80
81
    /**
82
     * @return string|null
83
     */
84
    public function getBaseUrl()
85
    {
86
        if ($this->baseUrl === null) {
87
            $this->initBaseUrl();
88
        }
89
90
        return $this->baseUrl;
91
    }
92
93
    protected function initBaseUrl()
94
    {
95
        $baseUrl = request()->root();
96
        $http = request()->getHttp();
97
98
        $baseUrl = str_replace(
99
            '://' . $this->getManager()->getCurrent()->getSubdomain() . '.' . $http->getRootDomain() . '.',
100
            '://' . $this->getSubdomain() . '.' . $http->getRootDomain() . '.',
101
            $baseUrl
102
        );
103
104
        $this->baseUrl = $baseUrl;
105
    }
106
107
    /**
108
     * @param bool $url
109
     * @return mixed
110
     */
111
    public function assembleURL($url = false, $params = [])
112
    {
113
        $url = $url ? $url : '/';
114
115
        $url = $this->getBaseUrl() . $url;
0 ignored issues
show
Bug introduced by
Are you sure $url of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

115
        $url = $this->getBaseUrl() . /** @scrutinizer ignore-type */ $url;
Loading history...
116
        if (count($params)) {
117
            $url .= '?' . http_build_query($params);
118
        }
119
120
        return $url;
121
    }
122
123
    /**
124
     * Compile path for this section from a given path of current section
125
     *
126
     * @param bool $path
127
     * @return string
128
     */
129
    public function compilePath($path = false)
130
    {
131
        $currentBasePath = $this->getManager()->getCurrent()->getPath();
132
        $path = str_replace($currentBasePath, $this->getPath(), $path);
133
        return $path;
134
    }
135
136
    /**
137
     * Return the path for section
138
     *
139
     * @return null|string
140
     */
141
    public function getPath()
142
    {
143
        if ($this->path === null) {
144
            $this->initPath();
145
        }
146
        return $this->path;
147
    }
148
149
    protected function initPath()
150
    {
151
        $this->path = $this->generatePath();
152
    }
153
154
    /**
155
     * @return string
156
     */
157
    protected function generatePath()
158
    {
159
        $path = app('path.base');
160
        if (!$this->isCurrent()) {
161
            $path = str_replace(
162
                DIRECTORY_SEPARATOR . $this->getManager()->getCurrent()->getFolder() . '',
163
                DIRECTORY_SEPARATOR . $this->getFolder() . '',
164
                $path
165
            );
166
        }
167
        return $path;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $path also could return the type object which is incompatible with the documented return type string.
Loading history...
168
    }
169
170
    /**
171
     * @return bool
172
     */
173
    public function isCurrent()
174
    {
175
        return $this->getName() == $this->getManager()->getCurrent()->getName();
176
    }
177
178
    /**
179
     * @return string
180
     */
181
    public function getName()
182
    {
183
        return $this->name;
184
    }
185
186
    /**
187
     * @return SectionsManager
188
     */
189
    protected function getManager()
190
    {
191
        return app('mvc.sections');
192
    }
193
194
    /**
195
     * @return string
196
     */
197
    public function getFolder()
198
    {
199
        return $this->folder;
0 ignored issues
show
Bug Best Practice introduced by
The property folder does not exist on Nip\Mvc\Sections\Section. Since you implemented __get, consider adding a @property annotation.
Loading history...
200
    }
201
202
    /**
203
     * @param string $place
204
     * @return bool
205
     */
206 2
    public function visibleIn($place)
207
    {
208 2
        return in_array($place, $this->visibleIn);
209
    }
210
}
211