ConditionPermission::isAllowed()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 5
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 3
rs 10
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
?>