/**
  * @param AccessInterceptorInterface $proxy
  * @param EventManager               $eventManager
  */
 public function addEventManagement(AccessInterceptorInterface $proxy)
 {
     $proxy->setMethodPrefixInterceptor('find', function ($proxy, $instance, $method, $params, &$returnEarly) {
         $results = $this->eventManager->trigger('preFind', $instance);
         if ($results->stopped()) {
             $returnEarly = true;
             return $results->last();
         }
     });
     $proxy->setMethodSuffixInterceptor('find', function ($proxy, $instance, $method, $params, $returnValue, &$returnEarly) {
         $this->eventManager->trigger('postFind', $instance, ['row' => $returnValue]);
     });
     $proxy->setMethodPrefixInterceptor('insert', function ($proxy, $instance, $method, $params, &$returnEarly) {
         $returnEarly = true;
         $this->eventManager->trigger('preInsert', $instance, ['values' => &$params['values']]);
         return $instance->insert($params['values']);
     });
     $proxy->setMethodPrefixInterceptor('update', function ($proxy, $instance, $method, $params, &$returnEarly) {
         $returnEarly = true;
         $this->eventManager->trigger('preUpdate', $instance, ['values' => &$params['values']]);
         return $instance->update($params['values'], $params['id']);
     });
     $proxy->setMethodSuffixInterceptor('update', function ($proxy, $instance, $method, $params, $returnValue, &$returnEarly) {
         $this->eventManager->trigger('onSave', $instance, ['row' => $returnValue]);
     });
     $proxy->setMethodSuffixInterceptor('delete', function ($proxy, $instance, $method, $params, $returnValue, &$returnEarly) {
         $this->eventManager->trigger('onDelete', $instance);
     });
     $proxy->setMethodSuffixInterceptor('getQueryFindAll', function ($proxy, $instance, $method, $params, $returnValue, &$returnEarly) {
         $this->eventManager->trigger('postFindAll', $instance, ['qb' => $returnValue, 'criteria' => $params['criteria']]);
     });
 }
 /**
  * @param AccessInterceptorInterface $proxy
  * @param EventManager               $eventManager
  */
 public function addEventManagement(AccessInterceptorInterface $proxy)
 {
     $proxy->setMethodPrefixInterceptor('get', function ($proxy, $instance, $method, $params, &$returnEarly) {
         $returnEarly = true;
         try {
             $resource = $instance->get($params['id']);
             $this->triggerEvent('postDispatch', $instance, $resource);
         } catch (ResourceNotFoundException $e) {
             $instance->response()->status(404);
             //Not Found
         }
     });
     $proxy->setMethodPrefixInterceptor('getList', function ($proxy, $instance, $method, $params, &$returnEarly) {
         $returnEarly = true;
         try {
             $resources = $instance->getList();
             $this->triggerEvent('postDispatch', $instance, $resources);
         } catch (PageOutOfRangeException $e) {
             $instance->response()->status(404);
             //Not Found
         }
     });
     $proxy->setMethodPrefixInterceptor('post', function ($proxy, $instance, $method, $params, &$returnEarly) {
         $returnEarly = true;
         try {
             $resource = $instance->post();
             $this->triggerEvent('postDispatch', $instance, $resource);
         } catch (BadRequestParameters $e) {
             $this->handleBadRequest($instance);
         }
     });
     $proxy->setMethodPrefixInterceptor('put', function ($proxy, $instance, $method, $params, &$returnEarly) {
         $returnEarly = true;
         try {
             $resource = $instance->put($params['id']);
             $this->triggerEvent('postDispatch', $instance, $resource);
         } catch (ResourceNotFoundException $e) {
             $instance->response()->status(404);
             //Not Found
         } catch (BadRequestParameters $e) {
             $this->handleBadRequest($instance);
         }
     });
     $proxy->setMethodPrefixInterceptor('delete', function ($proxy, $instance, $method, $params, &$returnEarly) {
         $returnEarly = true;
         try {
             $instance->delete($params['id']);
         } catch (ResourceNotFoundException $e) {
             $instance->response()->status(404);
             //Not Found
         }
     });
 }
 protected function expectsThatProxyInitializes4SuffixInterceptors()
 {
     $this->proxy->expects($this->exactly(4))->method('setMethodSuffixInterceptor');
 }
 /**
  * @param AccessInterceptorInterface $proxy
  * @param string                     $method
  * @param AnnotationCollection       $annotations
  */
 private function registerSuffixInterceptor(AccessInterceptorInterface $proxy, $method, AnnotationCollection $annotations)
 {
     $cacheHandler = $this->cacheHandler;
     $proxy->setMethodSuffixInterceptor($method, function ($proxy, $instance, $method, $params, $returnValue, &$returnEarly) use($cacheHandler, $annotations) {
         $interception = new ProxyInterceptionSuffix($instance, $method, $params, $returnValue);
         if (!($result = $cacheHandler->interceptProxySuffix($annotations, $interception))) {
             return null;
         }
         $returnEarly = true;
         return $result;
     });
 }