1 <?php namespace BookStack\Entities;
3 use BookStack\Entities\Repos\EntityRepo;
4 use Illuminate\Session\Store;
6 class EntityContextManager
11 protected $KEY_SHELF_CONTEXT_ID = 'context_bookshelf_id';
14 * EntityContextManager constructor.
15 * @param Store $session
16 * @param EntityRepo $entityRepo
18 public function __construct(Store $session, EntityRepo $entityRepo)
20 $this->session = $session;
21 $this->entityRepo = $entityRepo;
25 * Get the current bookshelf context for the given book.
27 * @return Bookshelf|null
29 public function getContextualShelfForBook(Book $book)
31 $contextBookshelfId = $this->session->get($this->KEY_SHELF_CONTEXT_ID, null);
32 if (is_int($contextBookshelfId)) {
34 /** @var Bookshelf $shelf */
35 $shelf = $this->entityRepo->getById('bookshelf', $contextBookshelfId);
37 if ($shelf && $shelf->contains($book)) {
45 * Store the current contextual shelf ID.
48 public function setShelfContext(int $shelfId)
50 $this->session->put($this->KEY_SHELF_CONTEXT_ID, $shelfId);
54 * Clear the session stored shelf context id.
56 public function clearShelfContext()
58 $this->session->forget($this->KEY_SHELF_CONTEXT_ID);