Minor refactor

This commit is contained in:
Alex Cabal 2025-02-15 11:39:09 -06:00
parent 388dbab1f1
commit 831c1531d8
4 changed files with 66 additions and 110 deletions

View file

@ -306,6 +306,15 @@ final class Ebook{
* We do this in a single database query to prevent 4+ queries for each ebook. * We do this in a single database query to prevent 4+ queries for each ebook.
*/ */
protected function GetAllContributors(): void{ protected function GetAllContributors(): void{
$this->_Authors = $this->_Authors ?? [];
$this->_Translators = $this->_Translators ?? [];
$this->_Illustrators = $this->_Illustrators ?? [];
$this->_Contributors = $this->_Contributors ?? [];
if(!isset($this->EbookId)){
return;
}
$contributors = Db::Query(' $contributors = Db::Query('
SELECT * SELECT *
from Contributors from Contributors
@ -313,11 +322,6 @@ final class Ebook{
order by MarcRole asc, SortOrder asc order by MarcRole asc, SortOrder asc
', [$this->EbookId], Contributor::class); ', [$this->EbookId], Contributor::class);
$this->_Authors = [];
$this->_Translators = [];
$this->_Illustrators = [];
$this->_Contributors = [];
foreach($contributors as $contributor){ foreach($contributors as $contributor){
switch($contributor->MarcRole){ switch($contributor->MarcRole){
case Enums\MarcRole::Author: case Enums\MarcRole::Author:
@ -1144,20 +1148,10 @@ final class Ebook{
/** /**
* Populates the `Identifier` property based on the `Title`, `Authors`, `Translators`, and `Illustrators`. Used when creating ebook placeholders. * Populates the `Identifier` property based on the `Title`, `Authors`, `Translators`, and `Illustrators`. Used when creating ebook placeholders.
*
* @throws Exceptions\InvalidEbookIdentifierException
*/ */
protected function FillIdentifierFromTitleAndContributors(): void{ protected function SetIdentifier(): void{
if(!isset($this->Authors) || sizeof($this->Authors) == 0){
throw new Exceptions\InvalidEbookIdentifierException('Authors required');
}
if(!isset($this->Title)){
throw new Exceptions\InvalidEbookIdentifierException('Title required');
}
$authorString = Ebook::GetContributorsUrlSlug($this->Authors); $authorString = Ebook::GetContributorsUrlSlug($this->Authors);
$titleString = Formatter::MakeUrlSafe($this->Title); $titleString = Formatter::MakeUrlSafe($this->Title ?? '');
$translatorString = ''; $translatorString = '';
$illustratorString = ''; $illustratorString = '';
@ -1182,8 +1176,6 @@ final class Ebook{
/** /**
* Populates `EbookPlaceholder` and other fields from `Template::EbookPlaceholderForm()`. * Populates `EbookPlaceholder` and other fields from `Template::EbookPlaceholderForm()`.
*
* @throws Exceptions\InvalidEbookIdentifierException
*/ */
public function FillFromEbookPlaceholderForm(): void{ public function FillFromEbookPlaceholderForm(): void{
$title = HttpInput::Str(POST, 'ebook-title'); $title = HttpInput::Str(POST, 'ebook-title');
@ -1250,7 +1242,7 @@ final class Ebook{
$this->Tags = []; $this->Tags = [];
$this->TocEntries = []; $this->TocEntries = [];
$this->FillIdentifierFromTitleAndContributors(); $this->SetIdentifier();
} }
// ******* // *******
@ -1263,9 +1255,7 @@ final class Ebook{
public function Validate(): void{ public function Validate(): void{
$error = new Exceptions\InvalidEbookException(); $error = new Exceptions\InvalidEbookException();
if(isset($this->Identifier)){ $this->Identifier = trim($this->Identifier ?? '');
$this->Identifier = trim($this->Identifier);
if($this->Identifier == ''){ if($this->Identifier == ''){
$error->Add(new Exceptions\EbookIdentifierRequiredException()); $error->Add(new Exceptions\EbookIdentifierRequiredException());
} }
@ -1273,17 +1263,12 @@ final class Ebook{
if(strlen($this->Identifier) > EBOOKS_MAX_LONG_STRING_LENGTH){ if(strlen($this->Identifier) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook Identifier')); $error->Add(new Exceptions\StringTooLongException('Ebook Identifier'));
} }
}
else{
$error->Add(new Exceptions\EbookIdentifierRequiredException());
}
$this->WwwFilesystemPath = trim($this->WwwFilesystemPath ?? ''); $this->WwwFilesystemPath = trim($this->WwwFilesystemPath ?? '');
if($this->WwwFilesystemPath == ''){ if($this->WwwFilesystemPath == ''){
$this->WwwFilesystemPath = null; $this->WwwFilesystemPath = null;
} }
else{
if(isset($this->WwwFilesystemPath)){
if(strlen($this->WwwFilesystemPath) > EBOOKS_MAX_LONG_STRING_LENGTH){ if(strlen($this->WwwFilesystemPath) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook WwwFilesystemPath')); $error->Add(new Exceptions\StringTooLongException('Ebook WwwFilesystemPath'));
} }
@ -1297,8 +1282,7 @@ final class Ebook{
if($this->RepoFilesystemPath == ''){ if($this->RepoFilesystemPath == ''){
$this->RepoFilesystemPath = null; $this->RepoFilesystemPath = null;
} }
else{
if(isset($this->RepoFilesystemPath)){
if(strlen($this->RepoFilesystemPath) > EBOOKS_MAX_LONG_STRING_LENGTH){ if(strlen($this->RepoFilesystemPath) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook RepoFilesystemPath')); $error->Add(new Exceptions\StringTooLongException('Ebook RepoFilesystemPath'));
} }
@ -1312,8 +1296,7 @@ final class Ebook{
if($this->KindleCoverUrl == ''){ if($this->KindleCoverUrl == ''){
$this->KindleCoverUrl = null; $this->KindleCoverUrl = null;
} }
else{
if(isset($this->KindleCoverUrl)){
if(!preg_match('|/*_EBOK_portrait.jpg$|ius', $this->KindleCoverUrl)){ if(!preg_match('|/*_EBOK_portrait.jpg$|ius', $this->KindleCoverUrl)){
$error->Add(new Exceptions\InvalidEbookKindleCoverUrlException('Invalid Ebook KindleCoverUrl: ' . $this->KindleCoverUrl)); $error->Add(new Exceptions\InvalidEbookKindleCoverUrlException('Invalid Ebook KindleCoverUrl: ' . $this->KindleCoverUrl));
} }
@ -1327,8 +1310,7 @@ final class Ebook{
if($this->EpubUrl == ''){ if($this->EpubUrl == ''){
$this->EpubUrl = null; $this->EpubUrl = null;
} }
else{
if(isset($this->EpubUrl)){
if(!preg_match('|/*.epub$|ius', $this->EpubUrl)){ if(!preg_match('|/*.epub$|ius', $this->EpubUrl)){
$error->Add(new Exceptions\InvalidEbookEpubUrlException('Invalid Ebook EpubUrl: ' . $this->EpubUrl)); $error->Add(new Exceptions\InvalidEbookEpubUrlException('Invalid Ebook EpubUrl: ' . $this->EpubUrl));
} }
@ -1342,8 +1324,7 @@ final class Ebook{
if($this->AdvancedEpubUrl == ''){ if($this->AdvancedEpubUrl == ''){
$this->AdvancedEpubUrl = null; $this->AdvancedEpubUrl = null;
} }
else{
if(isset($this->AdvancedEpubUrl)){
if(!preg_match('|/*_advanced.epub$|ius', $this->AdvancedEpubUrl)){ if(!preg_match('|/*_advanced.epub$|ius', $this->AdvancedEpubUrl)){
$error->Add(new Exceptions\InvalidEbookAdvancedEpubUrlException('Invalid Ebook AdvancedEpubUrl: ' . $this->AdvancedEpubUrl)); $error->Add(new Exceptions\InvalidEbookAdvancedEpubUrlException('Invalid Ebook AdvancedEpubUrl: ' . $this->AdvancedEpubUrl));
} }
@ -1357,8 +1338,7 @@ final class Ebook{
if($this->KepubUrl == ''){ if($this->KepubUrl == ''){
$this->KepubUrl = null; $this->KepubUrl = null;
} }
else{
if(isset($this->KepubUrl)){
if(!preg_match('|/*.kepub.epub$|ius', $this->KepubUrl)){ if(!preg_match('|/*.kepub.epub$|ius', $this->KepubUrl)){
$error->Add(new Exceptions\InvalidEbookKepubUrlException('Invalid Ebook KepubUrl: ' . $this->KepubUrl)); $error->Add(new Exceptions\InvalidEbookKepubUrlException('Invalid Ebook KepubUrl: ' . $this->KepubUrl));
} }
@ -1372,8 +1352,7 @@ final class Ebook{
if($this->Azw3Url == ''){ if($this->Azw3Url == ''){
$this->Azw3Url = null; $this->Azw3Url = null;
} }
else{
if(isset($this->Azw3Url)){
if(!preg_match('|/*.azw3$|ius', $this->Azw3Url)){ if(!preg_match('|/*.azw3$|ius', $this->Azw3Url)){
$error->Add(new Exceptions\InvalidEbookAzw3UrlException('Invalid Ebook Azw3Url: ' . $this->Azw3Url)); $error->Add(new Exceptions\InvalidEbookAzw3UrlException('Invalid Ebook Azw3Url: ' . $this->Azw3Url));
} }
@ -1387,8 +1366,7 @@ final class Ebook{
if($this->DistCoverUrl == ''){ if($this->DistCoverUrl == ''){
$this->DistCoverUrl = null; $this->DistCoverUrl = null;
} }
else{
if(isset($this->DistCoverUrl)){
if(!preg_match('|/*cover.jpg$|ius', $this->DistCoverUrl)){ if(!preg_match('|/*cover.jpg$|ius', $this->DistCoverUrl)){
$error->Add(new Exceptions\InvalidEbookDistCoverUrlException('Invalid Ebook DistCoverUrl: ' . $this->DistCoverUrl)); $error->Add(new Exceptions\InvalidEbookDistCoverUrlException('Invalid Ebook DistCoverUrl: ' . $this->DistCoverUrl));
} }
@ -1398,9 +1376,7 @@ final class Ebook{
} }
} }
if(isset($this->Title)){ $this->Title = trim($this->Title ?? '');
$this->Title = trim($this->Title);
if($this->Title == ''){ if($this->Title == ''){
$error->Add(new Exceptions\EbookTitleRequiredException()); $error->Add(new Exceptions\EbookTitleRequiredException());
} }
@ -1411,17 +1387,12 @@ final class Ebook{
if(strlen($this->Title) > EBOOKS_MAX_STRING_LENGTH){ if(strlen($this->Title) > EBOOKS_MAX_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook Title')); $error->Add(new Exceptions\StringTooLongException('Ebook Title'));
} }
}
else{
$error->Add(new Exceptions\EbookTitleRequiredException());
}
$this->FullTitle = trim($this->FullTitle ?? ''); $this->FullTitle = trim($this->FullTitle ?? '');
if($this->FullTitle == ''){ if($this->FullTitle == ''){
$this->FullTitle = null; $this->FullTitle = null;
} }
elseif(strlen($this->FullTitle) > EBOOKS_MAX_STRING_LENGTH){
if(isset($this->FullTitle) && strlen($this->FullTitle) > EBOOKS_MAX_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook FullTitle')); $error->Add(new Exceptions\StringTooLongException('Ebook FullTitle'));
} }
@ -1429,8 +1400,7 @@ final class Ebook{
if($this->AlternateTitle == ''){ if($this->AlternateTitle == ''){
$this->AlternateTitle = null; $this->AlternateTitle = null;
} }
elseif(strlen($this->AlternateTitle) > EBOOKS_MAX_STRING_LENGTH){
if(isset($this->AlternateTitle) && strlen($this->AlternateTitle) > EBOOKS_MAX_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook AlternateTitle')); $error->Add(new Exceptions\StringTooLongException('Ebook AlternateTitle'));
} }
@ -1448,32 +1418,24 @@ final class Ebook{
if($this->Language == ''){ if($this->Language == ''){
$this->Language = null; $this->Language = null;
} }
elseif(strlen($this->Language) > 10){
if(isset($this->Language)){
if(strlen($this->Language) > 10){
$error->Add(new Exceptions\StringTooLongException('Ebook Language: ' . $this->Language)); $error->Add(new Exceptions\StringTooLongException('Ebook Language: ' . $this->Language));
} }
}
if(isset($this->WordCount)){ if(isset($this->WordCount) && $this->WordCount <= 0){
if($this->WordCount <= 0){
$error->Add(new Exceptions\InvalidEbookWordCountException('Invalid Ebook WordCount: ' . $this->WordCount)); $error->Add(new Exceptions\InvalidEbookWordCountException('Invalid Ebook WordCount: ' . $this->WordCount));
} }
}
if(isset($this->ReadingEase)){ if(isset($this->ReadingEase) && $this->ReadingEase <= 0){
// In theory, Flesch reading ease can be negative, but in practice it's positive. // In theory, Flesch reading ease can be negative, but in practice it's positive.
if($this->ReadingEase <= 0){
$error->Add(new Exceptions\InvalidEbookReadingEaseException('Invalid Ebook ReadingEase: ' . $this->ReadingEase)); $error->Add(new Exceptions\InvalidEbookReadingEaseException('Invalid Ebook ReadingEase: ' . $this->ReadingEase));
} }
}
$this->GitHubUrl = trim($this->GitHubUrl ?? ''); $this->GitHubUrl = trim($this->GitHubUrl ?? '');
if($this->GitHubUrl == ''){ if($this->GitHubUrl == ''){
$this->GitHubUrl = null; $this->GitHubUrl = null;
} }
else{
if(isset($this->GitHubUrl)){
if(!preg_match('|^https://github.com/standardebooks/\w+|ius', $this->GitHubUrl)){ if(!preg_match('|^https://github.com/standardebooks/\w+|ius', $this->GitHubUrl)){
$error->Add(new Exceptions\InvalidEbookGitHubUrlException('Invalid Ebook GitHubUrl: ' . $this->GitHubUrl)); $error->Add(new Exceptions\InvalidEbookGitHubUrlException('Invalid Ebook GitHubUrl: ' . $this->GitHubUrl));
} }
@ -1487,8 +1449,7 @@ final class Ebook{
if($this->WikipediaUrl == ''){ if($this->WikipediaUrl == ''){
$this->WikipediaUrl = null; $this->WikipediaUrl = null;
} }
else{
if(isset($this->WikipediaUrl)){
if(!preg_match('|^https://.*wiki.*|ius', $this->WikipediaUrl)){ if(!preg_match('|^https://.*wiki.*|ius', $this->WikipediaUrl)){
$error->Add(new Exceptions\InvalidEbookWikipediaUrlException('Invalid Ebook WikipediaUrl: ' . $this->WikipediaUrl)); $error->Add(new Exceptions\InvalidEbookWikipediaUrlException('Invalid Ebook WikipediaUrl: ' . $this->WikipediaUrl));
} }
@ -1498,29 +1459,20 @@ final class Ebook{
} }
} }
if(isset($this->EbookCreated)){ if(isset($this->EbookCreated) && $this->EbookCreated > NOW){
if($this->EbookCreated > NOW){
$error->Add(new Exceptions\InvalidEbookCreatedDatetimeException($this->EbookCreated)); $error->Add(new Exceptions\InvalidEbookCreatedDatetimeException($this->EbookCreated));
} }
}
if(isset($this->EbookUpdated)){ if(isset($this->EbookUpdated) && $this->EbookUpdated > NOW){
if($this->EbookUpdated > NOW){
$error->Add(new Exceptions\InvalidEbookUpdatedDatetimeException($this->EbookUpdated)); $error->Add(new Exceptions\InvalidEbookUpdatedDatetimeException($this->EbookUpdated));
}
} }
if(isset($this->TextSinglePageByteCount)){ if(isset($this->TextSinglePageByteCount) && $this->TextSinglePageByteCount <= 0){
if($this->TextSinglePageByteCount <= 0){
$error->Add(new Exceptions\InvalidEbookTextSinglePageByteCountException('Invalid Ebook TextSinglePageByteCount: ' . $this->TextSinglePageByteCount)); $error->Add(new Exceptions\InvalidEbookTextSinglePageByteCountException('Invalid Ebook TextSinglePageByteCount: ' . $this->TextSinglePageByteCount));
} }
}
$this->InitializeIndexableProperties(); if(sizeof($this->Authors) == 0){
$error->Add(new Exceptions\EbookAuthorRequiredException());
if($this->IndexableAuthors == ''){
$error->Add(new Exceptions\EbookIndexableAuthorsRequiredException());
} }
if(isset($this->EbookPlaceholder)){ if(isset($this->EbookPlaceholder)){
@ -1812,7 +1764,7 @@ final class Ebook{
/** /**
* Initialize the various indexable properties that are used to search against. * Initialize the various indexable properties that are used to search against.
*/ */
protected function InitializeIndexableProperties(): void{ private function SetIndexableProperties(): void{
// Initialize `IndexableText`. // Initialize `IndexableText`.
$this->IndexableText = $this->FullTitle ?? ''; $this->IndexableText = $this->FullTitle ?? '';
@ -1888,6 +1840,8 @@ final class Ebook{
public function Create(): void{ public function Create(): void{
$this->Validate(); $this->Validate();
$this->SetIndexableProperties();
try{ try{
Ebook::GetByIdentifier($this->Identifier); Ebook::GetByIdentifier($this->Identifier);
throw new Exceptions\EbookExistsException($this->Identifier); throw new Exceptions\EbookExistsException($this->Identifier);
@ -1971,6 +1925,8 @@ final class Ebook{
public function Save(): void{ public function Save(): void{
$this->Validate(); $this->Validate();
$this->SetIndexableProperties();
try{ try{
$this->CreateTags(); $this->CreateTags();
$this->CreateLocSubjects(); $this->CreateLocSubjects();

View file

@ -0,0 +1,7 @@
<?
namespace Exceptions;
class EbookAuthorRequiredException extends AppException{
/** @var string $message */
protected $message = 'Author required.';
}

View file

@ -1,7 +0,0 @@
<?
namespace Exceptions;
class EbookIndexableAuthorsRequiredException extends AppException{
/** @var string $message */
protected $message = 'Ebook IndexableAuthors required.';
}

View file

@ -3,5 +3,5 @@ namespace Exceptions;
class EbookTitleRequiredException extends AppException{ class EbookTitleRequiredException extends AppException{
/** @var string $message */ /** @var string $message */
protected $message = 'Ebook Title required.'; protected $message = 'Title required.';
} }