Skip to content

Commit

Permalink
Add: Config fix and ResponseMatchEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminPaap committed Mar 14, 2018
1 parent 5dde418 commit e96cb17
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 14 deletions.
3 changes: 2 additions & 1 deletion DependencyInjection/ApiSandboxExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
7 changes: 2 additions & 5 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 9 additions & 1 deletion Event/ApiSandboxEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
74 changes: 74 additions & 0 deletions Event/ResponseMatchEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Bpa\ApiSandboxBundle\Event;

use Bpa\ApiSandboxBundle\Annotation\SandboxRequest;
use Bpa\ApiSandboxBundle\Annotation\SandboxResponse;
use Symfony\Component\EventDispatcher\Event;

/**
* Event which holds a matched response
*/
class ResponseMatchEvent extends Event
{
/**
* @var SandboxRequest
*/
private $request;

/**
* @var SandboxResponse
*/
private $response;

/**
* ResponseEvent constructor.
*
* @param SandboxRequest $request
* @param SandboxResponse $response
*/
public function __construct(SandboxRequest $request, SandboxResponse $response)
{
$this->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;
}
}
15 changes: 12 additions & 3 deletions EventListener/SandboxControllerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
}
}
}
1 change: 1 addition & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 7 additions & 4 deletions Service/ControllerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
);
}
Expand All @@ -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,
Expand All @@ -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;
}

Expand Down

0 comments on commit e96cb17

Please sign in to comment.