Convert some constants to enums

This commit is contained in:
Alex Cabal 2024-01-14 22:32:47 -06:00
parent 793d832e92
commit 531e3600ea
14 changed files with 93 additions and 248 deletions

View file

@ -64,7 +64,7 @@ class Artist extends PropertiesBase{
$error->Add(new Exceptions\ArtistNameRequiredException());
}
if($this->Name !== null && strlen($this->Name) > COVER_ARTWORK_MAX_STRING_LENGTH){
if($this->Name !== null && strlen($this->Name) > ARTWORK_MAX_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Artist Name'));
}

View file

@ -20,6 +20,7 @@ use function Safe\preg_replace;
* @property string $ThumbUrl
* @property string $Thumb2xUrl
* @property string $Dimensions
* @property ArtworkStatus|string|null $Status
* @property Ebook $Ebook
* @property Museum $Museum
* @property User $Submitter
@ -35,7 +36,6 @@ class Artwork extends PropertiesBase{
public bool $CompletedYearIsCirca = false;
public ?DateTime $Created = null;
public ?DateTime $Updated = null;
public ?string $Status = null;
public ?string $EbookWwwFilesystemPath = null;
public ?int $SubmitterUserId = null;
public ?int $ReviewerUserId = null;
@ -60,6 +60,7 @@ class Artwork extends PropertiesBase{
protected ?User $_Submitter = null;
protected ?User $_Reviewer = null;
protected ?ImageMimeType $_MimeType = null;
protected ?ArtworkStatus $_Status = null;
// *******
// SETTERS
@ -68,7 +69,7 @@ class Artwork extends PropertiesBase{
/**
* @param string|null|array<ArtworkTag> $tags
*/
protected function SetTags(string|array|null $tags): void{
protected function SetTags(null|string|array $tags): void{
if($tags === null || is_array($tags)){
$this->_Tags = $tags;
}
@ -85,6 +86,30 @@ class Artwork extends PropertiesBase{
}
}
protected function SetStatus(null|string|ArtworkStatus $status): void{
if($status instanceof ArtworkStatus){
$this->_Status = $status;
}
elseif($status === null){
$this->_Status = null;
}
else{
$this->_Status = ArtworkStatus::from($status);
}
}
protected function SetMimeType(null|string|ImageMimeType $mimeType): void{
if($mimeType instanceof ImageMimeType){
$this->_MimeType = $mimeType;
}
elseif($mimeType === null){
$this->_MimeType = null;
}
else{
$this->_MimeType = ImageMimeType::tryFrom($mimeType);
}
}
// *******
// GETTERS
// *******
@ -235,15 +260,6 @@ class Artwork extends PropertiesBase{
return $this->_Ebook;
}
protected function SetMimeType(null|string|ImageMimeType $mimeType): void{
if(is_string($mimeType)){
$this->_MimeType = ImageMimeType::tryFrom($mimeType);
}
else{
$this->_MimeType = $mimeType;
}
}
// *******
// METHODS
// *******
@ -277,7 +293,7 @@ class Artwork extends PropertiesBase{
$error->Add(new Exceptions\ArtworkNameRequiredException());
}
if($this->Name !== null && strlen($this->Name) > COVER_ARTWORK_MAX_STRING_LENGTH){
if($this->Name !== null && strlen($this->Name) > ARTWORK_MAX_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Artwork Name'));
}
@ -293,33 +309,33 @@ class Artwork extends PropertiesBase{
$error->Add(new Exceptions\InvalidPublicationYearException());
}
if($this->Status !== null && !in_array($this->Status, [COVER_ARTWORK_STATUS_UNVERIFIED, COVER_ARTWORK_STATUS_APPROVED, COVER_ARTWORK_STATUS_DECLINED, COVER_ARTWORK_STATUS_IN_USE])){
if($this->Status === null){
$error->Add(new Exceptions\InvalidArtworkException('Invalid status.'));
}
if($this->Status === COVER_ARTWORK_STATUS_IN_USE && $this->EbookWwwFilesystemPath === null){
if($this->Status == ArtworkStatus::InUse && $this->EbookWwwFilesystemPath === null){
$error->Add(new Exceptions\MissingEbookException());
}
if(count($this->Tags) == 0){
// In-use artwork doesn't have user-provided tags.
if($this->Status !== COVER_ARTWORK_STATUS_IN_USE){
if($this->Status != ArtworkStatus::InUse){
$error->Add(new Exceptions\TagsRequiredException());
}
}
if(count($this->Tags) > COVER_ARTWORK_MAX_TAGS){
if(count($this->Tags) > ARTWORK_MAX_TAGS){
$error->Add(new Exceptions\TooManyTagsException());
}
foreach($this->Tags as $tag){
if(strlen($tag->Name) > COVER_ARTWORK_MAX_STRING_LENGTH){
if(strlen($tag->Name) > ARTWORK_MAX_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Artwork Tag: '. $tag->Name));
}
}
if($this->MuseumUrl !== null){
if(strlen($this->MuseumUrl) > COVER_ARTWORK_MAX_STRING_LENGTH){
if(strlen($this->MuseumUrl) > ARTWORK_MAX_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Link to an approved museum page'));
}
@ -333,7 +349,7 @@ class Artwork extends PropertiesBase{
}
if($this->PublicationYearPageUrl !== null){
if(strlen($this->PublicationYearPageUrl) > COVER_ARTWORK_MAX_STRING_LENGTH){
if(strlen($this->PublicationYearPageUrl) > ARTWORK_MAX_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Link to page with year of publication'));
}
@ -351,7 +367,7 @@ class Artwork extends PropertiesBase{
}
if($this->CopyrightPageUrl !== null){
if(strlen($this->CopyrightPageUrl) > COVER_ARTWORK_MAX_STRING_LENGTH){
if(strlen($this->CopyrightPageUrl) > ARTWORK_MAX_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Link to page with copyright details'));
}
@ -369,7 +385,7 @@ class Artwork extends PropertiesBase{
}
if($this->ArtworkPageUrl !== null){
if(strlen($this->ArtworkPageUrl) > COVER_ARTWORK_MAX_STRING_LENGTH){
if(strlen($this->ArtworkPageUrl) > ARTWORK_MAX_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Link to page with artwork'));
}
@ -437,7 +453,7 @@ class Artwork extends PropertiesBase{
// Check for minimum dimensions
list($imageWidth, $imageHeight) = getimagesize($uploadedFile['tmp_name']);
if(!$imageWidth || !$imageHeight || $imageWidth < COVER_ARTWORK_IMAGE_MINIMUM_WIDTH || $imageHeight < COVER_ARTWORK_IMAGE_MINIMUM_HEIGHT){
if(!$imageWidth || !$imageHeight || $imageWidth < ARTWORK_IMAGE_MINIMUM_WIDTH || $imageHeight < ARTWORK_IMAGE_MINIMUM_HEIGHT){
$error->Add(new Exceptions\ArtworkImageDimensionsTooSmallException());
}
}
@ -624,8 +640,8 @@ class Artwork extends PropertiesBase{
// Generate the thumbnails
try{
$image = new Image($imageUploadPath);
$image->Resize(WEB_ROOT . $this->ThumbUrl, COVER_THUMBNAIL_WIDTH, COVER_THUMBNAIL_HEIGHT);
$image->Resize(WEB_ROOT . $this->Thumb2xUrl, COVER_THUMBNAIL_WIDTH * 2, COVER_THUMBNAIL_HEIGHT * 2);
$image->Resize(WEB_ROOT . $this->ThumbUrl, ARTWORK_THUMBNAIL_WIDTH, ARTWORK_THUMBNAIL_HEIGHT);
$image->Resize(WEB_ROOT . $this->Thumb2xUrl, ARTWORK_THUMBNAIL_WIDTH * 2, ARTWORK_THUMBNAIL_HEIGHT * 2);
}
catch(\Safe\Exceptions\FilesystemException | \Safe\Exceptions\ImageException){
throw new Exceptions\InvalidImageUploadException('Failed to generate thumbnail.');
@ -669,12 +685,6 @@ class Artwork extends PropertiesBase{
);
}
public function MarkInUse(string $ebookWwwFilesystemPath): void{
$this->EbookWwwFilesystemPath = $ebookWwwFilesystemPath;
$this->Status = COVER_ARTWORK_STATUS_IN_USE;
$this->Save();
}
public function Delete(): void{
Db::Query('
DELETE

7
lib/ArtworkStatus.php Normal file
View file

@ -0,0 +1,7 @@
<?
enum ArtworkStatus: string{
case Unverified = 'unverified';
case Declined = 'declined';
case Approved = 'approved';
case InUse = 'in_use';
}

View file

@ -38,17 +38,13 @@ const SORT_AUTHOR_ALPHA = 'author-alpha';
const SORT_READING_EASE = 'reading-ease';
const SORT_LENGTH = 'length';
const COVER_THUMBNAIL_HEIGHT = 350;
const COVER_THUMBNAIL_WIDTH = 350;
const COVER_ARTWORK_PER_PAGE = 20;
const COVER_ARTWORK_STATUS_APPROVED = 'approved';
const COVER_ARTWORK_STATUS_DECLINED = 'declined';
const COVER_ARTWORK_STATUS_IN_USE = 'in_use';
const COVER_ARTWORK_STATUS_UNVERIFIED = 'unverified';
const COVER_ARTWORK_MAX_STRING_LENGTH = 250;
const COVER_ARTWORK_MAX_TAGS = 15;
const COVER_ARTWORK_IMAGE_MINIMUM_WIDTH = 300;
const COVER_ARTWORK_IMAGE_MINIMUM_HEIGHT = 300;
const ARTWORK_THUMBNAIL_HEIGHT = 350;
const ARTWORK_THUMBNAIL_WIDTH = 350;
const ARTWORK_PER_PAGE = 20;
const ARTWORK_MAX_STRING_LENGTH = 250;
const ARTWORK_MAX_TAGS = 15;
const ARTWORK_IMAGE_MINIMUM_WIDTH = 300;
const ARTWORK_IMAGE_MINIMUM_HEIGHT = 300;
const SORT_COVER_ARTWORK_CREATED_NEWEST = 'created-newest';
const SORT_COVER_ARTIST_ALPHA = 'artist-alpha';
const SORT_COVER_ARTWORK_COMPLETED_NEWEST = 'completed-newest';
@ -116,7 +112,7 @@ const ARTWORK_UPLOADS_LOG_FILE_PATH = '/var/log/local/artwork-uploads.log'; // M
define('PD_YEAR', intval($nowPd->format('Y')) - 96);
define('PD_STRING', 'January 1, ' . (PD_YEAR + 1));
define('DONATION_HOLIDAY_ALERT_ON', $now > new DateTime('November 15, ' . $now->format('Y'), new DateTimeZone('UTC')) || $now < new DateTime('January 7, ' . $now->add(new DateInterval('P1Y'))->format('Y'), new DateTimeZone('UTC')));
define('DONATION_HOLIDAY_ALERT_ON', $now > new DateTime('November 15, ' . $now->format('Y'), new DateTimeZone('UTC')) || $now < new DateTime('January 7, ' . $now->add(new DateInterval('P1Y'))->format('Y'), new DateTimeZone('UTC')));
define('DONATION_ALERT_ON', DONATION_HOLIDAY_ALERT_ON || rand(1, 4) == 2);
// Controls the progress bar donation dialog

View file

@ -6,6 +6,6 @@ class ArtworkImageDimensionsTooSmallException extends AppException{
// This has to be initialized in a constructor because we use the number_format() function.
public function __construct(){
$this->message = 'Image dimensions are too small. The minimum image size is ' . number_format(COVER_ARTWORK_IMAGE_MINIMUM_WIDTH) . ' × ' . number_format(COVER_ARTWORK_IMAGE_MINIMUM_HEIGHT) . '.';
$this->message = 'Image dimensions are too small. The minimum image size is ' . number_format(ARTWORK_IMAGE_MINIMUM_WIDTH) . ' × ' . number_format(ARTWORK_IMAGE_MINIMUM_HEIGHT) . '.';
}
}

View file

@ -2,5 +2,5 @@
namespace Exceptions;
class TooManyTagsException extends AppException{
protected $message = 'Too many tags; the maximum is ' . COVER_ARTWORK_MAX_TAGS . '.';
protected $message = 'Too many tags; the maximum is ' . ARTWORK_MAX_TAGS . '.';
}