From 50efeb05d168bbdecd0ac8924935e84f19859697 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Tue, 30 Apr 2024 22:45:20 -0500 Subject: [PATCH] Style tweaks for getters and add more type hinting --- lib/Artist.php | 15 ++++++---- lib/Artwork.php | 30 +++++++------------ lib/ArtworkTag.php | 6 +++- ...eption.php => PatronNotFoundException.php} | 2 +- lib/Image.php | 2 +- lib/Museum.php | 10 +++---- lib/NewsletterSubscription.php | 9 +++--- lib/Patron.php | 26 +++++++++------- lib/Poll.php | 18 +++++------ lib/PollItem.php | 9 +++--- lib/PollVote.php | 14 +++++---- lib/Session.php | 9 +++--- lib/User.php | 21 ++++++------- 13 files changed, 87 insertions(+), 84 deletions(-) rename lib/Exceptions/{InvalidPatronException.php => PatronNotFoundException.php} (66%) diff --git a/lib/Artist.php b/lib/Artist.php index 653f4850..756c961f 100644 --- a/lib/Artist.php +++ b/lib/Artist.php @@ -80,6 +80,9 @@ class Artist extends Accessor{ // METHODS // ******* + /** + * @throws Exceptions\InvalidArtistException + */ public function Validate(): void{ $now = new DateTimeImmutable(); $thisYear = intval($now->format('Y')); @@ -110,6 +113,10 @@ class Artist extends Accessor{ // ORM METHODS // *********** + + /** + * @throws Exceptions\ArtistNotFoundException + */ public static function Get(?int $artistId): Artist{ if($artistId === null){ throw new Exceptions\ArtistNotFoundException(); @@ -121,11 +128,7 @@ class Artist extends Accessor{ where ArtistId = ? ', [$artistId], 'Artist'); - if(sizeof($result) == 0){ - throw new Exceptions\ArtistNotFoundException(); - } - - return $result[0]; + return $result[0] ?? throw new Exceptions\ArtistNotFoundException();; } public function Create(): void{ @@ -141,7 +144,7 @@ class Artist extends Accessor{ } /** - * @throws \Exceptions\ValidationException + * @throws Exceptions\ValidationException */ public static function GetOrCreate(Artist $artist): Artist{ $result = Db::Query(' diff --git a/lib/Artwork.php b/lib/Artwork.php index dd9a5c87..e35f87b2 100644 --- a/lib/Artwork.php +++ b/lib/Artwork.php @@ -178,7 +178,7 @@ class Artwork extends Accessor{ } /** - * @throws \Exceptions\InvalidArtworkException + *@throws Exceptions\InvalidArtworkException */ protected function GetImageUrl(): string{ if($this->_ImageUrl === null){ @@ -193,7 +193,7 @@ class Artwork extends Accessor{ } /** - * @throws \Exceptions\ArtworkNotFoundException + *@throws Exceptions\ArtworkNotFoundException */ protected function GetThumbUrl(): string{ if($this->_ThumbUrl === null){ @@ -208,7 +208,7 @@ class Artwork extends Accessor{ } /** - * @throws \Exceptions\ArtworkNotFoundException + *@throws Exceptions\ArtworkNotFoundException */ protected function GetThumb2xUrl(): string{ if($this->_Thumb2xUrl === null){ @@ -316,7 +316,7 @@ class Artwork extends Accessor{ } /** - * @throws \Exceptions\ValidationException + *@throws Exceptions\ValidationException */ protected function Validate(?string $imagePath = null, bool $isImageRequired = true): void{ $now = new DateTimeImmutable(); @@ -635,8 +635,8 @@ class Artwork extends Accessor{ } /** - * @throws \Exceptions\ValidationException - * @throws \Exceptions\InvalidImageUploadException + *@throws Exceptions\ValidationException + *@throws Exceptions\InvalidImageUploadException */ public function Create(?string $imagePath = null): void{ $this->MimeType = ImageMimeType::FromFile($imagePath); @@ -698,7 +698,7 @@ class Artwork extends Accessor{ } /** - * @throws \Exceptions\ValidationException + *@throws Exceptions\ValidationException */ public function Save(?string $imagePath = null): void{ $this->_UrlName = null; @@ -812,7 +812,7 @@ class Artwork extends Accessor{ // *********** /** - * @throws \Exceptions\ArtworkNotFoundException + *@throws Exceptions\ArtworkNotFoundException */ public static function Get(?int $artworkId): Artwork{ if($artworkId === null){ @@ -825,15 +825,11 @@ class Artwork extends Accessor{ where ArtworkId = ? ', [$artworkId], 'Artwork'); - if(sizeof($result) == 0){ - throw new Exceptions\ArtworkNotFoundException(); - } - - return $result[0]; + return $result[0] ?? throw new Exceptions\ArtworkNotFoundException(); } /** - * @throws \Exceptions\InvalidArtworkException + *@throws Exceptions\InvalidArtworkException */ public static function GetByUrl(?string $artistUrlName, ?string $artworkUrlName): Artwork{ if($artistUrlName === null || $artworkUrlName === null){ @@ -847,11 +843,7 @@ class Artwork extends Accessor{ where Artists.UrlName = ? and Artworks.UrlName = ? ', [$artistUrlName, $artworkUrlName], 'Artwork'); - if(sizeof($result) == 0){ - throw new Exceptions\ArtworkNotFoundException(); - } - - return $result[0]; + return $result[0] ?? throw new Exceptions\ArtworkNotFoundException(); } public static function FromHttpPost(): Artwork{ diff --git a/lib/ArtworkTag.php b/lib/ArtworkTag.php index c61ec27c..6294a667 100644 --- a/lib/ArtworkTag.php +++ b/lib/ArtworkTag.php @@ -26,6 +26,10 @@ class ArtworkTag extends Tag{ // ******* // METHODS // ******* + + /** + * @throws Exceptions\ValidationException + */ public function Validate(): void{ $error = new Exceptions\InvalidArtworkTagException($this->Name); @@ -61,7 +65,7 @@ class ArtworkTag extends Tag{ } /** - * @throws \Exceptions\ValidationException + * @throws Exceptions\ValidationException */ public static function GetOrCreate(ArtworkTag $artworkTag): ArtworkTag{ $result = Db::Query(' diff --git a/lib/Exceptions/InvalidPatronException.php b/lib/Exceptions/PatronNotFoundException.php similarity index 66% rename from lib/Exceptions/InvalidPatronException.php rename to lib/Exceptions/PatronNotFoundException.php index aa7ff07e..cbc6c606 100644 --- a/lib/Exceptions/InvalidPatronException.php +++ b/lib/Exceptions/PatronNotFoundException.php @@ -1,6 +1,6 @@ Path)['filename'] . '.jpg'; diff --git a/lib/Museum.php b/lib/Museum.php index faa35512..6bec2d08 100644 --- a/lib/Museum.php +++ b/lib/Museum.php @@ -560,6 +560,10 @@ class Museum extends Accessor{ return $outputUrl; } + /** + * @throws Exceptions\MuseumNotFoundException + * @throws Exceptions\InvalidUrlException + */ public static function GetByUrl(?string $url): Museum{ if($url === null){ throw new Exceptions\MuseumNotFoundException(); @@ -583,10 +587,6 @@ class Museum extends Accessor{ limit 1; ', [$parsedUrl['host']], 'Museum'); - if(sizeof($result) == 0){ - throw new Exceptions\MuseumNotFoundException(); - } - - return $result[0]; + return $result[0] ?? throw new Exceptions\MuseumNotFoundException(); } } diff --git a/lib/NewsletterSubscription.php b/lib/NewsletterSubscription.php index 1e414c8f..457c82e3 100644 --- a/lib/NewsletterSubscription.php +++ b/lib/NewsletterSubscription.php @@ -132,6 +132,9 @@ class NewsletterSubscription extends Accessor{ // ORM METHODS // *********** + /** + * @throws Exceptions\NewsletterSubscriptionNotFoundException + */ public static function Get(?string $uuid): NewsletterSubscription{ if($uuid === null){ throw new Exceptions\NewsletterSubscriptionNotFoundException(); @@ -144,10 +147,6 @@ class NewsletterSubscription extends Accessor{ where u.Uuid = ? ', [$uuid], 'NewsletterSubscription'); - if(sizeof($result) == 0){ - throw new Exceptions\NewsletterSubscriptionNotFoundException(); - } - - return $result[0]; + return $result[0] ?? throw new Exceptions\NewsletterSubscriptionNotFoundException(); } } diff --git a/lib/Patron.php b/lib/Patron.php index 1344a949..dbdf05b4 100644 --- a/lib/Patron.php +++ b/lib/Patron.php @@ -78,21 +78,31 @@ class Patron extends Accessor{ // ORM METHODS // *********** + /** + * @throws Exceptions\PatronNotFoundException + */ public static function Get(?int $userId): Patron{ + if($userId === null){ + throw new Exceptions\PatronNotFoundException(); + } + $result = Db::Query(' SELECT * from Patrons where UserId = ? ', [$userId], 'Patron'); - if(sizeof($result) == 0){ - throw new Exceptions\InvalidPatronException(); - } - - return $result[0]; + return $result[0] ?? throw new Exceptions\PatronNotFoundException();; } + /** + * @throws Exceptions\PatronNotFoundException + */ public static function GetByEmail(?string $email): Patron{ + if($email === null){ + throw new Exceptions\PatronNotFoundException(); + } + $result = Db::Query(' SELECT p.* from Patrons p @@ -100,10 +110,6 @@ class Patron extends Accessor{ where u.Email = ? ', [$email], 'Patron'); - if(sizeof($result) == 0){ - throw new Exceptions\InvalidPatronException(); - } - - return $result[0]; + return $result[0] ?? throw new Exceptions\PatronNotFoundException(); } } diff --git a/lib/Poll.php b/lib/Poll.php index 06ccf5b6..f024eb19 100644 --- a/lib/Poll.php +++ b/lib/Poll.php @@ -98,6 +98,9 @@ class Poll extends Accessor{ // ORM METHODS // *********** + /** + * @throws Exceptions\PollNotFoundException + */ public static function Get(?int $pollId): Poll{ if($pollId === null){ throw new Exceptions\PollNotFoundException(); @@ -109,13 +112,12 @@ class Poll extends Accessor{ where PollId = ? ', [$pollId], 'Poll'); - if(sizeof($result) == 0){ - throw new Exceptions\PollNotFoundException(); - } - - return $result[0]; + return $result[0] ?? throw new Exceptions\PollNotFoundException(); } + /** + * @throws Exceptions\PollNotFoundException + */ public static function GetByUrlName(?string $urlName): Poll{ if($urlName === null){ throw new Exceptions\PollNotFoundException(); @@ -127,10 +129,6 @@ class Poll extends Accessor{ where UrlName = ? ', [$urlName], 'Poll'); - if(sizeof($result) == 0){ - throw new Exceptions\PollNotFoundException(); - } - - return $result[0]; + return $result[0] ?? throw new Exceptions\PollNotFoundException(); } } diff --git a/lib/PollItem.php b/lib/PollItem.php index a597d8c3..9d413bac 100644 --- a/lib/PollItem.php +++ b/lib/PollItem.php @@ -34,6 +34,9 @@ class PollItem extends Accessor{ // ORM METHODS // *********** + /** + * @throws Exceptions\PollNotFoundException + */ public static function Get(?int $pollItemId): PollItem{ if($pollItemId === null ){ throw new Exceptions\PollItemNotFoundException(); @@ -45,10 +48,6 @@ class PollItem extends Accessor{ where PollItemId = ? ', [$pollItemId], 'PollItem'); - if(sizeof($result) == 0){ - throw new Exceptions\PollItemNotFoundException(); - } - - return $result[0]; + return $result[0] ?? throw new Exceptions\PollItemNotFoundException(); } } diff --git a/lib/PollVote.php b/lib/PollVote.php index 35847def..58be8183 100644 --- a/lib/PollVote.php +++ b/lib/PollVote.php @@ -32,6 +32,9 @@ class PollVote extends Accessor{ // METHODS // ******* + /** + * @throws Exceptions\InvalidPollVoteException + */ protected function Validate(): void{ $error = new Exceptions\InvalidPollVoteException(); @@ -72,7 +75,7 @@ class PollVote extends Accessor{ } if(!$this->User->Benefits->CanVote){ - $error->Add(new Exceptions\InvalidPatronException()); + $error->Add(new Exceptions\InvalidPermissionsException()); } } @@ -107,6 +110,9 @@ class PollVote extends Accessor{ ', [$this->UserId, $this->PollItemId, $this->Created]); } + /** + * @throws Exceptions\PollVoteNotFoundException + */ public static function Get(?string $pollUrlName, ?int $userId): PollVote{ if($pollUrlName === null || $userId === null){ throw new Exceptions\PollVoteNotFoundException(); @@ -123,10 +129,6 @@ class PollVote extends Accessor{ where pv.UserId = ? ', [$pollUrlName, $userId], 'PollVote'); - if(sizeof($result) == 0){ - throw new Exceptions\PollVoteNotFoundException(); - } - - return $result[0]; + return $result[0] ?? throw new Exceptions\PollVoteNotFoundException(); } } diff --git a/lib/Session.php b/lib/Session.php index ab9448fd..8e17c693 100644 --- a/lib/Session.php +++ b/lib/Session.php @@ -92,6 +92,9 @@ class Session extends Accessor{ setcookie('sessionid', $sessionId, ['expires' => strtotime('+1 week'), 'path' => '/', 'domain' => SITE_DOMAIN, 'secure' => true, 'httponly' => false, 'samesite' => 'Lax']); // Expires in two weeks } + /** + * @throws Exceptions\SessionNotFoundException + */ public static function Get(?string $sessionId): Session{ if($sessionId === null){ throw new Exceptions\SessionNotFoundException(); @@ -103,10 +106,6 @@ class Session extends Accessor{ where SessionId = ? ', [$sessionId], 'Session'); - if(sizeof($result) == 0){ - throw new Exceptions\SessionNotFoundException(); - } - - return $result[0]; + return $result[0] ?? throw new Exceptions\SessionNotFoundException(); } } diff --git a/lib/User.php b/lib/User.php index 9a0e59b2..aedf37e4 100644 --- a/lib/User.php +++ b/lib/User.php @@ -107,6 +107,9 @@ class User extends Accessor{ // ORM METHODS // *********** + /** + * @throws Exceptions\UserNotFoundException + */ public static function Get(?int $userId): User{ if($userId === null){ throw new Exceptions\UserNotFoundException(); @@ -118,13 +121,12 @@ class User extends Accessor{ where UserId = ? ', [$userId], 'User'); - if(sizeof($result) == 0){ - throw new Exceptions\UserNotFoundException(); - } - - return $result[0]; + return $result[0] ?? throw new Exceptions\UserNotFoundException(); } + /** + * @throws Exceptions\UserNotFoundException + */ public static function GetByEmail(?string $email): User{ if($email === null){ throw new Exceptions\UserNotFoundException(); @@ -136,13 +138,12 @@ class User extends Accessor{ where Email = ? ', [$email], 'User'); - if(sizeof($result) == 0){ - throw new Exceptions\UserNotFoundException(); - } - - return $result[0]; + return $result[0] ?? throw new Exceptions\UserNotFoundException(); } + /** + * @throws Exceptions\UserNotFoundException + */ public static function GetIfRegistered(?string $identifier, ?string $password = null): User{ // We consider a user "registered" if they have a row in the Benefits table. // Emails without that row may only be signed up for the newsletter and thus are not "registered" users