Completed
Push — master ( b03532...25b1a7 )
by ARCANEDEV
03:01
created

Manager::syncCurrentName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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