]> BookStack Code Mirror - bookstack/blob - app/Entities/EntityContextManager.php
Update validation.php
[bookstack] / app / Entities / EntityContextManager.php
1 <?php namespace BookStack\Entities;
2
3 use BookStack\Entities\Repos\EntityRepo;
4 use Illuminate\Session\Store;
5
6 class EntityContextManager
7 {
8     protected $session;
9     protected $entityRepo;
10
11     protected $KEY_SHELF_CONTEXT_ID = 'context_bookshelf_id';
12
13     /**
14      * EntityContextManager constructor.
15      * @param Store $session
16      * @param EntityRepo $entityRepo
17      */
18     public function __construct(Store $session, EntityRepo $entityRepo)
19     {
20         $this->session = $session;
21         $this->entityRepo = $entityRepo;
22     }
23
24     /**
25      * Get the current bookshelf context for the given book.
26      * @param Book $book
27      * @return Bookshelf|null
28      */
29     public function getContextualShelfForBook(Book $book)
30     {
31         $contextBookshelfId = $this->session->get($this->KEY_SHELF_CONTEXT_ID, null);
32         if (is_int($contextBookshelfId)) {
33
34             /** @var Bookshelf $shelf */
35             $shelf = $this->entityRepo->getById('bookshelf', $contextBookshelfId);
36
37             if ($shelf && $shelf->contains($book)) {
38                 return $shelf;
39             }
40         }
41         return null;
42     }
43
44     /**
45      * Store the current contextual shelf ID.
46      * @param int $shelfId
47      */
48     public function setShelfContext(int $shelfId)
49     {
50         $this->session->put($this->KEY_SHELF_CONTEXT_ID, $shelfId);
51     }
52
53     /**
54      * Clear the session stored shelf context id.
55      */
56     public function clearShelfContext()
57     {
58         $this->session->forget($this->KEY_SHELF_CONTEXT_ID);
59     }
60 }