MenuItem::isAllowed()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Nexendrie\Menu;
5
6
/**
7
 * Menu item
8
 *
9
 * @author Jakub Konečný
10
 * @property string $link
11
 * @property-read bool $allowed
12
 */
13 1
class MenuItem extends Collection {
14
  public string $text;
15
  protected string $link;
16
  public string $rawLink;
17
  /** @var array[] of [IMenuItemCondition, string] */
18
  protected array $conditions = [];
19
  /** @var IMenuItemLinkRender[] */
20
  protected array $linkRenders = [];
21
  
22
  public function __construct(string $link, string $text) {
23 1
    parent::__construct();
24 1
    $this->setRawLink($link);
25 1
    $this->text = $text;
26 1
  }
27
28
  /**
29
   * @deprecated Access the property directly
30
   */
31
  public function getLink(): string {
32 1
    $link = $this->rawLink;
33 1
    foreach($this->linkRenders as $render) {
34 1
      if($render->isApplicable($link)) {
35 1
        $link = $render->renderLink($link);
36 1
        break;
37
      }
38
    }
39 1
    return $link;
40
  }
41
42
  /**
43
   * @deprecated Access the property directly
44
   */
45
  public function setLink(string $link): void {
46
    $this->setRawLink($link);
47
  }
48
49
  /**
50
   * @deprecated Access the property directly
51
   */
52
  public function getRawLink(): string {
53
    return $this->link;
54
  }
55
56
  protected function setRawLink(string $rawLink): void {
57 1
    $this->rawLink = $this->link = $rawLink;
58 1
  }
59
60
  /**
61
   * @deprecated Access the property directly
62
   */
63
  public function getText(): string {
64
    return $this->text;
65
  }
66
67
  /**
68
   * @deprecated Access the property directly
69
   */
70
  public function setText(string $text): void {
71
    $this->text = $text;
72
  }
73
74
  /**
75
   * @param mixed $parameter
76
   */
77
  public function addCondition(IMenuItemCondition $condition, $parameter): void {
78 1
    $this->conditions[$condition->getName()] = [$condition, $parameter];
79 1
  }
80
  
81
  public function addLinkRender(IMenuItemLinkRender $render): void {
82 1
    $this->linkRenders[$render->getName()] = $render;
83 1
  }
84
85
  /**
86
   * @deprecated Access the property directly
87
   */
88
  public function isAllowed(): bool {
89 1
    foreach($this->conditions as $condition) {
90 1
      if(!$condition[0]->isAllowed($condition[1])) {
91 1
        return false;
92
      }
93
    }
94 1
    return true;
95
  }
96
}
97
?>