|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace GrahamCampbell\Parse; |
| 4 | + |
| 5 | +use Illuminate\Session\SessionManager; |
| 6 | +use Parse\ParseStorageInterface; |
| 7 | + |
| 8 | +class ParseSessionStorage implements ParseStorageInterface |
| 9 | +{ |
| 10 | + /** |
| 11 | + * @var \Illuminate\Session\SessionManager |
| 12 | + */ |
| 13 | + private $session; |
| 14 | + |
| 15 | + /** |
| 16 | + * @param \Illuminate\Session\SessionManager $session |
| 17 | + */ |
| 18 | + public function __construct(SessionManager $session) |
| 19 | + { |
| 20 | + $this->session = $session; |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Sets a key-value pair in storage. |
| 25 | + * |
| 26 | + * @param string $key The key to set |
| 27 | + * @param mixed $value The value to set |
| 28 | + * |
| 29 | + * @return null |
| 30 | + */ |
| 31 | + public function set($key, $value) |
| 32 | + { |
| 33 | + $this->session->put($key, $value); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Remove a key from storage. |
| 38 | + * |
| 39 | + * @param string $key The key to remove. |
| 40 | + * |
| 41 | + * @return null |
| 42 | + */ |
| 43 | + public function remove($key) |
| 44 | + { |
| 45 | + $this->session->forget($key); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Gets the value for a key from storage. |
| 50 | + * |
| 51 | + * @param string $key The key to get the value for |
| 52 | + * |
| 53 | + * @return mixed |
| 54 | + */ |
| 55 | + public function get($key) |
| 56 | + { |
| 57 | + if ($this->session->has($key)) { |
| 58 | + return $this->session->get($key); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Clear all the values in storage. |
| 64 | + * |
| 65 | + * @return null |
| 66 | + */ |
| 67 | + public function clear() |
| 68 | + { |
| 69 | + $this->session->forget(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Save the data, if necessary. This would be a no-op when using the |
| 74 | + * $_SESSION implementation, but could be used for saving to file or |
| 75 | + * database as an action instead of on every set. |
| 76 | + * |
| 77 | + * @return null |
| 78 | + */ |
| 79 | + public function save() |
| 80 | + { |
| 81 | + // |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Get all keys in storage. |
| 86 | + * |
| 87 | + * @return array |
| 88 | + */ |
| 89 | + public function getKeys() |
| 90 | + { |
| 91 | + return array_keys($this->session->get()); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Get all key-value pairs from storage. |
| 96 | + * |
| 97 | + * @return array |
| 98 | + */ |
| 99 | + public function getAll() |
| 100 | + { |
| 101 | + return $this->session->get(); |
| 102 | + } |
| 103 | +} |
0 commit comments