Skip to content

Commit 80b56b2

Browse files
mpa12github-actions[bot]DenverCoder1
authored
feat: Added enumeration of HTTP response status codes (#303)
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Jonah Lawrence <jonah@freshidea.com>
1 parent a074167 commit 80b56b2

File tree

9 files changed

+136
-14
lines changed

9 files changed

+136
-14
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Make sure your request is meaningful and you have tested the app locally before
88

99
#### Requirements
1010

11-
- [PHP 7.4+](https://www.apachefriends.org/index.html)
11+
- [PHP 8.1+](https://www.apachefriends.org/index.html)
1212
- [Composer](https://getcomposer.org)
1313

1414
#### Linux

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@
1616
"classmap": [
1717
"src/models/",
1818
"src/views/",
19-
"src/controllers/"
19+
"src/controllers/",
20+
"src/enums/",
21+
"src/exceptions/",
22+
"src/interfaces/"
2023
]
2124
},
2225
"require": {
23-
"php": "^7.4|^8.0",
26+
"php": "^8.1",
2427
"vlucas/phpdotenv": "^5.3"
2528
},
2629
"require-dev": {
2730
"phpunit/phpunit": "^11"
2831
},
2932
"scripts": {
30-
"start": "php7 -S localhost:8000 -t src || php -S localhost:8000 -t src",
33+
"start": "php8 -S localhost:8000 -t src || php -S localhost:8000 -t src",
3134
"test": "./vendor/bin/phpunit --testdox tests",
3235
"format:check": "prettier --check *.md **/**/*.{php,md,js,css} --print-width 120",
3336
"format": "prettier --write *.md **/**/*.{php,md,js,css} --print-width 120"

src/controllers/RendererController.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@ class RendererController
2020
*/
2121
private $params;
2222

23+
/**
24+
* @var ResponseEnum $statusCode Response status code
25+
*/
26+
private ResponseEnum $statusCode = ResponseEnum::HTTP_OK;
27+
2328
/**
2429
* Construct RendererController
2530
*
2631
* @param array<string, string> $params request parameters
2732
*/
28-
public function __construct($params)
33+
public function __construct(array $params)
2934
{
3035
$this->params = $params;
3136

@@ -40,6 +45,10 @@ public function __construct($params)
4045
$this->model = new ErrorModel(__DIR__ . "/../templates/error.php", $error->getMessage());
4146
// create error rendering view
4247
$this->view = new ErrorView($this->model);
48+
49+
// set status code
50+
$this->statusCode =
51+
$error instanceof IStatusException ? $error->getStatus() : ResponseEnum::HTTP_INTERNAL_SERVER_ERROR;
4352
}
4453
}
4554

@@ -89,6 +98,9 @@ public function setHeaders(): void
8998

9099
// set cache headers
91100
$this->setCacheRefreshDaily();
101+
102+
// set status code
103+
http_response_code($this->statusCode->value);
92104
}
93105

94106
/**

src/enums/ResponseEnum.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/**
4+
* Enumeration of HTTP response status codes.
5+
*
6+
* This enum represents the standard HTTP response status codes
7+
* defined by the Internet Assigned Numbers Authority (IANA) in
8+
* the Hypertext Transfer Protocol (HTTP) status code registry.
9+
* Each status code is associated with an integer value and a
10+
* descriptive name.
11+
*
12+
* Usage example:
13+
* if (ResponseEnum::HTTP_OK->value === 200) {
14+
* echo "Request was successful.";
15+
* }
16+
*/
17+
enum ResponseEnum: int
18+
{
19+
// 1xx: Informational
20+
case HTTP_CONTINUE = 100;
21+
case HTTP_SWITCHING_PROTOCOLS = 101;
22+
case HTTP_PROCESSING = 102;
23+
24+
// 2xx: Success
25+
case HTTP_OK = 200;
26+
case HTTP_CREATED = 201;
27+
case HTTP_ACCEPTED = 202;
28+
case HTTP_NON_AUTHORITATIVE_INFORMATION = 203;
29+
case HTTP_NO_CONTENT = 204;
30+
case HTTP_RESET_CONTENT = 205;
31+
case HTTP_PARTIAL_CONTENT = 206;
32+
case HTTP_MULTI_STATUS = 207;
33+
case HTTP_ALREADY_REPORTED = 208;
34+
case HTTP_IM_USED = 226;
35+
36+
// 3xx: Redirection
37+
case HTTP_MULTIPLE_CHOICES = 300;
38+
case HTTP_MOVED_PERMANENTLY = 301;
39+
case HTTP_FOUND = 302;
40+
case HTTP_SEE_OTHER = 303;
41+
case HTTP_NOT_MODIFIED = 304;
42+
case HTTP_USE_PROXY = 305;
43+
case HTTP_SWITCH_PROXY = 306; // No longer used
44+
case HTTP_TEMPORARY_REDIRECT = 307;
45+
case HTTP_PERMANENT_REDIRECT = 308;
46+
47+
// 4xx: Client Error
48+
case HTTP_BAD_REQUEST = 400;
49+
case HTTP_UNAUTHORIZED = 401;
50+
case HTTP_PAYMENT_REQUIRED = 402;
51+
case HTTP_FORBIDDEN = 403;
52+
case HTTP_NOT_FOUND = 404;
53+
case HTTP_METHOD_NOT_ALLOWED = 405;
54+
case HTTP_NOT_ACCEPTABLE = 406;
55+
case HTTP_PROXY_AUTHENTICATION_REQUIRED = 407;
56+
case HTTP_REQUEST_TIMEOUT = 408;
57+
case HTTP_CONFLICT = 409;
58+
case HTTP_GONE = 410;
59+
case HTTP_LENGTH_REQUIRED = 411;
60+
case HTTP_PRECONDITION_FAILED = 412;
61+
case HTTP_PAYLOAD_TOO_LARGE = 413;
62+
case HTTP_URI_TOO_LONG = 414;
63+
case HTTP_UNSUPPORTED_MEDIA_TYPE = 415;
64+
case HTTP_RANGE_NOT_SATISFIABLE = 416;
65+
case HTTP_EXPECTATION_FAILED = 417;
66+
case HTTP_IM_A_TEAPOT = 418;
67+
case HTTP_MISDIRECTED_REQUEST = 421;
68+
case HTTP_UNPROCESSABLE_ENTITY = 422;
69+
case HTTP_LOCKED = 423;
70+
case HTTP_FAILED_DEPENDENCY = 424;
71+
case HTTP_TOO_EARLY = 425;
72+
case HTTP_UPGRADE_REQUIRED = 426;
73+
case HTTP_PRECONDITION_REQUIRED = 428;
74+
case HTTP_TOO_MANY_REQUESTS = 429;
75+
case HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431;
76+
case HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451;
77+
78+
// 5xx: Server Error
79+
case HTTP_INTERNAL_SERVER_ERROR = 500;
80+
case HTTP_NOT_IMPLEMENTED = 501;
81+
case HTTP_BAD_GATEWAY = 502;
82+
case HTTP_SERVICE_UNAVAILABLE = 503;
83+
case HTTP_GATEWAY_TIMEOUT = 504;
84+
case HTTP_HTTP_VERSION_NOT_SUPPORTED = 505;
85+
case HTTP_VARIANT_ALSO_NEGOTIATES = 506;
86+
case HTTP_INSUFFICIENT_STORAGE = 507;
87+
case HTTP_LOOP_DETECTED = 508;
88+
case HTTP_NOT_EXTENDED = 510;
89+
case HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511;
90+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
class UnprocessableEntityException extends InvalidArgumentException implements IStatusException
4+
{
5+
public function getStatus(): ResponseEnum
6+
{
7+
return ResponseEnum::HTTP_UNPROCESSABLE_ENTITY;
8+
}
9+
}

src/interfaces/IStatusException.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
interface IStatusException
4+
{
5+
public function getStatus(): ResponseEnum;
6+
}

src/models/ErrorModel.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
class ErrorModel
77
{
88
/** @var string $message Text to display */
9-
public $message;
9+
public string $message;
1010

1111
/** @var string $template Path to template file */
12-
public $template;
12+
public string $template;
1313

1414
/**
1515
* Construct ErrorModel
1616
*
1717
* @param string $message Text to display
1818
* @param string $template Path to the template file
1919
*/
20-
public function __construct($template, $message)
20+
public function __construct(string $template, string $message)
2121
{
2222
$this->message = $message;
2323
$this->template = $template;

src/models/GoogleFontConverter.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* Class for converting Google Fonts to base 64 for displaying through SVG image
57
*/
@@ -10,9 +12,9 @@ class GoogleFontConverter
1012
*
1113
* @param string $font Google Font to fetch
1214
* @param string $text Text to display in font
13-
* @return string|false The CSS for displaying the font
15+
* @return string The CSS for displaying the font
1416
*/
15-
public static function fetchFontCSS($font, $weight, $text)
17+
public static function fetchFontCSS($font, $weight, $text): string
1618
{
1719
$url =
1820
"https://fonts.googleapis.com/css2?" .
@@ -70,7 +72,7 @@ private static function curlGetContents($url): string
7072
$response = curl_exec($ch);
7173
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
7274
curl_close($ch);
73-
if ($httpCode != 200) {
75+
if ($httpCode != ResponseEnum::HTTP_OK->value) {
7476
throw new InvalidArgumentException("Failed to fetch Google Font from API.");
7577
}
7678
return $response;

src/models/RendererModel.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function __construct($template, $params)
122122
private function checkLines($lines)
123123
{
124124
if (!$lines) {
125-
throw new InvalidArgumentException("Lines parameter must be set.");
125+
throw new UnprocessableEntityException("Lines parameter must be set.");
126126
}
127127
if (strlen($this->separator) === 1) {
128128
$lines = rtrim($lines, $this->separator);
@@ -176,7 +176,7 @@ private function checkNumberPositive($num, $field)
176176
{
177177
$digits = intval(preg_replace("/[^0-9\-]/", "", $num));
178178
if ($digits <= 0) {
179-
throw new InvalidArgumentException("$field must be a positive number.");
179+
throw new UnprocessableEntityException("$field must be a positive number.");
180180
}
181181
return $digits;
182182
}
@@ -192,7 +192,7 @@ private function checkNumberNonNegative($num, $field)
192192
{
193193
$digits = intval(preg_replace("/[^0-9\-]/", "", $num));
194194
if ($digits < 0) {
195-
throw new InvalidArgumentException("$field must be a non-negative number.");
195+
throw new UnprocessableEntityException("$field must be a non-negative number.");
196196
}
197197
return $digits;
198198
}

0 commit comments

Comments
 (0)