Skip to content

Commit 0c5efcf

Browse files
authored
feature #862 [Maintenance] Add an exception for unavailable storages (NoResponseMate)
This PR was merged into the 1.11 branch. Discussion ---------- | Q | A | --------------- | ----- | Bug fix? | kinda | New feature? | yes? | BC breaks? | no | Deprecations? | no | Related tickets | - | License | MIT There's a bunch of issues open Sylius regarding cli/stateless actions resulting in fatals since session is not available when resolving something within a context. Would be nice to have a unified way of knowing whether the storage medium can be accessed at all. Commits ------- b903e11 [Maintenance] Add an exception for unavailable storages f7bf035 Move StorageExceptionUnavailable to Resource/Exception
2 parents 6fcc1ec + f7bf035 commit 0c5efcf

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

src/Bundle/Storage/SessionStorage.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
namespace Sylius\Bundle\ResourceBundle\Storage;
1515

16+
use Sylius\Resource\Exception\StorageUnavailableException;
1617
use Sylius\Resource\Storage\StorageInterface;
18+
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
1719
use Symfony\Component\HttpFoundation\RequestStack;
1820
use 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
}

src/Bundle/spec/Storage/SessionStorageSpec.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
namespace spec\Sylius\Bundle\ResourceBundle\Storage;
1515

1616
use PhpSpec\ObjectBehavior;
17+
use Sylius\Resource\Exception\StorageUnavailableException;
1718
use Sylius\Resource\Storage\StorageInterface;
19+
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
1820
use Symfony\Component\HttpFoundation\RequestStack;
1921
use Symfony\Component\HttpFoundation\Session\Session;
2022
use 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);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

src/Component/src/Storage/StorageInterface.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,39 @@
1313

1414
namespace Sylius\Resource\Storage;
1515

16+
use Sylius\Resource\Exception\StorageUnavailableException;
17+
1618
interface 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

0 commit comments

Comments
 (0)