+ /**
+ * Prepare the local entity cache and ensure it's empty
+ */
+ protected function readyEntityCache()
+ {
+ $this->entityCache = [
+ 'books' => collect(),
+ 'chapters' => collect()
+ ];
+ }
+
+ /**
+ * Get a book via ID, Checks local cache
+ * @param $bookId
+ * @return Book
+ */
+ protected function getBook($bookId)
+ {
+ if (isset($this->entityCache['books']) && $this->entityCache['books']->has($bookId)) {
+ return $this->entityCache['books']->get($bookId);
+ }
+
+ $book = $this->book->find($bookId);
+ if ($book === null) $book = false;
+ if (isset($this->entityCache['books'])) {
+ $this->entityCache['books']->put($bookId, $book);
+ }
+
+ return $book;
+ }
+
+ /**
+ * Get a chapter via ID, Checks local cache
+ * @param $chapterId
+ * @return Book
+ */
+ protected function getChapter($chapterId)
+ {
+ if (isset($this->entityCache['chapters']) && $this->entityCache['chapters']->has($chapterId)) {
+ return $this->entityCache['chapters']->get($chapterId);
+ }
+
+ $chapter = $this->chapter->find($chapterId);
+ if ($chapter === null) $chapter = false;
+ if (isset($this->entityCache['chapters'])) {
+ $this->entityCache['chapters']->put($chapterId, $chapter);
+ }
+
+ return $chapter;
+ }
+