Issues (14)

src/Sections/SectionsManager.php (3 issues)

1
<?php
2
3
namespace Nip\Mvc\Sections;
4
5
use Nip\Mvc\Sections\UrlTransformer\HasUrlTransformerTrait;
6
use Nip\Mvc\Utility\PackageConfig;
7
8
/**
9
 * Class SectionsManager
10
 * @package Nip\Mvc\Sections
11
 */
12
class SectionsManager
13
{
14
    use HasUrlTransformerTrait;
15
16
    protected $currentKey = null;
17
18
    /**
19
     * @var SectionsCollection
20
     */
21
    protected $sections = null;
22
23
    /**
24
     * @param $section
25
     * @param $url
26
     * @return mixed
27
     */
28
    public static function transformUrl($section, $url)
29
    {
30
        return static::instance()->getOne($section)->getURL($url);
31
    }
32
33
    /**
34
     * @param $section
35
     * @param $url
36
     * @return mixed
37
     */
38
    public static function assembleURL($section, $url, $params = [])
39
    {
40
        return static::instance()->getOne($section)->assembleURL($url, $params);
41
    }
42
43
    /**
44
     * @param $key
45
     * @return Section
46
     */
47
    public function get($key)
48
    {
49
        return $this->getSections()->get($key);
50
    }
51
52
    /**
53
     * @param $key
54
     * @return Section
55
     */
56
    public function getOne($key)
57
    {
58
        return $this->get($key);
59
    }
60
61
    /**
62
     * @return Section
63
     */
64
    public function getCurrent()
65
    {
66
        return $this->getSections()->get($this->getCurrentKey());
0 ignored issues
show
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...
67
    }
68
69
    /**
70
     * @return null
71
     */
72
    public function getCurrentKey()
73
    {
74
        if ($this->currentKey === null) {
75
            $this->setCurrentKey(SectionDetector::run($this->sections));
76
        }
77
78
        return $this->currentKey;
79
    }
80
81
    /**
82
     * @param $key
83
     */
84
    public function setCurrentKey($key)
85
    {
86
        $this->currentKey = $key;
87
    }
88
89
    /**
90
     * @return SectionsCollection
91 3
     */
92
    public function getSections(): SectionsCollection
93 3
    {
94 3
        if ($this->sections === null) {
95 3
            $this->sections = new SectionsCollection();
96
            $this->loadFromConfig();
97 3
        }
98
        return $this->sections;
99
    }
100
101
    /**
102
     * @param SectionsCollection $sections
103
     */
104
    public function setSections(SectionsCollection $sections): void
105
    {
106
        $this->sections = $sections;
107
    }
108
109
    /**
110
     * @return void
111
     * @deprecated no need to call init
112
     */
113
    public function init()
114
    {
115
    }
116
117
    /**
118
     * @param $place
119
     * @return mixed
120 1
     */
121
    public function visibleIn($place)
122
    {
123
        return $this->getSections()->filter(function ($item) use ($place) {
124 1
            /** @var Section $item */
125 1
            return $item->visibleIn($place);
126
        });
127
    }
128 3
129
    protected function loadFromConfig()
130 3
    {
131 3
        $data = PackageConfig::value('sections', []);
0 ignored issues
show
array() of type array is incompatible with the type null|string expected by parameter $default of ByTIC\PackageBase\Utility\PackageConfig::value(). ( Ignorable by Annotation )

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

131
        $data = PackageConfig::value('sections', /** @scrutinizer ignore-type */ []);
Loading history...
132 3
        foreach ($data as $key => $row) {
0 ignored issues
show
The expression $data of type string is not traversable.
Loading history...
133 3
            $this->sections->set($key, $this->newSection($row->toArray()));
134
        }
135
    }
136 3
137
    /**
138
     * @param $data
139
     * @return mixed
140
     */
141
    protected function newSection($data)
142 3
    {
143
        $section = app(Section::class);
144 3
        $section->writeData($data);
145 3
        return $section;
146 3
    }
147
148
    /**
149
     * Singleton
150
     * @return SectionsManager
151
     */
152
    public static function instance()
153
    {
154
        static $instance;
155
        if (!($instance instanceof SectionsManager)) {
156
            $instance = app('mvc.sections');
157
        }
158
        return $instance;
159
    }
160
}
161