-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathAnnotationHandler.php
More file actions
158 lines (127 loc) · 4.59 KB
/
AnnotationHandler.php
File metadata and controls
158 lines (127 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace Ibrows\Bundle\NewsletterBundle\Annotation\Wizard;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class AnnotationHandler
{
protected $router;
protected $validation;
protected $annotationBags = array();
protected $annotations = array();
protected $currentAnnotation;
protected $currentAnnotationKey;
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
public function handle(FilterControllerEvent $event, $annotationBags){
usort($annotationBags, function($a, $b){
return $a->getAnnotation()->getNumber() > $b->getAnnotation()->getNumber();
});
$this->annotationBags = array_values($annotationBags);
$annotations = array();
foreach($annotationBags as $annotationBag){
$annotations[] = $annotationBag->getAnnotation();
}
$this->annotations = $annotations;
$controllerArray = $event->getController();
$controller = $controllerArray[0];
$hasFoundAnyErrors = false;
$wizardValidation = true;
$hasFoundCurrentMethod = false;
/* @var $annotationBag AnnotationBag */
foreach($annotationBags as $annotationBag){
$annotation = $annotationBag->getAnnotation();
$validationMethodName = $annotation->getValidationMethod();
$validation = $controller->$validationMethodName($this);
if($validation instanceof Response){
$hasFoundAnyErrors = true;
}
if(!$wizardValidation instanceof Response && $validation instanceof Response && !$hasFoundCurrentMethod){
$wizardValidation = $validation;
}
$isValid = ($hasFoundAnyErrors OR $wizardValidation instanceof Response OR $validation instanceof Response) ? false : true;
$annotation->setIsValid($isValid);
if(true === $annotation->isCurrentMethod()){
$this->currentAnnotation = $annotation;
$hasFoundCurrentMethod = true;
}
}
$this->validation = $wizardValidation;
}
public function getLastValidAnnotation()
{
$lastAnnotation = null;
foreach($this->annotationBags as $annotationBag){
$annotation = $annotationBag->getAnnotation();
if(!$annotation->isValid()){
return $lastAnnotation;
}
$lastAnnotation = $annotation;
}
return null;
}
public function getAnnotations()
{
return $this->annotations;
}
/**
* @return true|Response
*/
public function getValidation()
{
return $this->validation;
}
/**
* @return string
* @throws \InvalidArgumentException
*/
public function getNextStepUrl()
{
$currentNumber = $this->currentAnnotation->getNumber();
foreach($this->annotations as $annotation){
if($annotation->getNumber() == $currentNumber+1){
return $this->getStepUrl($annotation);
}
}
throw new \InvalidArgumentException("No route found");
}
/**
* @return string
* @throws \InvalidArgumentException
*/
public function getCurrentStepUrl()
{
return $this->getStepUrl($this->currentAnnotation);
}
/**
* @return string
* @throws \InvalidArgumentException
*/
public function getPrevStepUrl()
{
$currentNumber = $this->currentAnnotation->getNumber();
foreach($this->annotations as $annotation){
if($annotation->getNumber() == $currentNumber-1){
return $this->getStepUrl($annotation);
}
}
throw new \InvalidArgumentException("No route found");
}
/**
* @param \Symfony\Component\Routing\Annotation\Route $annotation
* @return type
* @throws \InvalidArgumentException
*/
public function getStepUrl(Annotation $annotation)
{
foreach($annotation->getAnnotationBag()->getAnnotations() as $annotation){
if($annotation instanceof Route){
return $this->router->generate($annotation->getName());
}
}
throw new \InvalidArgumentException("No route found");
}
}