forked from biblioverse/biblioteca
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
225 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace App\Kobo\Response; | ||
|
||
use App\Entity\Book; | ||
use App\Entity\BookmarkUser; | ||
use App\Entity\KoboDevice; | ||
use App\Kobo\SyncToken; | ||
use App\Service\BookProgressionService; | ||
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
|
||
class ReadingStateResponse | ||
{ | ||
public function __construct( | ||
protected BookProgressionService $bookProgressionService, | ||
protected SerializerInterface $serializer, | ||
protected SyncToken $syncToken, | ||
protected KoboDevice $kobo, | ||
protected Book $book, | ||
) { | ||
} | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public function createReadingState(): array | ||
{ | ||
$book = $this->book; | ||
$uuid = $book->getUuid(); | ||
|
||
$lastModified = $this->syncToken->maxLastModified($book->getUpdated(), $this->syncToken->currentDate, $book->getLastInteraction($this->kobo->getUser())?->getUpdated()); | ||
|
||
return [ | ||
'EntitlementId' => $uuid, | ||
'Created' => $this->syncToken->maxLastCreated($book->getCreated(), $this->syncToken->currentDate, $book->getLastInteraction($this->kobo->getUser())?->getCreated()), | ||
'LastModified' => $lastModified, | ||
'PriorityTimestamp' => $lastModified, | ||
'StatusInfo' => [ | ||
'LastModified' => $lastModified, | ||
'Status' => match ($this->isReadingFinished($book)) { | ||
true => SyncResponse::READING_STATUS_FINISHED, | ||
false => SyncResponse::READING_STATUS_IN_PROGRESS, | ||
null => SyncResponse::READING_STATUS_UNREAD, | ||
}, | ||
'TimesStartedReading' => 0, | ||
], | ||
|
||
// "Statistics"=> get_statistics_response(kobo_reading_state.statistics), | ||
'CurrentBookmark' => $this->createBookmark($this->kobo->getUser()->getBookmarkForBook($book)), | ||
]; | ||
} | ||
|
||
/** | ||
* @return bool|null Null if we do not now the reading state | ||
*/ | ||
private function isReadingFinished(Book $book): ?bool | ||
{ | ||
$progression = $this->bookProgressionService->getProgression($book, $this->kobo->getUser()); | ||
if ($progression === null) { | ||
return null; | ||
} | ||
|
||
return $progression >= 1.0; | ||
} | ||
|
||
private function createBookmark(?BookmarkUser $bookMark): array | ||
{ | ||
if (!$bookMark instanceof BookmarkUser) { | ||
return []; | ||
} | ||
|
||
$values = [ | ||
'Location' => [ | ||
'Type' => $bookMark->getLocationType(), | ||
'Value' => $bookMark->getLocationValue(), | ||
'Source' => $bookMark->getLocationSource(), | ||
], | ||
'ProgressPercent' => $bookMark->getPercentAsInt(), | ||
'ContentSourceProgressPercent' => $bookMark->getSourcePercentAsInt(), | ||
]; | ||
|
||
if (false === $bookMark->hasLocation()) { | ||
unset($values['Location']); | ||
} | ||
|
||
return array_filter($values); // Remove null values | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->serializer->serialize($this->createReadingState(), 'json', [DateTimeNormalizer::FORMAT_KEY => SyncResponse::DATE_FORMAT]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace App\Kobo\Response; | ||
|
||
use App\Entity\Book; | ||
use App\Entity\KoboDevice; | ||
use App\Kobo\SyncToken; | ||
use App\Service\BookProgressionService; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
|
||
/** | ||
* Inspired by https://github.com/janeczku/calibre-web/blob/master/cps/kobo.py | ||
*/ | ||
class ReadingStateResponseFactory | ||
{ | ||
public function __construct( | ||
protected MetadataResponseService $metadataResponseService, | ||
protected BookProgressionService $bookProgressionService, | ||
protected SerializerInterface $serializer) | ||
{ | ||
} | ||
|
||
public function create(SyncToken $syncToken, KoboDevice $kobo, Book $book): ReadingStateResponse | ||
{ | ||
return new ReadingStateResponse( | ||
$this->bookProgressionService, | ||
$this->serializer, | ||
$syncToken, | ||
$kobo, | ||
$book | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,6 @@ public function __construct(Book $book) | |
], | ||
], | ||
], | ||
], Response::HTTP_NO_CONTENT); | ||
], Response::HTTP_OK); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.