1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ma27\ApiKeyAuthenticationBundle\Service\Mapping; |
4
|
|
|
|
5
|
|
|
use ReflectionProperty; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Metadata object that contains the necessary data of the user model. |
9
|
|
|
* |
10
|
|
|
* @internal This code is part of the internal API to gather the appropriate model information and shouldn't be used for else use-cases |
11
|
|
|
*/ |
12
|
|
|
class ClassMetadata |
13
|
|
|
{ |
14
|
|
|
const LOGIN_PROPERTY = 'login'; |
15
|
|
|
const PASSWORD_PROPERTY = 'password'; |
16
|
|
|
const API_KEY_PROPERTY = 'apiKey'; |
17
|
|
|
const LAST_ACTION_PROPERTY = 'lastAction'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var int[] |
21
|
|
|
*/ |
22
|
|
|
private static $requiredProperties = array( |
23
|
|
|
self::LOGIN_PROPERTY, |
24
|
|
|
self::PASSWORD_PROPERTY, |
25
|
|
|
self::API_KEY_PROPERTY, |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ReflectionProperty[] |
30
|
|
|
*/ |
31
|
|
|
private $properties = array(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string[] |
35
|
|
|
*/ |
36
|
|
|
private $lazyPropertyNameCache = array(); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string[] |
40
|
|
|
*/ |
41
|
|
|
private $lazyValueCache = array(); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor. |
45
|
|
|
* |
46
|
|
|
* @param ReflectionProperty|string[] $properties |
47
|
|
|
* |
48
|
|
|
* @throws \InvalidArgumentException If one necessary property is missing |
49
|
|
|
*/ |
50
|
20 |
|
public function __construct(array $properties) |
51
|
|
|
{ |
52
|
20 |
|
$this->properties = $properties; |
|
|
|
|
53
|
|
|
|
54
|
20 |
|
foreach (self::$requiredProperties as $property) { |
55
|
20 |
|
if (!isset($this->properties[$property])) { |
56
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
57
|
1 |
|
'Missing required property "%s"!', |
58
|
20 |
|
$property |
59
|
|
|
)); |
60
|
|
|
} |
61
|
|
|
} |
62
|
19 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Gets the value of the given property. |
66
|
|
|
* |
67
|
|
|
* @param object $user |
68
|
|
|
* @param string $property |
69
|
|
|
* @param bool $strict |
70
|
|
|
* |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
11 |
|
public function getPropertyValue($user, $property = self::LOGIN_PROPERTY, $strict = false) |
74
|
|
|
{ |
75
|
11 |
|
if ($this->checkProperty($property, $strict)) { |
76
|
11 |
|
$oid = spl_object_hash($user); |
77
|
11 |
|
if (null !== $cacheHit = $this->resolveCache($oid, $property)) { |
78
|
10 |
|
return $cacheHit; |
79
|
|
|
} |
80
|
|
|
|
81
|
11 |
|
if (is_string($this->properties[$property])) { |
82
|
6 |
|
$this->properties[$property] = new ReflectionProperty($user, $this->properties[$property]); |
83
|
|
|
} |
84
|
11 |
|
$this->properties[$property]->setAccessible(true); |
85
|
|
|
|
86
|
11 |
|
return $this->lazyValueCache[$oid][$property] = $this->properties[$property]->getValue($user); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Gets the name of a specific property by its metadata constant. |
92
|
|
|
* |
93
|
|
|
* @param string $property |
94
|
|
|
* @param bool $strict |
95
|
|
|
* |
96
|
|
|
* @return null|string |
97
|
|
|
*/ |
98
|
18 |
|
public function getPropertyName($property = self::LOGIN_PROPERTY, $strict = false) |
99
|
|
|
{ |
100
|
18 |
|
if ($this->checkProperty($property, $strict)) { |
101
|
18 |
|
if (is_string($this->properties[$property])) { |
102
|
9 |
|
return $this->properties[$property]; |
103
|
|
|
} |
104
|
12 |
|
if (isset($this->lazyPropertyNameCache[$property])) { |
105
|
6 |
|
return $this->lazyPropertyNameCache[$property]; |
106
|
|
|
} |
107
|
|
|
|
108
|
12 |
|
return $this->lazyPropertyNameCache[$property] = $this->properties[$property]->getName(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Modifies a property and clears the cache. |
114
|
|
|
* |
115
|
|
|
* @param object $user |
116
|
|
|
* @param mixed $newValue |
117
|
|
|
* @param string $property |
118
|
|
|
*/ |
119
|
11 |
|
public function modifyProperty($user, $newValue, $property = self::LOGIN_PROPERTY) |
120
|
|
|
{ |
121
|
11 |
|
$this->checkProperty($property, true); |
122
|
|
|
|
123
|
10 |
|
$propertyObject = $this->properties[$property]; |
124
|
10 |
|
if (is_string($propertyObject)) { |
125
|
4 |
|
$this->properties[$property] = $propertyObject = new ReflectionProperty($user, $propertyObject); |
126
|
|
|
} |
127
|
|
|
|
128
|
10 |
|
$propertyObject->setAccessible(true); |
129
|
10 |
|
$propertyObject->setValue($user, $newValue); |
130
|
|
|
|
131
|
10 |
|
$oid = spl_object_hash($user); |
132
|
10 |
|
if (!array_key_exists($oid, $this->lazyValueCache)) { |
133
|
8 |
|
$this->lazyValueCache[$oid] = array(); |
134
|
|
|
} |
135
|
|
|
|
136
|
10 |
|
$this->lazyValueCache[$oid][$property] = $newValue; |
137
|
10 |
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Validates a property. |
141
|
|
|
* |
142
|
|
|
* @param string $property |
143
|
|
|
* @param bool $strict |
144
|
|
|
* |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
19 |
|
private function checkProperty($property = self::LOGIN_PROPERTY, $strict = false) |
148
|
|
|
{ |
149
|
19 |
|
if (!isset($this->properties[$property])) { |
150
|
1 |
|
if ($strict) { |
151
|
1 |
|
throw new \LogicException(sprintf( |
152
|
1 |
|
'Cannot get property "%s"!', |
153
|
1 |
|
$property |
154
|
|
|
)); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
|
160
|
18 |
|
return true; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Resolves the lazy value cache. |
165
|
|
|
* |
166
|
|
|
* @param string $oid |
167
|
|
|
* @param string $property |
168
|
|
|
* |
169
|
|
|
* @return mixed|null |
170
|
|
|
*/ |
171
|
11 |
|
private function resolveCache($oid, $property) |
172
|
|
|
{ |
173
|
11 |
|
if (isset($this->lazyValueCache[$oid])) { |
174
|
10 |
|
if (isset($this->lazyValueCache[$oid][$property])) { |
175
|
10 |
|
return $this->lazyValueCache[$oid][$property]; |
176
|
|
|
} |
177
|
|
|
|
178
|
8 |
|
return; |
179
|
|
|
} |
180
|
|
|
|
181
|
3 |
|
$this->lazyValueCache[$oid] = array(); |
182
|
3 |
|
} |
183
|
|
|
} |
184
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..