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

MenuItem   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getText() 0 3 1
A __construct() 0 4 1
A getLink() 0 10 3
A setLink() 0 3 1
A setText() 0 3 1
A addCondition() 0 3 1
A addLinkRender() 0 3 1
A isAllowed() 0 8 3
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
?>