src/ApiBundle/EventListener/BizRateLimitListener.php line 21

Open in your IDE?
  1. <?php
  2. namespace ApiBundle\EventListener;
  3. use ApiBundle\Event\ResourceEvent;
  4. use Codeages\Biz\Framework\Context\Biz;
  5. class BizRateLimitListener
  6. {
  7.     private $biz;
  8.     public function __construct(Biz $biz)
  9.     {
  10.         $this->biz $biz;
  11.     }
  12.     private $ruleMap = [
  13.         ['SmsCenter''post''register_sms_rate_limiter'],
  14.     ];
  15.     public function handle(ResourceEvent $event)
  16.     {
  17.         $request $event->getRequest();
  18.         foreach ($this->ruleMap as $rule) {
  19.             if ($this->isRateLimitApi($rule$event)) {
  20.                 $rateLimiter $this->getRateLimiter($rule[2]);
  21.                 $rateLimiter->handle($request);
  22.             }
  23.         }
  24.     }
  25.     private function isRateLimitApi($ruleResourceEvent $event)
  26.     {
  27.         $request $event->getRequest();
  28.         $resourceProxy $event->getResourceProxy();
  29.         $class get_class($resourceProxy->getResource());
  30.         $className $this->getClassName($class);
  31.         return === strcasecmp($rule[0], $className)
  32.             && === strcasecmp($rule[1], $request->getMethod());
  33.     }
  34.     private function getClassName($class)
  35.     {
  36.         $path explode('\\'$class);
  37.         return array_pop($path);
  38.     }
  39.     /**
  40.      * @return \AppBundle\Component\RateLimit\RateLimiterInterface
  41.      */
  42.     private function getRateLimiter($name)
  43.     {
  44.         return $this->biz[$name];
  45.     }
  46. }