Completed
Branch develop (51c2e3)
by Anton
03:56 queued 01:52
created

Section::getIcon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Vault\Models;
10
11
use Spiral\Security\GuardInterface;
12
13
/**
14
 * @todo get badge class (dynamic value)
15
 */
16
class Section
17
{
18
    /**
19
     * @var GuardInterface
20
     */
21
    private $guard = null;
22
23
    /**
24
     * @var array
25
     */
26
    private $section = [
27
        'title' => '',
28
        'badge' => '',
29
        'icon'  => '',
30
        'items' => []
31
    ];
32
33
    /**
34
     * @var Item[]
35
     */
36
    private $items = [];
37
38
    /**
39
     * @param GuardInterface $guard
40
     * @param array          $section
41
     */
42
    public function __construct(GuardInterface $guard, array $section)
43
    {
44
        $this->guard = $guard;
45
        $this->section = $section;
46
47
        foreach ($this->section['items'] as $target => $item) {
48
            $this->items[] = new Item($target, $item);
49
        }
50
    }
51
52
    /**
53
     * Section title.
54
     *
55
     * @return string
56
     */
57
    public function getTitle()
58
    {
59
        return $this->section['title'];
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    public function getIcon()
66
    {
67
        return $this->section['icon'];
68
    }
69
70
    /**
71
     * Must return true is section has at least one available item.
72
     *
73
     * @return bool
74
     */
75
    public function isAvailable()
76
    {
77
        foreach ($this->items as $item) {
78
            if ($item->isAllowed($this->guard)) {
79
                return true;
80
            }
81
        }
82
83
        return false;
84
    }
85
86
    /**
87
     * Must return true if at least one of section items points to a given controller (based on
88
     * target).
89
     *
90
     * @param string $controller
91
     *
92
     * @return bool
93
     */
94
    public function hasController($controller)
95
    {
96
        foreach ($this->items as $item) {
97
            if (
98
                $item->getTarget() == $controller
99
                || strpos($item->getTarget(), $controller . ':') === 0
100
            ) {
101
                return true;
102
            }
103
        }
104
105
        return false;
106
    }
107
108
    /**
109
     * @return Item[]
110
     */
111
    public function getItems()
112
    {
113
        return $this->items;
114
    }
115
}