1 | <?php |
||
9 | class AttributeManager |
||
10 | { |
||
11 | /** @var array **/ |
||
12 | private $attributes; |
||
13 | |||
14 | /** |
||
15 | * @param array $attributes |
||
16 | */ |
||
17 | 25 | public function __construct($attributes) |
|
21 | |||
22 | /** |
||
23 | * @param string $attributeId |
||
24 | * @return \PhpAbac\Model\AbstractAttribute |
||
25 | */ |
||
26 | 8 | public function getAttribute($attributeId) { |
|
27 | 8 | $attributeKeys = explode('.', $attributeId); |
|
28 | // The first element will be the attribute ID, then the field ID |
||
29 | 8 | $attributeId = array_shift($attributeKeys); |
|
30 | 8 | $attributeName = implode('.', $attributeKeys); |
|
31 | // The field ID is also the attribute object property |
||
32 | 8 | $attributeData = $this->attributes[$attributeId]; |
|
33 | return |
||
34 | 8 | ($attributeId === 'environment') |
|
35 | 4 | ? $this->getEnvironmentAttribute($attributeData, $attributeName) |
|
36 | 8 | : $this->getClassicAttribute($attributeData, $attributeName) |
|
37 | ; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @param array $attributeData |
||
42 | * @param string $property |
||
43 | * @return \PhpAbac\Model\Attribute |
||
44 | */ |
||
45 | 6 | private function getClassicAttribute($attributeData, $property) { |
|
46 | return |
||
47 | 6 | (new Attribute()) |
|
|
|||
48 | 6 | ->setName($attributeData['fields'][$property]['name']) |
|
49 | 6 | ->setType($attributeData['type']) |
|
50 | 6 | ->setProperty($property) |
|
51 | 6 | ->setSlug($this->slugify($attributeData['fields'][$property]['name'])) |
|
52 | ; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param array $attributeData |
||
57 | * @param string $key |
||
58 | * @return \PhpAbac\Model\EnvironmentAttribute |
||
59 | */ |
||
60 | 4 | private function getEnvironmentAttribute($attributeData, $key) { |
|
61 | return |
||
62 | 4 | (new EnvironmentAttribute()) |
|
63 | 4 | ->setName($attributeData[$key]['name']) |
|
64 | 4 | ->setType('environment') |
|
65 | 4 | ->setVariableName($attributeData[$key]['variable_name']) |
|
66 | 4 | ->setSlug($this->slugify($attributeData[$key]['name'])) |
|
67 | ; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param AbstractAttribute $attribute |
||
72 | * @param string $attributeType |
||
73 | * @param object $user |
||
74 | * @param object $object |
||
75 | * @return mixed |
||
76 | */ |
||
77 | 6 | public function retrieveAttribute(AbstractAttribute $attribute, $user = null, $object = null) |
|
78 | { |
||
79 | 6 | switch($attribute->getType()) { |
|
80 | 6 | case 'user': |
|
81 | 3 | return $this->retrieveClassicAttribute($attribute, $user); |
|
82 | 4 | case 'resource': |
|
83 | 3 | return $this->retrieveClassicAttribute($attribute, $object); |
|
84 | 2 | case 'environment': |
|
85 | 2 | return $this->retrieveEnvironmentAttribute($attribute); |
|
86 | } |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param Attribute $attribute |
||
91 | * @param object $object |
||
92 | * @return mixed |
||
93 | */ |
||
94 | 5 | private function retrieveClassicAttribute(Attribute $attribute, $object) |
|
95 | { |
||
96 | 5 | $propertyPath = explode('.', $attribute->getProperty()); |
|
97 | 5 | $propertyValue = $object; |
|
98 | 5 | foreach($propertyPath as $property) { |
|
99 | 5 | $getter = 'get'.ucfirst($property); |
|
100 | 5 | if(!method_exists($propertyValue, $getter)) { |
|
101 | throw new \InvalidArgumentException('There is no getter for the "'.$attribute->getProperty().'" attribute for object "'.get_class($propertyValue).'"'); |
||
102 | } |
||
103 | 5 | if (($propertyValue = $propertyValue->{$getter}()) === null) { |
|
104 | 5 | return null; |
|
105 | } |
||
106 | } |
||
107 | 5 | return $propertyValue; |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * |
||
112 | * @param \PhpAbac\Model\EnvironmentAttribute $attribute |
||
113 | * @return mixed |
||
114 | */ |
||
115 | 2 | private function retrieveEnvironmentAttribute(EnvironmentAttribute $attribute) { |
|
118 | |||
119 | /* |
||
120 | * @param string $name |
||
121 | * @return string |
||
122 | */ |
||
123 | 8 | public function slugify($name) |
|
138 | } |
||
139 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: