Skip to content

Commit

Permalink
Breaking change: Remove prepared Response argument and add return type (
Browse files Browse the repository at this point in the history
Fix #58)
  • Loading branch information
cundd committed Apr 23, 2019
1 parent 23595da commit 064a641
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
5 changes: 2 additions & 3 deletions Classes/BootstrapDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,10 @@ private function configureDispatcher(ObjectManagerInterface $objectManager)
* Entry point for the PSR 7 middleware
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
*/
public function processRequest(ServerRequestInterface $request, ResponseInterface $response)
public function processRequest(ServerRequestInterface $request)
{
return $this->dispatcher->processRequest($request, $response);
return $this->dispatcher->processRequest($request);
}
}
27 changes: 10 additions & 17 deletions Classes/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Dispatcher implements SingletonInterface, DispatcherInterface
/**
* The shared instance
*
* @var \Cundd\Rest\Dispatcher
* @var Dispatcher
*/
protected static $sharedDispatcher;

Expand Down Expand Up @@ -76,47 +76,41 @@ public function __construct(
* Entry point for the PSR 7 middleware
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
* @throws \Exception
*/
public function processRequest(ServerRequestInterface $request, ResponseInterface $response)
public function processRequest(ServerRequestInterface $request): ResponseInterface
{
$this->requestFactory->registerCurrentRequest($request);
if (method_exists($this->objectManager, 'reassignRequest')) {
$this->objectManager->reassignRequest();
}

return $this->dispatch($this->requestFactory->getRequest(), $response);
return $this->dispatch($this->requestFactory->getRequest());
}

/**
* Dispatch the REST request
*
* @param RestRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
*/
public function dispatch(RestRequestInterface $request, ResponseInterface $response)
public function dispatch(RestRequestInterface $request): ResponseInterface
{
return $this->addCorsHeaders(
$request,
$this->addAdditionalHeaders($this->dispatchInternal($request, $response))
$this->addAdditionalHeaders($this->dispatchInternal($request))
);
}

/**
* Checks the cache for an entry for the current request and returns it, or calls the handler if nothing is found
*
* @param RestRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
*/
private function getCachedResponseOrCallHandler(
RestRequestInterface $request,
/** @noinspection PhpUnusedParameterInspection */
ResponseInterface $response
) {
private function getCachedResponseOrCallHandler(RestRequestInterface $request)
{
$cache = $this->objectManager->getCache($request->getResourceType());
$cachedResponse = $cache->getCachedValueForRequest($request);

Expand Down Expand Up @@ -197,7 +191,7 @@ public function greet(RestRequestInterface $request)
/**
* Returns the shared dispatcher instance
*
* @return \Cundd\Rest\Dispatcher
* @return Dispatcher
*/
public static function getSharedDispatcher()
{
Expand Down Expand Up @@ -252,10 +246,9 @@ private function addCorsHeaders(RestRequestInterface $request, ResponseInterface

/**
* @param RestRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
*/
private function dispatchInternal(RestRequestInterface $request, ResponseInterface $response): ResponseInterface
private function dispatchInternal(RestRequestInterface $request): ResponseInterface
{
$requestPath = $request->getPath();
if (!$requestPath || $requestPath === '/') {
Expand All @@ -277,7 +270,7 @@ private function dispatchInternal(RestRequestInterface $request, ResponseInterfa
return $this->responseFactory->createErrorResponse('Forbidden', 403, $request);
}

$newResponse = $this->getCachedResponseOrCallHandler($request, $response);
$newResponse = $this->getCachedResponseOrCallHandler($request);

$this->logger->logResponse(
'response: ' . $newResponse->getStatusCode(),
Expand Down
6 changes: 2 additions & 4 deletions Classes/Dispatcher/DispatcherInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@ interface DispatcherInterface
* Entry point for the PSR 7 middleware
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
*/
public function processRequest(ServerRequestInterface $request, ResponseInterface $response);
public function processRequest(ServerRequestInterface $request): ResponseInterface;

/**
* Dispatch the request
*
* @param RestRequestInterface $request
* @param ResponseInterface $response
* @return ResponseInterface
*/
public function dispatch(RestRequestInterface $request, ResponseInterface $response);
public function dispatch(RestRequestInterface $request): ResponseInterface;
}

0 comments on commit 064a641

Please sign in to comment.