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