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