Skip to content

Commit

Permalink
Convert some constants to enums
Browse files Browse the repository at this point in the history
  • Loading branch information
acabal committed Jan 15, 2024
1 parent 793d832 commit 531e360
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 248 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,16 @@ Before submitting design contributions, please discuss them with the Standard Eb
- Finding a self-hosted replacement for GitHub, like possibly [Gitea](https://gitea.io/) or [GitLab](https://about.gitlab.com/), and figuring out how to reproducably install and update it on the SE server.
- Converting some constants to enums, like `SORT_*` or `SOURCE_*`.
### Artwork database
- Allow submitter or admins to edit unapproved artwork submissions. Approved/in use submissions should not be editable by anyone.
- Include in-use ebook slug as a search parameter when searching for artwork by keyword.
- Remove `in_use` status for an artwork; instead, an artwork with an `EbookWwwFilesystemPath` that is not `null` should be considered to be "in use" regardless of its `Status`.
## PHP code style
- Indent with tabs.
Expand Down
2 changes: 1 addition & 1 deletion lib/Artist.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function Validate(): void{
$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'));
}

Expand Down
70 changes: 40 additions & 30 deletions lib/Artwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* @property string $ThumbUrl
* @property string $Thumb2xUrl
* @property string $Dimensions
* @property ArtworkStatus|string|null $Status
* @property Ebook $Ebook
* @property Museum $Museum
* @property User $Submitter
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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;
}
Expand All @@ -85,6 +86,30 @@ protected function SetTags(string|array|null $tags): void{
}
}

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
// *******
Expand Down Expand Up @@ -235,15 +260,6 @@ protected function GetEbook(): ?Ebook{
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
// *******
Expand Down Expand Up @@ -277,7 +293,7 @@ protected function Validate(array &$uploadedFile = []): void{
$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'));
}

Expand All @@ -293,33 +309,33 @@ protected function Validate(array &$uploadedFile = []): void{
$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'));
}

Expand All @@ -333,7 +349,7 @@ protected function Validate(array &$uploadedFile = []): void{
}

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'));
}

Expand All @@ -351,7 +367,7 @@ protected function Validate(array &$uploadedFile = []): void{
}

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'));
}

Expand All @@ -369,7 +385,7 @@ protected function Validate(array &$uploadedFile = []): void{
}

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'));
}

Expand Down Expand Up @@ -437,7 +453,7 @@ protected function Validate(array &$uploadedFile = []): void{

// 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());
}
}
Expand Down Expand Up @@ -624,8 +640,8 @@ public function Create(array $uploadedFile): void{
// 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.');
Expand Down Expand Up @@ -669,12 +685,6 @@ public function Save(): void{
);
}

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
Expand Down
7 changes: 7 additions & 0 deletions lib/ArtworkStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?
enum ArtworkStatus: string{
case Unverified = 'unverified';
case Declined = 'declined';
case Approved = 'approved';
case InUse = 'in_use';
}
20 changes: 8 additions & 12 deletions lib/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,13 @@
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';
Expand Down Expand Up @@ -116,7 +112,7 @@
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
Expand Down
2 changes: 1 addition & 1 deletion lib/Exceptions/ArtworkImageDimensionsTooSmallException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) . '.';
}
}
2 changes: 1 addition & 1 deletion lib/Exceptions/TooManyTagsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 . '.';
}
Loading

0 comments on commit 531e360

Please sign in to comment.