Skip to content

Commit

Permalink
Add request as parameter for response constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Kolb committed Nov 19, 2021
1 parent 89f85f8 commit cbcaf99
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 10 deletions.
10 changes: 5 additions & 5 deletions docs/response-constructor.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This is the interface for the response constructor:
interface ResponseConstructorInterface
{
/** @param mixed $result */
public function constructResponse($result): Response;
public function constructResponse($result, Request $request): Response;
}
```

Expand All @@ -17,14 +17,14 @@ interface ResponseConstructorInterface
Most of the time the result will be an object or array and be converted into JSON throught the JSONResponseConstructor. Obviously it needs the custom normalizers for the values objects to be able to do so.

```php
final class JsonResponseConstructor implements ResponseConstructorInterface
final class SerializerJsonResponseConstructor implements ResponseConstructorInterface
{
public function __construct(
private SerializerInterface $serializer,
) {
}

public function constructResponse($data): JsonResponse
public function constructResponse($data, Request $request): JsonResponse
{
return new JsonResponse(
$this->serializer->serialize($data),
Expand All @@ -44,7 +44,7 @@ A file should be returned as a binary and not as JSON, so we would need a custom
final class FileResponseConstructor implements ResponseConstructorInterface
{
/** @param File $data */
public function constructResponse($data): Response
public function constructResponse($data, Request $request): Response
{
return FileManagementHelper::binaryResponse(
$data->fileContent,
Expand Down Expand Up @@ -107,7 +107,7 @@ Such a callable would be send to a simple streamed response constructor:
final class StreamedResponseConstructor implements ResponseConstructorInterface
{
/** @param callable $data */
public function constructResponse($data): Response
public function constructResponse($data, Request $request): Response
{
return new StreamedResponse($data);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/CommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,6 @@ public function handle(
// Construct and return response
$responseConstructor = $this->serviceMap->getResponseConstructor($configuration, $this->defaultResponseConstructorClass);

return $responseConstructor->constructResponse(null);
return $responseConstructor->constructResponse(null, $request);
}
}
2 changes: 1 addition & 1 deletion src/Controller/QueryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,6 @@ public function handle(
// Construct and return response
$responseConstructor = $this->serviceMap->getResponseConstructor($configuration, $this->defaultResponseConstructorClass);

return $responseConstructor->constructResponse($result);
return $responseConstructor->constructResponse($result, $request);
}
}
3 changes: 2 additions & 1 deletion src/ResponseConstructor/EmptyJsonResponseConstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
namespace DigitalCraftsman\CQRS\ResponseConstructor;

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

final class EmptyJsonResponseConstructor implements ResponseConstructorInterface
{
public function constructResponse($data): JsonResponse
public function constructResponse($data, Request $request): JsonResponse
{
return new JsonResponse('', Response::HTTP_NO_CONTENT, [], true);
}
Expand Down
3 changes: 2 additions & 1 deletion src/ResponseConstructor/EmptyResponseConstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

namespace DigitalCraftsman\CQRS\ResponseConstructor;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

final class EmptyResponseConstructor implements ResponseConstructorInterface
{
public function constructResponse($data): Response
public function constructResponse($data, Request $request): Response
{
return new Response(null, Response::HTTP_NO_CONTENT);
}
Expand Down
3 changes: 2 additions & 1 deletion src/ResponseConstructor/ResponseConstructorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

namespace DigitalCraftsman\CQRS\ResponseConstructor;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

interface ResponseConstructorInterface
{
/** @param ?mixed $data */
public function constructResponse($data): Response;
public function constructResponse($data, Request $request): Response;
}

0 comments on commit cbcaf99

Please sign in to comment.