TMember   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
dl 0
loc 82
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isGuest() 0 2 1
A isMember() 0 2 1
A getRoles() 0 2 1
B has() 0 44 6
1
<?php
2
3
/**
4
 * @file TMember.php
5
 * @brief This file contains the TMember trait.
6
 * @details
7
 * @author Filippo F. Fadda
8
 */
9
10
11
namespace Daikengo\User;
12
13
14
use Daikengo\User\IUser;
15
use Daikengo\Permission\IPermission;
16
use Daikengo\Collection\RoleCollection;
17
18
use Meta\Extension;
19
20
21
/**
22
 * @brief This trait implements the `IUser` interface for the `Member` class.
23
 * @details Use this trait when you can't extend the `Member` class since you already have a class for it in your
24
 * project.
25
 *
26
 * @cond HIDDEN_SYMBOLS
27
 *
28
 * @property RoleCollection $roles
29
 *
30
 * @endcond
31
 */
32
trait TMember {
33
  use Extension\TProperty;
34
35
  /**
36
   * @var RoleCollection $roles
37
   */
38
  private $roles;
39
40
41
  /**
42
   * @copydoc IUser::has()
43
   */
44
  public function has(IPermission $permission) {
45
    $result = FALSE;
46
47
    $permissionReflection = new \ReflectionObject($permission);
48
49
    foreach ($this->roles as $roleName => $roleClass) {
50
51
      do {
52
        $role = new $roleClass;
53
54
        // Sets the execution role for the current user.
55
        $permission->setRole($role);
56
57
        // Creates a reflection class for the roleName.
58
        $roleReflection = new \ReflectionObject($role);
59
60
        // Determines the method's name related to the roleName.
61
        $methodName = 'checkFor' . $roleReflection->getShortName();
62
63
        if ($permissionReflection->hasMethod($methodName)) { // If a method exists for the roleName...
64
          // Gets the method.
65
          $method = $permissionReflection->getMethod($methodName);
66
67
          // Invokes the method.
68
          $result = $method->invoke($permission);
69
70
          // Exits from the do while and foreach as well.
71
          break 2;
72
        }
73
        else {
74
          // Go back to the previous role class in the hierarchy. For example, from AdminRole to ModeratorRole.
75
          $parentRoleReflection = $roleReflection->getParentClass();
76
77
          // Proceed only if the parent role is not an abstract class.
78
          if (is_object($parentRoleReflection) && !$parentRoleReflection->isAbstract())
79
            $roleClass = $parentRoleReflection->name;
80
          else
81
            break; // No more roles in the hierarchy.
82
        }
83
      } while (TRUE);
84
85
    }
86
87
    return $result;
88
  }
89
90
91
  /**
92
   * @brief This implementation returns always `false`.
93
   * @return bool
94
   */
95
  public function isGuest() {
96
    return FALSE;
97
  }
98
99
100
  /**
101
   * @brief This implementation returns always `true`.
102
   * @return bool
103
   */
104
  public function isMember() {
105
    return TRUE;
106
  }
107
108
109
  /**
110
   * @copydoc IUser::getRoles()
111
   */
112
  public function getRoles() {
113
    return $this->roles;
114
  }
115
116
}