ConditionPermission   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 21
ccs 8
cts 8
cp 1
rs 10
c 1
b 0
f 1
eloc 9
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isAllowed() 0 7 3
A __construct() 0 2 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Nexendrie\Menu;
5
6
use Nette\Security\User;
7
use Nette\Utils\Strings;
8
9
/**
10
 * ConditionPermission
11
 *
12
 * @author Jakub Konečný
13
 */
14 1
final class ConditionPermission extends BaseCondition {
15
  private User $user;
16
  /** @var string */
17
  protected string $name = "acl";
18
  
19
  public function __construct(User $user) {
20 1
    $this->user = $user;
21 1
  }
22
  
23
  /**
24
   * @param string $parameter
25
   * @throws \InvalidArgumentException
26
   * @throws \OutOfBoundsException
27
   */
28
  public function isAllowed($parameter = null): bool {
29 1
    if(!is_string($parameter)) {
30 1
      throw new \InvalidArgumentException("Method " . __METHOD__ . " expects string as parameter.");
31 1
    } elseif(!str_contains($parameter, ":")) {
32 1
      throw new \OutOfBoundsException("Method " . __METHOD__ . " expects parameter in format resource:privilege.");
33
    }
34 1
    return $this->user->isAllowed(Strings::before($parameter, ":"), Strings::after($parameter, ":"));
35
  }
36
}
37
?>