This repository has been archived by the owner on Jul 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Decouples AcceptAndContentTypeMiddleware from ResponseManagerInterface #3
Open
TiMESPLiNTER
wants to merge
8
commits into
chubbyphp-legacy:master
Choose a base branch
from
TiMESPLiNTER:decouple-accept-and-content-type-middleware
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8c87a3c
Decouples AcceptAndContentTypeMiddleware from ResponseManagerInterface
TiMESPLiNTER 7e4e4fd
Add a bridge ResponseManager for the middleware
TiMESPLiNTER 5300379
CS
TiMESPLiNTER a24f97c
Static analysis
TiMESPLiNTER 12a7418
Update src/Manager/AcceptAndContentTypeMiddlewareResponseManager.php
TiMESPLiNTER b673a29
Update src/Manager/AcceptAndContentTypeMiddlewareResponseManager.php
TiMESPLiNTER 81b545a
Update src/Middleware/AcceptAndContentTypeMiddlewareResponseFactoryIn…
TiMESPLiNTER 69de4d9
Update src/Middleware/AcceptAndContentTypeMiddlewareResponseFactoryIn…
TiMESPLiNTER File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/Manager/AcceptAndContentTypeMiddlewareResponseManager.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Chubbyphp\ApiHttp\Manager; | ||
|
||
use Chubbyphp\ApiHttp\ApiProblem\ClientError\NotAcceptable; | ||
use Chubbyphp\ApiHttp\ApiProblem\ClientError\UnsupportedMediaType; | ||
use Chubbyphp\ApiHttp\Middleware\AcceptAndContentTypeMiddlewareResponseFactoryInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
final class AcceptAndContentTypeMiddlewareResponseManager implements AcceptAndContentTypeMiddlewareResponseFactoryInterface | ||
{ | ||
|
||
/** | ||
* @var ResponseManagerInterface | ||
*/ | ||
private $responseManager; | ||
|
||
public function __construct(ResponseManagerInterface $responseManager) | ||
{ | ||
$this->responseManager = $responseManager; | ||
} | ||
|
||
/** | ||
* @param string $accept | ||
* @param array<int, string> $acceptableMimeTypes | ||
* @param string $mimeType | ||
* @return ResponseInterface | ||
*/ | ||
public function createForNotAcceptable( | ||
string $accept, | ||
array $acceptableMimeTypes, | ||
string $mimeType | ||
): ResponseInterface { | ||
return $this->responseManager->createFromApiProblem( | ||
new NotAcceptable($accept, $acceptableMimeTypes), | ||
$mimeType | ||
); | ||
} | ||
|
||
/** | ||
* @param string $mediaType | ||
* @param array<int, string> $supportedMediaTypes | ||
* @param string $mimeType | ||
* @return ResponseInterface | ||
*/ | ||
public function createForUnsupportedMediaType( | ||
string $mediaType, | ||
array $supportedMediaTypes, | ||
string $mimeType | ||
): ResponseInterface { | ||
return $this->responseManager->createFromApiProblem( | ||
new UnsupportedMediaType($mediaType, $supportedMediaTypes), | ||
$mimeType | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,6 @@ | |
|
||
namespace Chubbyphp\ApiHttp\Middleware; | ||
|
||
use Chubbyphp\ApiHttp\ApiProblem\ClientError\NotAcceptable; | ||
use Chubbyphp\ApiHttp\ApiProblem\ClientError\UnsupportedMediaType; | ||
use Chubbyphp\ApiHttp\Manager\ResponseManagerInterface; | ||
use Chubbyphp\Negotiation\AcceptNegotiatorInterface; | ||
use Chubbyphp\Negotiation\ContentTypeNegotiatorInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
@@ -27,30 +24,28 @@ final class AcceptAndContentTypeMiddleware implements MiddlewareInterface | |
private $contentTypeNegotiator; | ||
|
||
/** | ||
* @var ResponseManagerInterface | ||
* @var AcceptAndContentTypeMiddlewareResponseFactoryInterface | ||
*/ | ||
private $responseManager; | ||
private $responseFactory; | ||
|
||
public function __construct( | ||
AcceptNegotiatorInterface $acceptNegotiator, | ||
ContentTypeNegotiatorInterface $contentTypeNegotiator, | ||
ResponseManagerInterface $responseManager | ||
AcceptAndContentTypeMiddlewareResponseFactoryInterface $responseFactory | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a BC, both arguments should be possible, if the old is given, @trigger_error with user deprecation |
||
) { | ||
$this->acceptNegotiator = $acceptNegotiator; | ||
$this->contentTypeNegotiator = $contentTypeNegotiator; | ||
$this->responseManager = $responseManager; | ||
$this->responseFactory = $responseFactory; | ||
} | ||
|
||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
if (null === $accept = $this->acceptNegotiator->negotiate($request)) { | ||
$supportedMediaTypes = $this->acceptNegotiator->getSupportedMediaTypes(); | ||
|
||
return $this->responseManager->createFromApiProblem( | ||
new NotAcceptable( | ||
$request->getHeaderLine('Accept'), | ||
$supportedMediaTypes | ||
), | ||
return $this->responseFactory->createForNotAcceptable( | ||
$request->getHeaderLine('Accept'), | ||
$supportedMediaTypes, | ||
$supportedMediaTypes[0] | ||
); | ||
} | ||
|
@@ -59,11 +54,9 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface | |
|
||
if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH'], true)) { | ||
if (null === $contentType = $this->contentTypeNegotiator->negotiate($request)) { | ||
return $this->responseManager->createFromApiProblem( | ||
new UnsupportedMediaType( | ||
$request->getHeaderLine('Content-Type'), | ||
$this->contentTypeNegotiator->getSupportedMediaTypes() | ||
), | ||
return $this->responseFactory->createForUnsupportedMediaType( | ||
$request->getHeaderLine('Content-Type'), | ||
$this->contentTypeNegotiator->getSupportedMediaTypes(), | ||
$accept->getValue() | ||
); | ||
} | ||
|
34 changes: 34 additions & 0 deletions
34
src/Middleware/AcceptAndContentTypeMiddlewareResponseFactoryInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Chubbyphp\ApiHttp\Middleware; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
|
||
interface AcceptAndContentTypeMiddlewareResponseFactoryInterface | ||
{ | ||
/** | ||
* @param string $accept | ||
* @param array<int, string> $acceptableMimeTypes | ||
* @param string $mimeType | ||
* @return ResponseInterface | ||
*/ | ||
public function createForNotAcceptable( | ||
string $accept, | ||
array $acceptableMimeTypes, | ||
string $mimeType | ||
): ResponseInterface; | ||
|
||
/** | ||
* @param string $mediaType | ||
* @param array<int, string> $supportedMediaTypes | ||
* @param string $mimeType | ||
* @return ResponseInterface | ||
*/ | ||
public function createForUnsupportedMediaType( | ||
string $mediaType, | ||
array $supportedMediaTypes, | ||
string $mimeType | ||
): ResponseInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useless phpdoc, its already typesafe by design