Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| ResponseFactory | |
100.00% |
7 / 7 |
|
100.00% |
7 / 7 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createResponse | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createResponseFromHtml | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createResponseFromText | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createResponseNoContent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createResponseFromPayload | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createResponseRedirect | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * This file is part of php-fast-forward/http-factory. |
| 7 | * |
| 8 | * This source file is subject to the license bundled |
| 9 | * with this source code in the file LICENSE. |
| 10 | * |
| 11 | * @copyright Copyright (c) 2025-2026 Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 12 | * @license https://opensource.org/licenses/MIT MIT License |
| 13 | * |
| 14 | * @see https://github.com/php-fast-forward/http-factory |
| 15 | * @see https://github.com/php-fast-forward |
| 16 | * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 17 | */ |
| 18 | |
| 19 | namespace FastForward\Http\Message\Factory; |
| 20 | |
| 21 | use FastForward\Http\Message\EmptyResponse; |
| 22 | use FastForward\Http\Message\HtmlResponse; |
| 23 | use FastForward\Http\Message\JsonResponse; |
| 24 | use FastForward\Http\Message\PayloadResponseInterface; |
| 25 | use FastForward\Http\Message\RedirectResponse; |
| 26 | use FastForward\Http\Message\TextResponse; |
| 27 | use Psr\Http\Message\ResponseFactoryInterface as PsrResponseFactoryInterface; |
| 28 | use Psr\Http\Message\ResponseInterface; |
| 29 | use Psr\Http\Message\UriInterface; |
| 30 | |
| 31 | /** |
| 32 | * Class ResponseFactory. |
| 33 | * |
| 34 | * Factory for generating different types of HTTP responses. |
| 35 | * This class encapsulates a PSR-17 response factory and provides |
| 36 | * convenient methods for producing responses with content, payloads, redirects, or no content. |
| 37 | */ |
| 38 | class ResponseFactory implements ResponseFactoryInterface |
| 39 | { |
| 40 | /** |
| 41 | * Constructs the ResponseFactory. |
| 42 | * |
| 43 | * @param PsrResponseFactoryInterface $responseFactory the underlying PSR-17 response factory implementation |
| 44 | */ |
| 45 | public function __construct( |
| 46 | private PsrResponseFactoryInterface $responseFactory, |
| 47 | ) {} |
| 48 | |
| 49 | /** |
| 50 | * Creates a standard HTTP response. |
| 51 | * |
| 52 | * @param int $code The HTTP status code. Defaults to 200 (OK). |
| 53 | * @param string $reasonPhrase optional reason phrase |
| 54 | * |
| 55 | * @return ResponseInterface the generated response |
| 56 | */ |
| 57 | public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface |
| 58 | { |
| 59 | return $this->responseFactory->createResponse($code, $reasonPhrase); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Creates an HTTP response containing HTML content. |
| 64 | * |
| 65 | * The response SHALL have 'Content-Type: text/html' set automatically. |
| 66 | * |
| 67 | * @param string $html the HTML content to include in the response body |
| 68 | * |
| 69 | * @return ResponseInterface the generated HTML response |
| 70 | */ |
| 71 | public function createResponseFromHtml(string $html): ResponseInterface |
| 72 | { |
| 73 | return new HtmlResponse($html); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Creates an HTTP response containing plain text content. |
| 78 | * |
| 79 | * The response SHALL have 'Content-Type: text/plain' set automatically. |
| 80 | * |
| 81 | * @param string $text the plain text content to include in the response body |
| 82 | * |
| 83 | * @return ResponseInterface the generated plain text response |
| 84 | */ |
| 85 | public function createResponseFromText(string $text): ResponseInterface |
| 86 | { |
| 87 | return new TextResponse($text); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Creates an HTTP 204 No Content response. |
| 92 | * |
| 93 | * This response SHALL contain no body and have status code 204. |
| 94 | * |
| 95 | * @param array<string, string|string[]> $headers optional headers to include |
| 96 | * @param array $headers |
| 97 | * |
| 98 | * @return ResponseInterface the generated no content response |
| 99 | */ |
| 100 | public function createResponseNoContent(array $headers = []): ResponseInterface |
| 101 | { |
| 102 | return new EmptyResponse($headers); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Creates an HTTP response containing a JSON-encoded payload. |
| 107 | * |
| 108 | * The response SHALL have 'Content-Type: application/json' set automatically. |
| 109 | * |
| 110 | * @param array $payload the payload to encode as JSON |
| 111 | * |
| 112 | * @return PayloadResponseInterface the generated JSON response |
| 113 | */ |
| 114 | public function createResponseFromPayload(array $payload): PayloadResponseInterface |
| 115 | { |
| 116 | return new JsonResponse($payload); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Creates an HTTP redirect response. |
| 121 | * |
| 122 | * The response SHALL include a 'Location' header and appropriate status code. |
| 123 | * By default, a temporary (302) redirect is issued unless $permanent is true. |
| 124 | * |
| 125 | * @param string|UriInterface $uri the target location for the redirect |
| 126 | * @param bool $permanent whether to issue a permanent (301) redirect |
| 127 | * @param array<string, string|string[]> $headers optional additional headers |
| 128 | * @param array $headers |
| 129 | * |
| 130 | * @return ResponseInterface the generated redirect response |
| 131 | */ |
| 132 | public function createResponseRedirect( |
| 133 | string|UriInterface $uri, |
| 134 | bool $permanent = false, |
| 135 | array $headers = [], |
| 136 | ): ResponseInterface { |
| 137 | return new RedirectResponse($uri, $permanent, $headers); |
| 138 | } |
| 139 | } |