1 <?php namespace BookStack\Entities\Managers;
3 use BookStack\Entities\Book;
4 use BookStack\Entities\Bookshelf;
5 use Illuminate\Session\Store;
11 protected $KEY_SHELF_CONTEXT_ID = 'context_bookshelf_id';
14 * EntityContextManager constructor.
16 public function __construct(Store $session)
18 $this->session = $session;
22 * Get the current bookshelf context for the given book.
24 public function getContextualShelfForBook(Book $book): ?Bookshelf
26 $contextBookshelfId = $this->session->get($this->KEY_SHELF_CONTEXT_ID, null);
28 if (!is_int($contextBookshelfId)) {
32 $shelf = Bookshelf::visible()->find($contextBookshelfId);
33 $shelfContainsBook = $shelf && $shelf->contains($book);
35 return $shelfContainsBook ? $shelf : null;
39 * Store the current contextual shelf ID.
42 public function setShelfContext(int $shelfId)
44 $this->session->put($this->KEY_SHELF_CONTEXT_ID, $shelfId);
48 * Clear the session stored shelf context id.
50 public function clearShelfContext()
52 $this->session->forget($this->KEY_SHELF_CONTEXT_ID);