Use HTTP code enums instead of ints

This commit is contained in:
Alex Cabal 2024-11-20 14:52:05 -06:00
parent 3f822b85c3
commit 3050ab7219
28 changed files with 56 additions and 56 deletions

View file

@ -11,7 +11,7 @@ try{
$_SESSION['subscription-confirmed'] = $subscription->UserId;
}
http_response_code(303);
http_response_code(Enums\HttpCode::SeeOther->value);
header('Location: ' . $subscription->Url);
}
catch(Exceptions\NewsletterSubscriptionNotFoundException){

View file

@ -17,7 +17,7 @@ catch(Exceptions\NewsletterSubscriptionNotFoundException){
Template::Emit404();
}
else{
http_response_code(404);
http_response_code(Enums\HttpCode::NotFound->value);
exit();
}
}

View file

@ -34,7 +34,7 @@ try{
if($created){
// HTTP 201 Created
http_response_code(201);
http_response_code(Enums\HttpCode::Created->value);
}
}
catch(Exceptions\AppException){

View file

@ -9,7 +9,7 @@ $subscription = $_SESSION['subscription'] ?? new NewsletterSubscription();
$exception = $_SESSION['exception'] ?? null;
if($exception){
http_response_code(422);
http_response_code(Enums\HttpCode::UnprocessableContent->value);
session_unset();
}

View file

@ -14,7 +14,7 @@ try{
if(HttpInput::Str(POST, 'automationtest')){
// A bot filled out this form field, which should always be empty. Pretend like we succeeded.
if($requestType == Enums\HttpRequestType::Web){
http_response_code(303);
http_response_code(Enums\HttpCode::SeeOther->value);
$uuid = Uuid::uuid4();
$subscription->User = new User();
$subscription->User->Uuid = $uuid->toString();
@ -23,7 +23,7 @@ try{
}
else{
// Access via Enums\HttpRequestType::Rest api; 201 CREATED with location
http_response_code(201);
http_response_code(Enums\HttpCode::Created->value);
header('Location: /newsletter/subscriptions/success');
}
@ -43,13 +43,13 @@ try{
session_unset();
if($requestType == Enums\HttpRequestType::Web){
http_response_code(303);
http_response_code(Enums\HttpCode::SeeOther->value);
$_SESSION['is-subscription-created'] = $subscription->UserId;
header('Location: /newsletter/subscriptions/success');
}
else{
// Access via Enums\HttpRequestType::Rest api; 201 CREATED with location
http_response_code(201);
http_response_code(Enums\HttpCode::Created->value);
header('Location: /newsletter/subscriptions/success');
}
}
@ -62,7 +62,7 @@ catch(Exceptions\NewsletterSubscriptionExistsException){
$subscription->IsConfirmed = $existingSubscription->IsConfirmed;
$subscription->Save();
http_response_code(303);
http_response_code(Enums\HttpCode::SeeOther->value);
if(!$subscription->IsConfirmed){
// Don't re-send the email after all, to prevent spam
@ -77,7 +77,7 @@ catch(Exceptions\NewsletterSubscriptionExistsException){
}
else{
// Access via Enums\HttpRequestType::Rest api; 409 CONFLICT
http_response_code(409);
http_response_code(Enums\HttpCode::Conflict->value);
}
}
catch(Exceptions\InvalidNewsletterSubscription $ex){
@ -86,11 +86,11 @@ catch(Exceptions\InvalidNewsletterSubscription $ex){
$_SESSION['exception'] = $ex;
// Access via form; 303 redirect to the form, which will emit a 422 Unprocessable Entity
http_response_code(303);
http_response_code(Enums\HttpCode::SeeOther->value);
header('Location: /newsletter/subscriptions/new');
}
else{
// Access via Enums\HttpRequestType::Rest api; 422 Unprocessable Entity
http_response_code(422);
http_response_code(Enums\HttpCode::UnprocessableContent->value);
}
}

View file

@ -16,7 +16,7 @@ if(isset($_SESSION['is-subscription-created'])){
if($created){
// HTTP 201 Created
http_response_code(201);
http_response_code(Enums\HttpCode::Created->value);
}
?><?= Template::Header(['title' => 'Your subscription to the Standard Ebooks newsletter', 'highlight' => 'newsletter', 'description' => 'Your subscription to the Standard Ebooks newsletter.']) ?>