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

SectionsManager::assembleURL()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\Mvc\Sections;
4
5
use Nip\Mvc\Sections\UrlTransformer\HasUrlTransformerTrait;
6
7
/**
8
 * Class SectionsManager
9
 * @package Nip\Mvc\Sections
10
 */
11
class SectionsManager
12
{
13
    use HasUrlTransformerTrait;
14
15
    protected $currentKey = null;
16
17
    /**
18
     * @var SectionsCollection
19
     */
20
    protected $sections = null;
21
22
    /**
23
     * @param $section
24
     * @param $url
25
     * @return mixed
26
     */
27
    public static function transformUrl($section, $url)
28
    {
29
        return static::instance()->getOne($section)->getURL($url);
30
    }
31
32
    /**
33
     * @param $section
34
     * @param $url
35
     * @return mixed
36
     */
37
    public static function assembleURL($section, $url, $params = [])
38
    {
39
        return static::instance()->getOne($section)->assembleURL($url, $params);
40
    }
41
42
    /**
43
     * @param $key
44
     * @return Section
45
     */
46
    public function get($key)
47
    {
48
        return $this->getSections()->get($key);
49
    }
50
51
    /**
52
     * @param $key
53
     * @return Section
54
     */
55
    public function getOne($key)
56
    {
57
        return $this->get($key);
58
    }
59
60
    /**
61
     * @return Section
62
     */
63
    public function getCurrent()
64
    {
65
        return $this->getSections()->get($this->getCurrentKey());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getCurrentKey() targeting Nip\Mvc\Sections\SectionsManager::getCurrentKey() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
66
    }
67
68
    /**
69
     * @return null
70
     */
71
    public function getCurrentKey()
72
    {
73
        if ($this->currentKey === null) {
74
            $this->setCurrentKey(SectionDetector::run($this->sections));
75
        }
76
77
        return $this->currentKey;
78
    }
79
80
    /**
81
     * @param $key
82
     */
83
    public function setCurrentKey($key)
84
    {
85
        $this->currentKey = $key;
86
    }
87
88
    /**
89
     * @return SectionsCollection
90
     */
91 3
    public function getSections(): SectionsCollection
92
    {
93 3
        if ($this->sections === null) {
94 3
            $this->sections = new SectionsCollection();
95 3
            $this->loadFromConfig();
96
        }
97 3
        return $this->sections;
98
    }
99
100
    /**
101
     * @param SectionsCollection $sections
102
     */
103
    public function setSections(SectionsCollection $sections): void
104
    {
105
        $this->sections = $sections;
106
    }
107
108
    /**
109
     * @return void
110
     * @deprecated no need to call init
111
     */
112
    public function init()
113
    {
114
    }
115
116
    /**
117
     * @param $place
118
     * @return mixed
119
     */
120 1
    public function visibleIn($place)
121
    {
122
        return $this->getSections()->filter(function ($item) use ($place) {
123
            /** @var Section $item */
124 1
            return $item->visibleIn($place);
125 1
        });
126
    }
127
128 3
    protected function loadFromConfig()
129
    {
130 3
        if (function_exists('config')) {
131 3
            $data = config('mvc.sections', []);
132 3
            foreach ($data as $key => $row) {
133 3
                $this->sections->set($key, $this->newSection($row->toArray()));
134
            }
135
        }
136 3
    }
137
138
    /**
139
     * @param $data
140
     * @return mixed
141
     */
142 3
    protected function newSection($data)
143
    {
144 3
        $section = app(Section::class);
145 3
        $section->writeData($data);
146 3
        return $section;
147
    }
148
149
    /**
150
     * Singleton
151
     * @return SectionsManager
152
     */
153
    public static function instance()
154
    {
155
        static $instance;
156
        if (!($instance instanceof SectionsManager)) {
157
            $instance = app('mvc.sections');
158
        }
159
        return $instance;
160
    }
161
}
162