File tree Expand file tree Collapse file tree 4 files changed +65
-5
lines changed
Expand file tree Collapse file tree 4 files changed +65
-5
lines changed Original file line number Diff line number Diff line change 1313
1414namespace Sylius \Bundle \ResourceBundle \Storage ;
1515
16+ use Sylius \Resource \Exception \StorageUnavailableException ;
1617use Sylius \Resource \Storage \StorageInterface ;
18+ use Symfony \Component \HttpFoundation \Exception \SessionNotFoundException ;
1719use Symfony \Component \HttpFoundation \RequestStack ;
1820use Symfony \Component \HttpFoundation \Session \SessionInterface ;
1921
@@ -71,13 +73,20 @@ public function all(): array
7173 return $ this ->getSession ()->all ();
7274 }
7375
76+ /**
77+ * @throws StorageUnavailableException
78+ */
7479 private function getSession (): SessionInterface
7580 {
76- if ($ this ->requestStack instanceof SessionInterface) {
77- return $ this ->requestStack ;
78- }
81+ try {
82+ if ($ this ->requestStack instanceof SessionInterface) {
83+ return $ this ->requestStack ;
84+ }
7985
80- /** @phpstan-ignore-next-line */
81- return $ this ->requestStack ->getSession ();
86+ /** @phpstan-ignore-next-line */
87+ return $ this ->requestStack ->getSession ();
88+ } catch (SessionNotFoundException $ exception ) {
89+ throw new StorageUnavailableException ($ exception ->getMessage (), $ exception );
90+ }
8291 }
8392}
Original file line number Diff line number Diff line change 1414namespace spec \Sylius \Bundle \ResourceBundle \Storage ;
1515
1616use PhpSpec \ObjectBehavior ;
17+ use Sylius \Resource \Exception \StorageUnavailableException ;
1718use Sylius \Resource \Storage \StorageInterface ;
19+ use Symfony \Component \HttpFoundation \Exception \SessionNotFoundException ;
1820use Symfony \Component \HttpFoundation \RequestStack ;
1921use Symfony \Component \HttpFoundation \Session \Session ;
2022use Symfony \Component \HttpFoundation \Session \Storage \MockArraySessionStorage ;
@@ -38,6 +40,18 @@ function it_is_a_storage(): void
3840 $ this ->shouldImplement (StorageInterface::class);
3941 }
4042
43+ function it_throws_storage_unavailable_exception_when_there_is_no_session (RequestStack $ requestStack ): void
44+ {
45+ $ requestStack ->getSession ()->willThrow (SessionNotFoundException::class);
46+
47+ $ call = $ this ->shouldThrow (StorageUnavailableException::class);
48+ $ call ->during ('has ' , ['name ' ]);
49+ $ call ->during ('get ' , ['name ' ]);
50+ $ call ->during ('set ' , ['name ' , 'value ' ]);
51+ $ call ->during ('remove ' , ['name ' ]);
52+ $ call ->during ('all ' );
53+ }
54+
4155 function it_does_not_have_a_named_value_if_it_was_not_set_previously (): void
4256 {
4357 $ this ->get ('name ' )->shouldReturn (null );
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /*
4+ * This file is part of the Sylius package.
5+ *
6+ * (c) Sylius Sp. z o.o.
7+ *
8+ * For the full copyright and license information, please view the LICENSE
9+ * file that was distributed with this source code.
10+ */
11+
12+ declare (strict_types=1 );
13+
14+ namespace Sylius \Resource \Exception ;
15+
16+ class StorageUnavailableException extends RuntimeException
17+ {
18+ public function __construct (string $ message = '' , \Exception $ previous = null )
19+ {
20+ parent ::__construct ($ message , 0 , $ previous );
21+ }
22+ }
Original file line number Diff line number Diff line change 1313
1414namespace Sylius \Resource \Storage ;
1515
16+ use Sylius \Resource \Exception \StorageUnavailableException ;
17+
1618interface StorageInterface
1719{
20+ /**
21+ * @throws StorageUnavailableException
22+ */
1823 public function has (string $ name ): bool ;
1924
2025 /**
2126 * @param mixed $default
2227 *
2328 * @return mixed
29+ *
30+ * @throws StorageUnavailableException
2431 */
2532 public function get (string $ name , $ default = null );
2633
2734 /**
2835 * @param mixed $value
36+ *
37+ * @throws StorageUnavailableException
2938 */
3039 public function set (string $ name , $ value ): void ;
3140
41+ /**
42+ * @throws StorageUnavailableException
43+ */
3244 public function remove (string $ name ): void ;
3345
46+ /**
47+ * @throws StorageUnavailableException
48+ */
3449 public function all (): array ;
3550}
3651
You can’t perform that action at this time.
0 commit comments