99use Noxlogic \RateLimitBundle \Exception \RateLimitExceptionInterface ;
1010use Noxlogic \RateLimitBundle \LimitProcessorInterface ;
1111use Noxlogic \RateLimitBundle \Service \RateLimitService ;
12- use Noxlogic \RateLimitBundle \Util \AnnotationLimitProcessor ;
13- use Noxlogic \RateLimitBundle \Util \PathLimitProcessor ;
1412use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
1513use Symfony \Component \HttpFoundation \Request ;
1614use Symfony \Component \HttpFoundation \Response ;
@@ -31,23 +29,23 @@ class RateLimitAnnotationListener extends BaseListener
3129 protected $ rateLimitService ;
3230
3331 /**
34- * @var \Noxlogic\RateLimitBundle\Util\PathLimitProcessor
32+ * @var LimitProcessorInterface
3533 */
36- protected $ pathLimitProcessor ;
34+ protected $ limitProcessor ;
3735
3836 /**
3937 * @param EventDispatcherInterface $eventDispatcher
4038 * @param RateLimitService $rateLimitService
41- * @param PathLimitProcessor $pathLimitProcessor
39+ * @param LimitProcessorInterface $limitProcessor
4240 */
4341 public function __construct (
4442 EventDispatcherInterface $ eventDispatcher ,
4543 RateLimitService $ rateLimitService ,
46- PathLimitProcessor $ pathLimitProcessor
44+ LimitProcessorInterface $ limitProcessor
4745 ) {
4846 $ this ->eventDispatcher = $ eventDispatcher ;
4947 $ this ->rateLimitService = $ rateLimitService ;
50- $ this ->pathLimitProcessor = $ pathLimitProcessor ;
48+ $ this ->limitProcessor = $ limitProcessor ;
5149 }
5250
5351 /**
@@ -65,15 +63,7 @@ public function onKernelController(FilterControllerEvent $event)
6563 return ;
6664 }
6765
68- // Find the best match
69- $ annotations = $ event ->getRequest ()->attributes ->get ('_x-rate-limit ' , array ());
70-
71- $ limitProcessor = $ this ->pathLimitProcessor ;
72- if ($ annotations ) {
73- $ limitProcessor = new AnnotationLimitProcessor ($ annotations , $ event ->getController ());
74- }
75-
76- $ rateLimit = $ limitProcessor ->getRateLimit ($ event ->getRequest ());
66+ $ rateLimit = $ this ->limitProcessor ->getRateLimit ($ event ->getRequest ());
7767
7868 // Another treatment before applying RateLimit ?
7969 $ checkedRateLimitEvent = new CheckedRateLimitEvent ($ event ->getRequest (), $ rateLimit );
@@ -85,7 +75,7 @@ public function onKernelController(FilterControllerEvent $event)
8575 return ;
8676 }
8777
88- $ key = $ this ->getKey ($ limitProcessor , $ rateLimit , $ event-> getRequest () );
78+ $ key = $ this ->getKey ($ rateLimit , $ event );
8979
9080 $ rateLimitInfo = $ this ->rateLimitService ->getRateLimitInfo ($ key , $ rateLimit );
9181 if (!$ rateLimitInfo ) {
@@ -139,7 +129,7 @@ protected function findBestMethodMatch(Request $request, array $annotations)
139129
140130 // Empty array, check the path limits
141131 if (count ($ annotations ) == 0 ) {
142- return $ this ->pathLimitProcessor ->getRateLimit ($ request );
132+ return $ this ->limitProcessor ->getRateLimit ($ request );
143133 }
144134
145135 $ best_match = null ;
@@ -160,13 +150,14 @@ protected function findBestMethodMatch(Request $request, array $annotations)
160150 return $ best_match ;
161151 }
162152
163- private function getKey (LimitProcessorInterface $ limitProcessor , RateLimit $ rateLimit , Request $ request )
153+ private function getKey (RateLimit $ rateLimit , FilterControllerEvent $ event )
164154 {
165155 // Let listeners manipulate the key
156+ $ request = $ event ->getRequest ();
166157 $ keyEvent = new GenerateKeyEvent ($ request , '' , $ rateLimit ->getPayload ());
167158
168159 $ keyEvent ->addToKey (join ('. ' , $ rateLimit ->getMethods ()));
169- $ keyEvent ->addToKey ($ limitProcessor ->getRateLimitAlias ($ request ));
160+ $ keyEvent ->addToKey ($ this -> limitProcessor ->getRateLimitAlias ($ request, $ event -> getController () ));
170161
171162 $ this ->eventDispatcher ->dispatch (RateLimitEvents::GENERATE_KEY , $ keyEvent );
172163
0 commit comments