Completed
Push — master ( f23563...5ddca9 )
by ARCANEDEV
03:15
created

Manager::addRouteItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 5
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php namespace Arcanesoft\Sidebar;
2
3
use Arcanesoft\Sidebar\Contracts\Manager as ManagerContract;
4
use Arcanesoft\Sidebar\Entities\Item;
5
use Arcanesoft\Sidebar\Entities\ItemCollection;
6
use Illuminate\Support\HtmlString;
7
8
/**
9
 * Class     Manager
10
 *
11
 * @package  Arcanesoft\Sidebar
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
class Manager implements ManagerContract
15
{
16
    /* -----------------------------------------------------------------
17
     |  Properties
18
     | -----------------------------------------------------------------
19
     */
20
21
    /**
22
     * The view name.
23
     *
24
     * @var string
25
     */
26
    protected $view = '';
27
28
    /**
29
     * The current name.
30
     *
31
     * @var string
32
     */
33
    protected $currentName;
34
35
    /**
36
     * The sidebar items collection.
37
     *
38
     * @var \Arcanesoft\Sidebar\Entities\ItemCollection
39
     */
40
    protected $items;
41
42
    /**
43
     * The authenticated user.
44
     *
45
     * @var \Arcanesoft\Contracts\Auth\Models\User
46
     */
47
    protected $user;
48
49
    /* -----------------------------------------------------------------
50
     |  Constructor
51
     | -----------------------------------------------------------------
52
     */
53
54
    /**
55
     * Manager constructor.
56
     */
57 12
    public function __construct()
58
    {
59 12
        $this->items = new ItemCollection;
60 12
    }
61
62
    /* -----------------------------------------------------------------
63
     |  Getters & Setters
64
     | -----------------------------------------------------------------
65
     */
66
67
    /**
68
     * Set the view name.
69
     *
70
     * @param  string  $view
71
     *
72
     * @return self
73
     */
74
    public function setView($view)
75
    {
76
        if ( ! is_null($view))
77
            $this->view = $view;
78
79
        return $this;
80
    }
81
82
    /**
83
     * Get the current item name.
84
     *
85
     * @return string
86
     */
87 3
    public function getCurrent()
88
    {
89 3
        return $this->currentName;
90
    }
91
92
    /**
93
     * Set the current item name.
94
     *
95
     * @param  string  $currentName
96
     *
97
     * @return $this
98
     */
99 3
    public function setCurrent($currentName)
100
    {
101 3
        $this->currentName = $currentName;
102
103 3
        return $this;
104
    }
105
106
    /**
107
     * Get the sidebar items.
108
     *
109
     * @return \Arcanesoft\Sidebar\Entities\ItemCollection
110
     */
111 9
    public function getItems()
112
    {
113 9
        return $this->items;
114
    }
115
116
    /* -----------------------------------------------------------------
117
     |  Main Methods
118
     | -----------------------------------------------------------------
119
     */
120
121
    /**
122
     * Add a routed item.
123
     *
124
     * @param  string       $name
125
     * @param  string       $title
126
     * @param  string       $route
127
     * @param  array        $parameters
128
     * @param  string|null  $icon
129
     *
130
     * @return self
131
     */
132 3
    public function addRouteItem($name, $title, $route, array $parameters = [], $icon = null)
133
    {
134 3
        return $this->addItem($name, $title, route($route, $parameters), $icon);
135
    }
136
137
    /**
138
     * Add an item.
139
     *
140
     * @param  string       $name
141
     * @param  string       $title
142
     * @param  string       $url
143
     * @param  string|null  $icon
144
     *
145
     * @return self
146
     */
147 6
    public function addItem($name, $title, $url = '#', $icon = null)
148
    {
149 6
        return $this->add(compact('name', 'title', 'url', 'icon'));
150
    }
151
152
    /**
153
     * Add an item from array.
154
     *
155
     * @param  array  $array
156
     *
157
     * @return self
158
     */
159 6
    public function add(array $array)
160
    {
161 6
        $item = Item::makeFromArray($array);
162
163 6
        if ($item->allowed())
164 6
            $this->items->push($item);
165
166 6
        return $this;
167
    }
168
169
    /**
170
     * Load items from multiple config keys.
171
     *
172
     * @param  string  $key
173
     *
174
     * @return self
175
     */
176
    public function loadItemsFromConfig($key)
177
    {
178
        foreach (config($key, []) as $configKey) {
179
            $this->loadItemFromConfig($configKey);
180
        }
181
182
        return $this;
183
    }
184
185
    /**
186
     * Load sidebar item from config file.
187
     *
188
     * @param  string  $key
189
     *
190
     * @return self
191
     */
192
    public function loadItemFromConfig($key)
193
    {
194
        if (config()->has($key))
195
            $this->add(config($key));
196
        else
197
            // Throw an exception ??
198
199
        return $this;
200
    }
201
202
    /**
203
     * Render the sidebar.
204
     *
205
     * @param  string|null  $view
206
     *
207
     * @return \Illuminate\Support\HtmlString
208
     */
209
    public function render($view = null)
210
    {
211
        $this->syncCurrentName()->setView($view);
212
213
        return new HtmlString(
214
            view($this->view, ['sidebarItems' => $this->getItems()])->render()
215
        );
216
    }
217
218
    /* -----------------------------------------------------------------
219
     |  Check Methods
220
     | -----------------------------------------------------------------
221
     */
222
223
    /**
224
     * Check if the sidebar has items.
225
     *
226
     * @return bool
227
     */
228 6
    public function hasItems()
229
    {
230 6
        return ! $this->items->isEmpty();
231
    }
232
233
    /* -----------------------------------------------------------------
234
     |  Other Methods
235
     | -----------------------------------------------------------------
236
     */
237
238
    /**
239
     * Sync the current name wih the sidebar items.
240
     *
241
     * @return self
242
     */
243
    private function syncCurrentName()
244
    {
245
        $this->items->setCurrent($this->currentName);
246
247
        return $this;
248
    }
249
}
250