Completed
Push — master ( 3a3759...49115f )
by Jakub
17:00
created

MenuItem::addLinkRender()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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 string $text
12
 * @property-read bool $allowed
13
 */
14 1
class MenuItem {
15 1
  use \Nette\SmartObject;
16
  
17
  /** @var string */
18
  protected $text;
19
  /** @var string */
20
  protected $link;
21
  /** @var array of [IMenuItemCondition, parameter] */
22
  protected $conditions = [];
23
  /** @var IMenuItemLinkRender[] */
24
  protected $linkRenders = [];
25
  
26
  public function __construct(string $link, string $text) {
27 1
    $this->link = $link;
28 1
    $this->text = $text;
29 1
  }
30
  
31
  public function getLink(): string {
32 1
    $link = $this->link;
33 1
    foreach($this->linkRenders as $render) {
34 1
      if($render->isApplicable($this->link)) {
35 1
        $link =  $render->renderLink($this->link);
36 1
        break;
37
      }
38
    }
39 1
    return $link;
40
  }
41
  
42
  public function setLink(string $link) {
43 1
    $this->link = $link;
44 1
  }
45
  
46
  public function getText(): string {
47 1
    return $this->text;
48
  }
49
  
50
  public function setText(string $text) {
51 1
    $this->text = $text;
52 1
  }
53
  
54
  /**
55
   * @param mixed $parameter
56
   */
57
  public function addCondition(IMenuItemCondition $condition, $parameter): void {
58 1
    $this->conditions[$condition->getName()] = [$condition, $parameter];
59 1
  }
60
  
61
  public function addLinkRender(IMenuItemLinkRender $render): void {
62 1
    $this->linkRenders[$render->getName()] = $render;
63 1
  }
64
  
65
  public function isAllowed(): bool {
66 1
    foreach($this->conditions as $condition) {
67 1
      if(!$condition[0]->isAllowed($condition[1])) {
68 1
        return false;
69
      }
70
    }
71 1
    return true;
72
  }
73
}
74
?>