1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ma27\ApiKeyAuthenticationBundle\Annotation\Driver; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\Reader; |
6
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Model\User\ClassMetadata; |
7
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Model\User\ModelConfigurationDriverInterface; |
8
|
|
|
use ReflectionClass; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Annotation driver which parses the annotations of the user model instance. |
12
|
|
|
*/ |
13
|
|
|
final class AnnotationDriver implements ModelConfigurationDriverInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Reader |
17
|
|
|
*/ |
18
|
|
|
private $reader; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $userClass; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor. |
27
|
|
|
* |
28
|
|
|
* @param Reader $annotationReader |
29
|
|
|
* @param string $userClass |
30
|
|
|
*/ |
31
|
7 |
|
public function __construct(Reader $annotationReader, $userClass) |
32
|
|
|
{ |
33
|
7 |
|
$this->reader = $annotationReader; |
34
|
7 |
|
$this->userClass = (string) $userClass; |
35
|
7 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
* |
40
|
|
|
* @throws \LogicException If one of the annotations is missing |
41
|
|
|
* @throws \LogicException If one property has multiple "auth" annotations |
42
|
|
|
*/ |
43
|
7 |
|
public function getMetadataForUser() |
44
|
|
|
{ |
45
|
7 |
|
$reflection = new ReflectionClass($this->userClass); |
46
|
7 |
|
$properties = $reflection->getProperties(); |
47
|
7 |
|
$loginProperty = $passwordProperty = $apiKeyProperty = $lastActionProperty = null; |
48
|
|
|
|
49
|
7 |
|
foreach ($properties as $reflectionProperty) { |
50
|
7 |
|
foreach (array('login', 'password', 'apiKey', 'lastAction') as $annotation) { |
51
|
7 |
|
$class = sprintf('Ma27\\ApiKeyAuthenticationBundle\\Annotation\\%s', ucfirst($annotation)); |
52
|
7 |
|
$annotationObject = $this->reader->getPropertyAnnotation($reflectionProperty, $class); |
|
|
|
|
53
|
|
|
|
54
|
7 |
|
if ($annotationObject) { |
55
|
|
|
switch ($annotation) { |
56
|
7 |
|
case 'login': |
57
|
6 |
|
if (!empty($loginProperty)) { |
58
|
|
|
throw $this->createDuplicateAnnotationException(); |
59
|
|
|
} |
60
|
|
|
|
61
|
6 |
|
$loginProperty = $reflectionProperty; |
62
|
6 |
|
break; |
63
|
7 |
|
case 'password': |
64
|
6 |
|
if (!empty($passwordProperty)) { |
65
|
|
|
throw $this->createDuplicateAnnotationException(); |
66
|
|
|
} |
67
|
|
|
|
68
|
6 |
|
$passwordProperty = $reflectionProperty; |
69
|
6 |
|
break; |
70
|
7 |
|
case 'apiKey': |
71
|
7 |
|
if (!empty($apiKeyProperty)) { |
72
|
|
|
throw $this->createDuplicateAnnotationException(); |
73
|
|
|
} |
74
|
|
|
|
75
|
7 |
|
$apiKeyProperty = $reflectionProperty; |
76
|
7 |
|
break; |
77
|
|
|
case 'lastAction': |
78
|
|
|
if (!empty($lastActionProperty)) { |
79
|
|
|
throw $this->createDuplicateAnnotationException(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$lastActionProperty = $reflectionProperty; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if ($loginProperty |
86
|
7 |
|
&& $passwordProperty |
87
|
7 |
|
&& $apiKeyProperty |
88
|
7 |
|
&& $lastActionProperty |
89
|
7 |
|
) { |
90
|
|
|
break; |
91
|
|
|
} |
92
|
|
|
|
93
|
7 |
|
continue; |
94
|
|
|
} |
95
|
7 |
|
} |
96
|
7 |
|
} |
97
|
|
|
|
98
|
7 |
|
if (!$loginProperty || !$passwordProperty || !$apiKeyProperty) { |
99
|
1 |
|
throw new \LogicException(sprintf( |
100
|
1 |
|
'A user class must have a "%s", "%s", "%s" annotation!', |
101
|
1 |
|
'Login', |
102
|
1 |
|
'Password', |
103
|
|
|
'ApiKey' |
104
|
1 |
|
)); |
105
|
|
|
} |
106
|
|
|
|
107
|
6 |
|
return new ClassMetadata( |
108
|
6 |
|
$reflection, |
109
|
6 |
|
$this->userClass, |
110
|
|
|
array( |
|
|
|
|
111
|
6 |
|
ClassMetadata::LOGIN_PROPERTY => $loginProperty, |
112
|
6 |
|
ClassMetadata::PASSWORD_PROPERTY => $passwordProperty, |
113
|
6 |
|
ClassMetadata::API_KEY_PROPERTY => $apiKeyProperty, |
114
|
6 |
|
ClassMetadata::LAST_ACTION_PROPERTY => $lastActionProperty, |
115
|
|
|
) |
116
|
6 |
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Creates the exception when. |
121
|
|
|
* |
122
|
|
|
* @return \InvalidArgumentException |
123
|
|
|
*/ |
124
|
|
|
private function createDuplicateAnnotationException() |
125
|
|
|
{ |
126
|
|
|
return new \InvalidArgumentException('None of the Ma27\\ApiKeyAuthenticationBundle annotations can be declared twice!'); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
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.