Type tweaks for exceptions and some validation

This commit is contained in:
Alex Cabal 2024-04-25 20:14:34 -05:00
parent 5b3f8f7b77
commit 7eaa400ae3
13 changed files with 49 additions and 37 deletions

View file

@ -84,7 +84,7 @@ class Artist extends Accessor{
$now = new DateTimeImmutable(); $now = new DateTimeImmutable();
$thisYear = intval($now->format('Y')); $thisYear = intval($now->format('Y'));
$error = new Exceptions\ValidationException(); $error = new Exceptions\InvalidArtistException();
if($this->Name === null || $this->Name == ''){ if($this->Name === null || $this->Name == ''){
$error->Add(new Exceptions\ArtistNameRequiredException()); $error->Add(new Exceptions\ArtistNameRequiredException());

View file

@ -321,10 +321,10 @@ class Artwork extends Accessor{
protected function Validate(?string $imagePath = null, bool $isImageRequired = true): void{ protected function Validate(?string $imagePath = null, bool $isImageRequired = true): void{
$now = new DateTimeImmutable(); $now = new DateTimeImmutable();
$thisYear = intval($now->format('Y')); $thisYear = intval($now->format('Y'));
$error = new Exceptions\ValidationException(); $error = new Exceptions\InvalidArtworkException();
if($this->Artist === null){ if($this->Artist === null){
$error->Add(new Exceptions\InvalidArtworkException()); $error->Add(new Exceptions\InvalidArtistException());
} }
try{ try{

View file

@ -27,7 +27,7 @@ class ArtworkTag extends Tag{
// METHODS // METHODS
// ******* // *******
public function Validate(): void{ public function Validate(): void{
$error = new Exceptions\ValidationException(); $error = new Exceptions\InvalidArtworkTagException($this->Name);
$this->Name = mb_strtolower(trim($this->Name)); $this->Name = mb_strtolower(trim($this->Name));
// Collapse spaces into one // Collapse spaces into one

View file

@ -0,0 +1,6 @@
<?
namespace Exceptions;
class InvalidArtistException extends ValidationException{
protected $message = 'Artist is invalid.';
}

View file

@ -1,6 +1,6 @@
<? <?
namespace Exceptions; namespace Exceptions;
class InvalidArtworkException extends AppException{ class InvalidArtworkException extends ValidationException{
protected $message = 'Artwork is invalid.'; protected $message = 'Artwork is invalid.';
} }

View file

@ -1,5 +1,12 @@
<? <?
namespace Exceptions; namespace Exceptions;
class InvalidArtworkTagException extends AppException{ class InvalidArtworkTagException extends ValidationException{
protected $message = 'Artwork tag is invalid.';
public function __construct(?string $tagName){
if($tagName !== null && trim($tagName) != ''){
$this->message = 'Artwork tag ' . $tagName . ' is invalid.';
}
}
} }

View file

@ -0,0 +1,6 @@
<?
namespace Exceptions;
class InvalidNewsletterSubscription extends ValidationException{
protected $message = 'Newsletter subscription is invalid.';
}

View file

@ -0,0 +1,6 @@
<?
namespace Exceptions;
class InvalidPollVoteException extends ValidationException{
protected $message = 'Vote is invalid.';
}

View file

@ -31,8 +31,8 @@ class NewsletterSubscription extends Accessor{
// METHODS // METHODS
// ******* // *******
public function Create(): void{ public function Create(?string $expectedCaptcha = null, ?string $receivedCaptcha = null): void{
$this->Validate(); $this->Validate($expectedCaptcha, $receivedCaptcha);
// Do we need to create a user? // Do we need to create a user?
try{ try{
@ -111,8 +111,8 @@ class NewsletterSubscription extends Accessor{
', [$this->UserId]); ', [$this->UserId]);
} }
public function Validate(): void{ public function Validate(?string $expectedCaptcha = null, ?string $receivedCaptcha = null): void{
$error = new Exceptions\ValidationException(); $error = new Exceptions\InvalidNewsletterSubscription();
if($this->User === null || $this->User->Email == '' || !filter_var($this->User->Email, FILTER_VALIDATE_EMAIL)){ if($this->User === null || $this->User->Email == '' || !filter_var($this->User->Email, FILTER_VALIDATE_EMAIL)){
$error->Add(new Exceptions\InvalidEmailException()); $error->Add(new Exceptions\InvalidEmailException());
@ -122,6 +122,12 @@ class NewsletterSubscription extends Accessor{
$error->Add(new Exceptions\NewsletterRequiredException()); $error->Add(new Exceptions\NewsletterRequiredException());
} }
if($expectedCaptcha !== null){
if($expectedCaptcha === '' || mb_strtolower($expectedCaptcha) !== mb_strtolower($receivedCaptcha ?? '')){
$error->Add(new Exceptions\InvalidCaptchaException());
}
}
if($error->HasExceptions){ if($error->HasExceptions){
throw $error; throw $error;
} }

View file

@ -7,7 +7,7 @@ use Safe\DateTimeImmutable;
* @property string $Url * @property string $Url
*/ */
class PollVote extends Accessor{ class PollVote extends Accessor{
public int $UserId; public ?int $UserId = null;
public DateTimeImmutable $Created; public DateTimeImmutable $Created;
public ?int $PollItemId = null; public ?int $PollItemId = null;
protected ?User $_User = null; protected ?User $_User = null;
@ -33,7 +33,7 @@ class PollVote extends Accessor{
// ******* // *******
protected function Validate(): void{ protected function Validate(): void{
$error = new Exceptions\ValidationException(); $error = new Exceptions\InvalidPollVoteException();
if($this->User === null){ if($this->User === null){
$error->Add(new Exceptions\UserNotFoundException()); $error->Add(new Exceptions\UserNotFoundException());

View file

@ -38,26 +38,10 @@ try{
$subscription->IsSubscribedToNewsletter = HttpInput::Bool(POST, 'issubscribedtonewsletter') ?? false; $subscription->IsSubscribedToNewsletter = HttpInput::Bool(POST, 'issubscribedtonewsletter') ?? false;
$subscription->IsSubscribedToSummary = HttpInput::Bool(POST, 'issubscribedtosummary') ?? false; $subscription->IsSubscribedToSummary = HttpInput::Bool(POST, 'issubscribedtosummary') ?? false;
$captcha = HttpInput::Str(SESSION, 'captcha') ?? ''; $expectedCaptcha = HttpInput::Str(SESSION, 'captcha') ?? '';
$receivedCaptcha = HttpInput::Str(POST, 'captcha');
$exception = new Exceptions\ValidationException(); $subscription->Create($expectedCaptcha, $receivedCaptcha);
try{
$subscription->Validate();
}
catch(Exceptions\ValidationException $ex){
$exception->Add($ex);
}
if($captcha === '' || mb_strtolower($captcha) !== mb_strtolower(HttpInput::Str(POST, 'captcha') ?? '')){
$exception->Add(new Exceptions\InvalidCaptchaException());
}
if($exception->HasExceptions){
throw $exception;
}
$subscription->Create();
session_unset(); session_unset();
@ -99,8 +83,7 @@ catch(Exceptions\NewsletterSubscriptionExistsException){
http_response_code(409); http_response_code(409);
} }
} }
catch(Exceptions\AppException $ex){ catch(Exceptions\InvalidNewsletterSubscription $ex){
// Validation failed
if($requestType == WEB){ if($requestType == WEB){
$_SESSION['subscription'] = $subscription; $_SESSION['subscription'] = $subscription;
$_SESSION['exception'] = $ex; $_SESSION['exception'] = $ex;

View file

@ -30,8 +30,7 @@ try{
header('Location: ' . $vote->Url); header('Location: ' . $vote->Url);
} }
} }
catch(Exceptions\AppException $ex){ catch(Exceptions\InvalidPollVoteException $ex){
// Validation failed
if($requestType == WEB){ if($requestType == WEB){
$_SESSION['vote'] = $vote; $_SESSION['vote'] = $vote;
$_SESSION['exception'] = $ex; $_SESSION['exception'] = $ex;

View file

@ -33,8 +33,7 @@ try{
header('Location: ' . $session->Url); header('Location: ' . $session->Url);
} }
} }
catch(Exceptions\AppException $ex){ catch(Exceptions\InvalidLoginException $ex){
// Login failed
if($requestType == WEB){ if($requestType == WEB){
$_SESSION['email'] = $email; $_SESSION['email'] = $email;
$_SESSION['redirect'] = $redirect; $_SESSION['redirect'] = $redirect;