Completed
Push — master ( 890b95...e7da07 )
by ARCANEDEV
12s
created

Manager::setSelectedItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
cc 1
nc 1
nop 1
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php namespace Arcanesoft\Sidebar;
2
3
use Arcanesoft\Sidebar\Contracts\Manager as ManagerContract;
4
use Illuminate\Support\Collection as IlluminateCollection;
5
6
/**
7
 * Class     Manager
8
 *
9
 * @package  Arcanesoft\Sidebar
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class Manager implements ManagerContract
13
{
14
    /* -----------------------------------------------------------------
15
     |  Properties
16
     | -----------------------------------------------------------------
17
     */
18
19
    /** @var  \Arcanesoft\Sidebar\Collection */
20
    protected $items;
21
22
    /** @var  bool */
23
    protected $shown = true;
24
25
    /* -----------------------------------------------------------------
26
     |  Constructor
27
     | -----------------------------------------------------------------
28
     */
29
30 24
    public function __construct()
31
    {
32 24
        $this->items = new Collection;
33 24
    }
34
35
    /* -----------------------------------------------------------------
36
     |  Getters & Setters
37
     | -----------------------------------------------------------------
38
     */
39
40
    /**
41
     * Set the selected item.
42
     *
43
     * @param  string  $name
44
     *
45
     * @return $this
46
     */
47 4
    public function setSelectedItem(string $name)
48
    {
49 4
        $this->items->setSelected($name);
50
51 4
        return $this;
52
    }
53
54
    /**
55
     * Get the sidebar items.
56
     *
57
     * @return \Arcanesoft\Sidebar\Collection
58
     */
59 20
    public function items()
60
    {
61 20
        return $this->items;
62
    }
63
64
    /* -----------------------------------------------------------------
65
     |  Main Methods
66
     | -----------------------------------------------------------------
67
     */
68
69
    /**
70
     * Load the sidebar items from config files.
71
     *
72
     * @param  array  $items
73
     *
74
     * @return $this
75
     */
76 4
    public function loadFromArray(array $items)
77
    {
78 4
        foreach ($items as $item) {
79 4
            if (is_array($item) && ! empty($item)) {
80 4
                $this->add($item);
81
            }
82
        }
83
84 4
        return $this;
85
    }
86
87
    /**
88
     * Load sidebar items from config file(s).
89
     *
90
     * @param  string  $key
91
     *
92
     * @return $this
93
     */
94 8
    public function loadFromConfig($key)
95
    {
96 8
        $items = new IlluminateCollection(config()->get($key, []));
97
98 8
        if ($items->isEmpty()) return $this;
99
100
        $allHasConfig = $items->every(function ($item) {
101 8
            return config()->has($item);
102 8
        });
103
104 8
        if ($allHasConfig) {
105
            $items->each(function ($item) {
106 8
                $this->loadFromConfig($item);
107 8
            });
108
109 8
            return $this;
110
        }
111
112 8
        return $this->add($items->toArray());
113
    }
114
115
    /**
116
     * Add an item from array.
117
     *
118
     * @param  array  $item
119
     *
120
     * @return $this
121
     */
122 16
    public function add(array $item)
123
    {
124 16
        $this->items->addItem($item);
125
126 16
        return $this;
127
    }
128
129
    /**
130
     * Show the sidebar.
131
     *
132
     * @return $this
133
     */
134 4
    public function show()
135
    {
136 4
        $this->shown = true;
137
138 4
        return $this;
139
    }
140
141
    /**
142
     * Hide the sidebar.
143
     *
144
     * @return $this
145
     */
146 4
    public function hide()
147
    {
148 4
        $this->shown = false;
149
150 4
        return $this;
151
    }
152
153
    /* -----------------------------------------------------------------
154
     |  Check Methods
155
     | -----------------------------------------------------------------
156
     */
157
158
    /**
159
     * Check if the sidebar has items.
160
     *
161
     * @return bool
162
     */
163 12
    public function hasItems()
164
    {
165 12
        return $this->items->isNotEmpty();
166
    }
167
168
    /**
169
     * Check if the sidebar is shown.
170
     *
171
     * @return bool
172
     */
173 4
    public function isShown()
174
    {
175 4
        return $this->shown === true;
176
    }
177
}
178