Item   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 77
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getTarget() 0 4 1
A getTitle() 0 4 1
A getUri() 0 4 1
A isVisible() 0 13 2
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 Psr\Http\Message\UriInterface;
12
use Spiral\Core\Component;
13
use Spiral\Translator\Traits\TranslatorTrait;
14
use Spiral\Vault\Vault;
15
16
class Item extends Component
17
{
18
    use TranslatorTrait;
19
20
    /**
21
     * @var Vault
22
     */
23
    private $vault;
24
25
    /**
26
     * @var string
27
     */
28
    private $target = '';
29
30
    /**
31
     * @var array
32
     */
33
    private $item = [
34
        'title'      => '',
35
        'badge'      => '',
36
        'permission' => '',
37
    ];
38
39
    /**
40
     * @param string $target
41
     * @param array  $options
42
     */
43
    public function __construct(Vault $vault, string $target, array $options)
44
    {
45
        $this->vault = $vault;
46
        $this->target = $target;
47
        $this->item = $options;
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    public function getTarget(): string
54
    {
55
        return $this->target;
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    public function getTitle(): string
62
    {
63
        return $this->say($this->item['title']);
64
    }
65
66
    /**
67
     * @return UriInterface
68
     */
69
    public function getUri(): UriInterface
70
    {
71
        return $this->vault->uri($this->getTarget());
72
    }
73
74
    /**
75
     * Check if item is visible.
76
     *
77
     * @return bool
78
     */
79
    public function isVisible(): bool
80
    {
81
        if (!empty($this->item['permission'])) {
82
            return $this->vault->getGuard()->allows($this->item['permission']);
83
        }
84
85
        //Remove :action
86
        $target = current(explode(':', $this->getTarget()));
87
88
        return $this->vault->getGuard()->allows(
89
            "{$this->vault->getConfig()->guardNamespace()}.{$target}"
90
        );
91
    }
92
}