From e96cb175c7d17ccc3e8956406761312e5310c8bd Mon Sep 17 00:00:00 2001 From: Benjamin Paap Date: Wed, 14 Mar 2018 10:24:21 +0100 Subject: [PATCH] Add: Config fix and ResponseMatchEvent --- DependencyInjection/ApiSandboxExtension.php | 3 +- DependencyInjection/Configuration.php | 7 +- Event/ApiSandboxEvents.php | 10 ++- Event/ResponseMatchEvent.php | 74 +++++++++++++++++++++ EventListener/SandboxControllerListener.php | 15 ++++- Resources/config/services.yml | 1 + Service/ControllerService.php | 11 +-- 7 files changed, 107 insertions(+), 14 deletions(-) create mode 100644 Event/ResponseMatchEvent.php diff --git a/DependencyInjection/ApiSandboxExtension.php b/DependencyInjection/ApiSandboxExtension.php index fb319d4..265c618 100644 --- a/DependencyInjection/ApiSandboxExtension.php +++ b/DependencyInjection/ApiSandboxExtension.php @@ -20,7 +20,8 @@ public function load(array $configs, ContainerBuilder $container) $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); - $container->setParameter('bpa_apisandbox.response.force', $config['response']['force']); + $container->setParameter('bpa_apisandbox.response.force', $config['force_response']); + $container->setParameter('bpa_apisandbox.enabled', $config['enabled']); $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('services.yml'); diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index bddece7..51f9d7a 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -20,11 +20,8 @@ public function getConfigTreeBuilder() $rootNode ->children() - ->arrayNode('response') - ->children() - ->booleanNode('force')->defaultFalse()->end() - ->end() - ->end() + ->scalarNode('force_response')->defaultFalse()->end() + ->scalarNode('enabled')->defaultFalse()->end() ->end(); return $treeBuilder; diff --git a/Event/ApiSandboxEvents.php b/Event/ApiSandboxEvents.php index 4000512..7bce415 100644 --- a/Event/ApiSandboxEvents.php +++ b/Event/ApiSandboxEvents.php @@ -7,5 +7,13 @@ */ final class ApiSandboxEvents { - const ANNOTATIONS_LOADED = 'annotations.loaded'; + /** + * Fires after the annotations were loaded + */ + const ANNOTATIONS_LOADED = 'api.sandbox.annotations.loaded'; + + /** + * Fires when a matching response is found to further check the response in custom integrations + */ + const RESPONSE_MATCH = 'api.sandbox.response.match'; } diff --git a/Event/ResponseMatchEvent.php b/Event/ResponseMatchEvent.php new file mode 100644 index 0000000..a9ac512 --- /dev/null +++ b/Event/ResponseMatchEvent.php @@ -0,0 +1,74 @@ +response = $response; + } + + /** + * @return SandboxRequest + */ + public function getRequest() + { + return $this->request; + } + + /** + * @param SandboxRequest $request + * + * @return $this + */ + public function setRequest(SandboxRequest $request) + { + $this->request = $request; + + return $this; + } + + /** + * @return SandboxResponse + */ + public function getResponse() + { + return $this->response; + } + + /** + * @param SandboxResponse $response + * + * @return $this + */ + public function setResponse(SandboxResponse $response) + { + $this->response = $response; + + return $this; + } +} diff --git a/EventListener/SandboxControllerListener.php b/EventListener/SandboxControllerListener.php index d88c575..59708fc 100644 --- a/EventListener/SandboxControllerListener.php +++ b/EventListener/SandboxControllerListener.php @@ -15,13 +15,20 @@ class SandboxControllerListener */ private $service; + /** + * @var boolean + */ + private $enabled; + /** * SandboxControllerListener constructor. * + * @param boolean $enabled * @param ControllerService $service */ - public function __construct(ControllerService $service) + public function __construct($enabled, ControllerService $service) { + $this->enabled = $enabled; $this->service = $service; } @@ -32,8 +39,10 @@ public function onKernelController(FilterControllerEvent $event) { list($controller, $method) = $event->getController(); - $controller = $this->service->getSandboxController($controller, $method); + if ($this->enabled) { + $controller = $this->service->getSandboxController($controller, $method); - $event->setController($controller); + $event->setController($controller); + } } } diff --git a/Resources/config/services.yml b/Resources/config/services.yml index b4b0a8d..832e171 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -2,6 +2,7 @@ services: bpa_sandbox.controller_listener: class: 'Bpa\ApiSandboxBundle\EventListener\SandboxControllerListener' arguments: + - '%bpa_apisandbox.enabled%' - '@bpa_sandbox.controller_service' - '@request_stack' tags: diff --git a/Service/ControllerService.php b/Service/ControllerService.php index 06b80db..0847fd9 100644 --- a/Service/ControllerService.php +++ b/Service/ControllerService.php @@ -6,6 +6,7 @@ use Bpa\ApiSandboxBundle\Annotation\SandboxResponse; use Bpa\ApiSandboxBundle\Event\AnnotationEvent; use Bpa\ApiSandboxBundle\Event\ApiSandboxEvents; +use Bpa\ApiSandboxBundle\Event\ResponseMatchEvent; use Doctrine\Common\Annotations\AnnotationReader; use Symfony\Component\Config\FileLocatorInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -162,7 +163,7 @@ private function isResponseMatching(SandboxRequest $request, SandboxResponse $re if ($parameter->isRequired() && null === $value) { throw new HttpException( - Response::HTTP_INTERNAL_SERVER_ERROR, + Response::HTTP_BAD_REQUEST, sprintf('Parameter "%s" is missing.', $parameter->getName()) ); } @@ -173,10 +174,10 @@ private function isResponseMatching(SandboxRequest $request, SandboxResponse $re } } - if (null !== $format = $parameter->getFormat() && null !== $value) { + if ((null !== $format = $parameter->getFormat()) && null !== $value) { if (!preg_match('@'.$format.'@', $value)) { throw new HttpException( - Response::HTTP_INTERNAL_SERVER_ERROR, + Response::HTTP_BAD_REQUEST, sprintf( 'Value "%s" for parameter "%s" does not match format "%s"', $value, @@ -186,9 +187,11 @@ private function isResponseMatching(SandboxRequest $request, SandboxResponse $re ); } } - } + $event = new ResponseMatchEvent($request, $response); + $this->dispatcher->dispatch(ApiSandboxEvents::RESPONSE_MATCH, $event); + return true; }