mirror of
https://github.com/standardebooks/web.git
synced 2025-07-19 04:44:48 -04:00
Record and summarize Ebook downloads in the DB (#498)
This commit is contained in:
parent
61b8ca27b1
commit
475c437126
12 changed files with 289 additions and 4 deletions
52
lib/EbookDownloadSummary.php
Normal file
52
lib/EbookDownloadSummary.php
Normal file
|
@ -0,0 +1,52 @@
|
|||
<?
|
||||
|
||||
use Safe\DateTimeImmutable;
|
||||
|
||||
class EbookDownloadSummary{
|
||||
public int $EbookId;
|
||||
public DateTimeImmutable $Date;
|
||||
public int $DownloadCount = 0;
|
||||
public int $BotDownloadCount = 0;
|
||||
|
||||
public function __construct(int $ebookId, DateTimeImmutable $date){
|
||||
$this->EbookId = $ebookId;
|
||||
$this->Date = $date;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exceptions\InvalidEbookDownloadSummaryException
|
||||
*/
|
||||
public function Validate(): void{
|
||||
$error = new Exceptions\InvalidEbookDownloadSummaryException();
|
||||
|
||||
if($this->DownloadCount < 0){
|
||||
$error->Add(new Exceptions\InvalidEbookDownloadCountException('Invalid EbookDownloadSummary DownloadCount: ' . $this->DownloadCount));
|
||||
}
|
||||
|
||||
if($this->BotDownloadCount < 0){
|
||||
$error->Add(new Exceptions\InvalidEbookDownloadCountException('Invalid EbookDownloadSummary BotDownloadCount: ' . $this->BotDownloadCount));
|
||||
}
|
||||
|
||||
if($error->HasExceptions){
|
||||
throw $error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exceptions\InvalidEbookDownloadSummaryException
|
||||
*/
|
||||
public function Create(): void{
|
||||
$this->Validate();
|
||||
|
||||
Db::Query('
|
||||
INSERT into EbookDownloadSummaries (EbookId, Date, DownloadCount, BotDownloadCount)
|
||||
values (?,
|
||||
?,
|
||||
?,
|
||||
?)
|
||||
on duplicate key update
|
||||
DownloadCount = value(DownloadCount),
|
||||
BotDownloadCount = value(BotDownloadCount)
|
||||
', [$this->EbookId, $this->Date, $this->DownloadCount, $this->BotDownloadCount]);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue