Completed
Pull Request — master (#3)
by ARCANEDEV
07:13
created

SidebarCollection   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 61
c 0
b 0
f 0
ccs 12
cts 12
cp 1
rs 10
wmc 5
lcom 0
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setSelected() 0 6 1
A hasAnySelected() 0 6 1
A pushItems() 0 8 2
A pushItem() 0 4 1
1
<?php namespace Arcanesoft\Sidebar;
2
3
use Illuminate\Support\Collection;
4
5
/**
6
 * Class     SidebarCollection
7
 *
8
 * @package  Arcanesoft\Sidebar
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class SidebarCollection extends Collection
12
{
13
    /* -----------------------------------------------------------------
14
     |  Main Methods
15
     | -----------------------------------------------------------------
16
     */
17
18
    /**
19
     * Push multiple sidebar items into the collection.
20
     *
21
     * @param  array  $items
22
     *
23
     * @return $this
24
     */
25 52
    public function pushItems(array $items)
26
    {
27 52
        foreach ($items as $item) {
28 16
            $this->pushItem($item);
29
        }
30
31 52
        return $this;
32
    }
33
34
    /**
35
     * Push a new sidebar item to the collection.
36
     *
37
     * @param  array  $attributes
38
     *
39
     * @return $this
40
     */
41 28
    public function pushItem(array $attributes)
42
    {
43 28
        return $this->push(new SidebarItem($attributes));
44
    }
45
46
    /**
47
     * Set the selected item.
48
     *
49
     * @param  string  $name
50
     *
51
     * @return $this
52
     */
53 12
    public function setSelected(string $name)
54
    {
55
        return $this->transform(function (SidebarItem $item) use ($name) {
56 8
            return $item->setSelected($name);
57 12
        });
58
    }
59
60
    /**
61
     * Check if there is any item selected.
62
     *
63
     * @return bool
64
     */
65 16
    public function hasAnySelected() : bool
66
    {
67
        return $this->filter(function (SidebarItem $item) {
68 12
            return $item->isActive();
69 16
        })->isNotEmpty();
70
    }
71
}
72