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