|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ma27\ApiKeyAuthenticationBundle\Service\Mapping\Driver; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
6
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Service\Mapping\ClassMetadata; |
|
7
|
|
|
use ReflectionClass; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Annotation driver which parses the annotations of the user model instance. |
|
11
|
|
|
* |
|
12
|
|
|
* @internal This code is part of the internal API to gather the appropriate model information and shouldn't be used for else use-cases |
|
13
|
|
|
*/ |
|
14
|
|
|
final class AnnotationDriver implements ModelConfigurationDriverInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Reader |
|
18
|
|
|
*/ |
|
19
|
|
|
private $reader; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private $userClass; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param Reader $annotationReader |
|
30
|
|
|
* @param string $userClass |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(Reader $annotationReader, $userClass) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->reader = $annotationReader; |
|
35
|
|
|
$this->userClass = (string) $userClass; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
* |
|
41
|
|
|
* @throws \LogicException If one of the annotations is missing |
|
42
|
|
|
* @throws \LogicException If one property has multiple "auth" annotations |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getMetadataForUser() |
|
45
|
|
|
{ |
|
46
|
|
|
$reflection = new ReflectionClass($this->userClass); |
|
47
|
|
|
$properties = $reflection->getProperties(); |
|
48
|
|
|
$loginProperty = $passwordProperty = $apiKeyProperty = $lastActionProperty = null; |
|
49
|
|
|
|
|
50
|
|
|
foreach ($properties as $reflectionProperty) { |
|
51
|
|
|
foreach (array('login', 'password', 'apiKey', 'lastAction') as $annotation) { |
|
52
|
|
|
$class = sprintf('Ma27\\ApiKeyAuthenticationBundle\\Annotation\\%s', ucfirst($annotation)); |
|
53
|
|
|
$annotationObject = $this->reader->getPropertyAnnotation($reflectionProperty, $class); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
if ($annotationObject) { |
|
56
|
|
|
switch ($annotation) { |
|
57
|
|
|
case 'login': |
|
58
|
|
|
$this->assertUnique($loginProperty); |
|
59
|
|
|
$loginProperty = $reflectionProperty; |
|
60
|
|
|
break; |
|
61
|
|
|
case 'password': |
|
62
|
|
|
$this->assertUnique($passwordProperty); |
|
63
|
|
|
$passwordProperty = $reflectionProperty; |
|
64
|
|
|
break; |
|
65
|
|
|
case 'apiKey': |
|
66
|
|
|
$this->assertUnique($apiKeyProperty); |
|
67
|
|
|
$apiKeyProperty = $reflectionProperty; |
|
68
|
|
|
break; |
|
69
|
|
|
case 'lastAction': |
|
70
|
|
|
$this->assertUnique($lastActionProperty); |
|
71
|
|
|
$lastActionProperty = $reflectionProperty; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if ($loginProperty && $passwordProperty && $apiKeyProperty && $lastActionProperty) { |
|
75
|
|
|
break; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
continue; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (!$loginProperty || !$passwordProperty || !$apiKeyProperty) { |
|
84
|
|
|
throw new \LogicException(sprintf( |
|
85
|
|
|
'A user class must have a "%s", "%s", "%s" annotation!', |
|
86
|
|
|
'Login', |
|
87
|
|
|
'Password', |
|
88
|
|
|
'ApiKey' |
|
89
|
|
|
)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return array( |
|
|
|
|
|
|
93
|
|
|
ClassMetadata::LOGIN_PROPERTY => $loginProperty, |
|
94
|
|
|
ClassMetadata::PASSWORD_PROPERTY => $passwordProperty, |
|
95
|
|
|
ClassMetadata::API_KEY_PROPERTY => $apiKeyProperty, |
|
96
|
|
|
ClassMetadata::LAST_ACTION_PROPERTY => $lastActionProperty, |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Checks whether a property is already set. |
|
102
|
|
|
* |
|
103
|
|
|
* @param \ReflectionProperty $property |
|
104
|
|
|
*/ |
|
105
|
|
|
private function assertUnique(\ReflectionProperty $property = null) |
|
106
|
|
|
{ |
|
107
|
|
|
if (!empty($property)) { |
|
108
|
|
|
throw $this->createDuplicateAnnotationException(); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Creates the exception when. |
|
114
|
|
|
* |
|
115
|
|
|
* @return \InvalidArgumentException |
|
116
|
|
|
*/ |
|
117
|
|
|
private function createDuplicateAnnotationException() |
|
118
|
|
|
{ |
|
119
|
|
|
return new \InvalidArgumentException('None of the Ma27\\ApiKeyAuthenticationBundle annotations can be declared twice!'); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.