Style tweaks for getters and add more type hinting

This commit is contained in:
Alex Cabal 2024-04-30 22:45:20 -05:00
parent 9e31ed1aac
commit 50efeb05d1
13 changed files with 87 additions and 84 deletions

View file

@ -80,6 +80,9 @@ class Artist extends Accessor{
// METHODS // METHODS
// ******* // *******
/**
* @throws Exceptions\InvalidArtistException
*/
public function Validate(): void{ public function Validate(): void{
$now = new DateTimeImmutable(); $now = new DateTimeImmutable();
$thisYear = intval($now->format('Y')); $thisYear = intval($now->format('Y'));
@ -110,6 +113,10 @@ class Artist extends Accessor{
// ORM METHODS // ORM METHODS
// *********** // ***********
/**
* @throws Exceptions\ArtistNotFoundException
*/
public static function Get(?int $artistId): Artist{ public static function Get(?int $artistId): Artist{
if($artistId === null){ if($artistId === null){
throw new Exceptions\ArtistNotFoundException(); throw new Exceptions\ArtistNotFoundException();
@ -121,11 +128,7 @@ class Artist extends Accessor{
where ArtistId = ? where ArtistId = ?
', [$artistId], 'Artist'); ', [$artistId], 'Artist');
if(sizeof($result) == 0){ return $result[0] ?? throw new Exceptions\ArtistNotFoundException();;
throw new Exceptions\ArtistNotFoundException();
}
return $result[0];
} }
public function Create(): void{ 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{ public static function GetOrCreate(Artist $artist): Artist{
$result = Db::Query(' $result = Db::Query('

View file

@ -178,7 +178,7 @@ class Artwork extends Accessor{
} }
/** /**
* @throws \Exceptions\InvalidArtworkException *@throws Exceptions\InvalidArtworkException
*/ */
protected function GetImageUrl(): string{ protected function GetImageUrl(): string{
if($this->_ImageUrl === null){ if($this->_ImageUrl === null){
@ -193,7 +193,7 @@ class Artwork extends Accessor{
} }
/** /**
* @throws \Exceptions\ArtworkNotFoundException *@throws Exceptions\ArtworkNotFoundException
*/ */
protected function GetThumbUrl(): string{ protected function GetThumbUrl(): string{
if($this->_ThumbUrl === null){ if($this->_ThumbUrl === null){
@ -208,7 +208,7 @@ class Artwork extends Accessor{
} }
/** /**
* @throws \Exceptions\ArtworkNotFoundException *@throws Exceptions\ArtworkNotFoundException
*/ */
protected function GetThumb2xUrl(): string{ protected function GetThumb2xUrl(): string{
if($this->_Thumb2xUrl === null){ 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{ protected function Validate(?string $imagePath = null, bool $isImageRequired = true): void{
$now = new DateTimeImmutable(); $now = new DateTimeImmutable();
@ -635,8 +635,8 @@ class Artwork extends Accessor{
} }
/** /**
* @throws \Exceptions\ValidationException *@throws Exceptions\ValidationException
* @throws \Exceptions\InvalidImageUploadException *@throws Exceptions\InvalidImageUploadException
*/ */
public function Create(?string $imagePath = null): void{ public function Create(?string $imagePath = null): void{
$this->MimeType = ImageMimeType::FromFile($imagePath); $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{ public function Save(?string $imagePath = null): void{
$this->_UrlName = null; $this->_UrlName = null;
@ -812,7 +812,7 @@ class Artwork extends Accessor{
// *********** // ***********
/** /**
* @throws \Exceptions\ArtworkNotFoundException *@throws Exceptions\ArtworkNotFoundException
*/ */
public static function Get(?int $artworkId): Artwork{ public static function Get(?int $artworkId): Artwork{
if($artworkId === null){ if($artworkId === null){
@ -825,15 +825,11 @@ class Artwork extends Accessor{
where ArtworkId = ? where ArtworkId = ?
', [$artworkId], 'Artwork'); ', [$artworkId], 'Artwork');
if(sizeof($result) == 0){ return $result[0] ?? throw new Exceptions\ArtworkNotFoundException();
throw new Exceptions\ArtworkNotFoundException();
}
return $result[0];
} }
/** /**
* @throws \Exceptions\InvalidArtworkException *@throws Exceptions\InvalidArtworkException
*/ */
public static function GetByUrl(?string $artistUrlName, ?string $artworkUrlName): Artwork{ public static function GetByUrl(?string $artistUrlName, ?string $artworkUrlName): Artwork{
if($artistUrlName === null || $artworkUrlName === null){ if($artistUrlName === null || $artworkUrlName === null){
@ -847,11 +843,7 @@ class Artwork extends Accessor{
where Artists.UrlName = ? and Artworks.UrlName = ? where Artists.UrlName = ? and Artworks.UrlName = ?
', [$artistUrlName, $artworkUrlName], 'Artwork'); ', [$artistUrlName, $artworkUrlName], 'Artwork');
if(sizeof($result) == 0){ return $result[0] ?? throw new Exceptions\ArtworkNotFoundException();
throw new Exceptions\ArtworkNotFoundException();
}
return $result[0];
} }
public static function FromHttpPost(): Artwork{ public static function FromHttpPost(): Artwork{

View file

@ -26,6 +26,10 @@ class ArtworkTag extends Tag{
// ******* // *******
// METHODS // METHODS
// ******* // *******
/**
* @throws Exceptions\ValidationException
*/
public function Validate(): void{ public function Validate(): void{
$error = new Exceptions\InvalidArtworkTagException($this->Name); $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{ public static function GetOrCreate(ArtworkTag $artworkTag): ArtworkTag{
$result = Db::Query(' $result = Db::Query('

View file

@ -1,6 +1,6 @@
<? <?
namespace Exceptions; namespace Exceptions;
class InvalidPatronException extends AppException{ class PatronNotFoundException extends AppException{
protected $message = 'We couldnt locate you in the Patrons Circle.'; protected $message = 'We couldnt locate you in the Patrons Circle.';
} }

View file

@ -43,7 +43,7 @@ class Image{
/** /**
* @return resource * @return resource
* @throws \Exceptions\InvalidImageUploadException *@throws Exceptions\InvalidImageUploadException
*/ */
private function GetImageHandleFromTiff(){ private function GetImageHandleFromTiff(){
$tempFilename = sys_get_temp_dir() . '/se-' . pathinfo($this->Path)['filename'] . '.jpg'; $tempFilename = sys_get_temp_dir() . '/se-' . pathinfo($this->Path)['filename'] . '.jpg';

View file

@ -560,6 +560,10 @@ class Museum extends Accessor{
return $outputUrl; return $outputUrl;
} }
/**
* @throws Exceptions\MuseumNotFoundException
* @throws Exceptions\InvalidUrlException
*/
public static function GetByUrl(?string $url): Museum{ public static function GetByUrl(?string $url): Museum{
if($url === null){ if($url === null){
throw new Exceptions\MuseumNotFoundException(); throw new Exceptions\MuseumNotFoundException();
@ -583,10 +587,6 @@ class Museum extends Accessor{
limit 1; limit 1;
', [$parsedUrl['host']], 'Museum'); ', [$parsedUrl['host']], 'Museum');
if(sizeof($result) == 0){ return $result[0] ?? throw new Exceptions\MuseumNotFoundException();
throw new Exceptions\MuseumNotFoundException();
}
return $result[0];
} }
} }

View file

@ -132,6 +132,9 @@ class NewsletterSubscription extends Accessor{
// ORM METHODS // ORM METHODS
// *********** // ***********
/**
* @throws Exceptions\NewsletterSubscriptionNotFoundException
*/
public static function Get(?string $uuid): NewsletterSubscription{ public static function Get(?string $uuid): NewsletterSubscription{
if($uuid === null){ if($uuid === null){
throw new Exceptions\NewsletterSubscriptionNotFoundException(); throw new Exceptions\NewsletterSubscriptionNotFoundException();
@ -144,10 +147,6 @@ class NewsletterSubscription extends Accessor{
where u.Uuid = ? where u.Uuid = ?
', [$uuid], 'NewsletterSubscription'); ', [$uuid], 'NewsletterSubscription');
if(sizeof($result) == 0){ return $result[0] ?? throw new Exceptions\NewsletterSubscriptionNotFoundException();
throw new Exceptions\NewsletterSubscriptionNotFoundException();
}
return $result[0];
} }
} }

View file

@ -78,21 +78,31 @@ class Patron extends Accessor{
// ORM METHODS // ORM METHODS
// *********** // ***********
/**
* @throws Exceptions\PatronNotFoundException
*/
public static function Get(?int $userId): Patron{ public static function Get(?int $userId): Patron{
if($userId === null){
throw new Exceptions\PatronNotFoundException();
}
$result = Db::Query(' $result = Db::Query('
SELECT * SELECT *
from Patrons from Patrons
where UserId = ? where UserId = ?
', [$userId], 'Patron'); ', [$userId], 'Patron');
if(sizeof($result) == 0){ return $result[0] ?? throw new Exceptions\PatronNotFoundException();;
throw new Exceptions\InvalidPatronException();
}
return $result[0];
} }
/**
* @throws Exceptions\PatronNotFoundException
*/
public static function GetByEmail(?string $email): Patron{ public static function GetByEmail(?string $email): Patron{
if($email === null){
throw new Exceptions\PatronNotFoundException();
}
$result = Db::Query(' $result = Db::Query('
SELECT p.* SELECT p.*
from Patrons p from Patrons p
@ -100,10 +110,6 @@ class Patron extends Accessor{
where u.Email = ? where u.Email = ?
', [$email], 'Patron'); ', [$email], 'Patron');
if(sizeof($result) == 0){ return $result[0] ?? throw new Exceptions\PatronNotFoundException();
throw new Exceptions\InvalidPatronException();
}
return $result[0];
} }
} }

View file

@ -98,6 +98,9 @@ class Poll extends Accessor{
// ORM METHODS // ORM METHODS
// *********** // ***********
/**
* @throws Exceptions\PollNotFoundException
*/
public static function Get(?int $pollId): Poll{ public static function Get(?int $pollId): Poll{
if($pollId === null){ if($pollId === null){
throw new Exceptions\PollNotFoundException(); throw new Exceptions\PollNotFoundException();
@ -109,13 +112,12 @@ class Poll extends Accessor{
where PollId = ? where PollId = ?
', [$pollId], 'Poll'); ', [$pollId], 'Poll');
if(sizeof($result) == 0){ return $result[0] ?? throw new Exceptions\PollNotFoundException();
throw new Exceptions\PollNotFoundException();
}
return $result[0];
} }
/**
* @throws Exceptions\PollNotFoundException
*/
public static function GetByUrlName(?string $urlName): Poll{ public static function GetByUrlName(?string $urlName): Poll{
if($urlName === null){ if($urlName === null){
throw new Exceptions\PollNotFoundException(); throw new Exceptions\PollNotFoundException();
@ -127,10 +129,6 @@ class Poll extends Accessor{
where UrlName = ? where UrlName = ?
', [$urlName], 'Poll'); ', [$urlName], 'Poll');
if(sizeof($result) == 0){ return $result[0] ?? throw new Exceptions\PollNotFoundException();
throw new Exceptions\PollNotFoundException();
}
return $result[0];
} }
} }

View file

@ -34,6 +34,9 @@ class PollItem extends Accessor{
// ORM METHODS // ORM METHODS
// *********** // ***********
/**
* @throws Exceptions\PollNotFoundException
*/
public static function Get(?int $pollItemId): PollItem{ public static function Get(?int $pollItemId): PollItem{
if($pollItemId === null ){ if($pollItemId === null ){
throw new Exceptions\PollItemNotFoundException(); throw new Exceptions\PollItemNotFoundException();
@ -45,10 +48,6 @@ class PollItem extends Accessor{
where PollItemId = ? where PollItemId = ?
', [$pollItemId], 'PollItem'); ', [$pollItemId], 'PollItem');
if(sizeof($result) == 0){ return $result[0] ?? throw new Exceptions\PollItemNotFoundException();
throw new Exceptions\PollItemNotFoundException();
}
return $result[0];
} }
} }

View file

@ -32,6 +32,9 @@ class PollVote extends Accessor{
// METHODS // METHODS
// ******* // *******
/**
* @throws Exceptions\InvalidPollVoteException
*/
protected function Validate(): void{ protected function Validate(): void{
$error = new Exceptions\InvalidPollVoteException(); $error = new Exceptions\InvalidPollVoteException();
@ -72,7 +75,7 @@ class PollVote extends Accessor{
} }
if(!$this->User->Benefits->CanVote){ 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]); ', [$this->UserId, $this->PollItemId, $this->Created]);
} }
/**
* @throws Exceptions\PollVoteNotFoundException
*/
public static function Get(?string $pollUrlName, ?int $userId): PollVote{ public static function Get(?string $pollUrlName, ?int $userId): PollVote{
if($pollUrlName === null || $userId === null){ if($pollUrlName === null || $userId === null){
throw new Exceptions\PollVoteNotFoundException(); throw new Exceptions\PollVoteNotFoundException();
@ -123,10 +129,6 @@ class PollVote extends Accessor{
where pv.UserId = ? where pv.UserId = ?
', [$pollUrlName, $userId], 'PollVote'); ', [$pollUrlName, $userId], 'PollVote');
if(sizeof($result) == 0){ return $result[0] ?? throw new Exceptions\PollVoteNotFoundException();
throw new Exceptions\PollVoteNotFoundException();
}
return $result[0];
} }
} }

View file

@ -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 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{ public static function Get(?string $sessionId): Session{
if($sessionId === null){ if($sessionId === null){
throw new Exceptions\SessionNotFoundException(); throw new Exceptions\SessionNotFoundException();
@ -103,10 +106,6 @@ class Session extends Accessor{
where SessionId = ? where SessionId = ?
', [$sessionId], 'Session'); ', [$sessionId], 'Session');
if(sizeof($result) == 0){ return $result[0] ?? throw new Exceptions\SessionNotFoundException();
throw new Exceptions\SessionNotFoundException();
}
return $result[0];
} }
} }

View file

@ -107,6 +107,9 @@ class User extends Accessor{
// ORM METHODS // ORM METHODS
// *********** // ***********
/**
* @throws Exceptions\UserNotFoundException
*/
public static function Get(?int $userId): User{ public static function Get(?int $userId): User{
if($userId === null){ if($userId === null){
throw new Exceptions\UserNotFoundException(); throw new Exceptions\UserNotFoundException();
@ -118,13 +121,12 @@ class User extends Accessor{
where UserId = ? where UserId = ?
', [$userId], 'User'); ', [$userId], 'User');
if(sizeof($result) == 0){ return $result[0] ?? throw new Exceptions\UserNotFoundException();
throw new Exceptions\UserNotFoundException();
}
return $result[0];
} }
/**
* @throws Exceptions\UserNotFoundException
*/
public static function GetByEmail(?string $email): User{ public static function GetByEmail(?string $email): User{
if($email === null){ if($email === null){
throw new Exceptions\UserNotFoundException(); throw new Exceptions\UserNotFoundException();
@ -136,13 +138,12 @@ class User extends Accessor{
where Email = ? where Email = ?
', [$email], 'User'); ', [$email], 'User');
if(sizeof($result) == 0){ return $result[0] ?? throw new Exceptions\UserNotFoundException();
throw new Exceptions\UserNotFoundException();
}
return $result[0];
} }
/**
* @throws Exceptions\UserNotFoundException
*/
public static function GetIfRegistered(?string $identifier, ?string $password = null): User{ public static function GetIfRegistered(?string $identifier, ?string $password = null): User{
// We consider a user "registered" if they have a row in the Benefits table. // 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 // Emails without that row may only be signed up for the newsletter and thus are not "registered" users