Skip to content

Commit

Permalink
Reworked SF session dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
fred-jan committed Feb 2, 2024
1 parent 857e17e commit cf8c848
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 133 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"symfony/http-foundation": "^5.4 || ^6.0"
},
"require-dev": {
"phpspec/phpspec": "^7.2",
"phpspec/phpspec": "^7.5",
"symfony/yaml": "^5.4",
"vimeo/psalm": "^4.22",
"friendsofsymfony/rest-bundle": "^3.1"
Expand Down
41 changes: 0 additions & 41 deletions spec/DependencyInjection/Compiler/RegisterSessionBagsPassSpec.php

This file was deleted.

2 changes: 1 addition & 1 deletion spec/Storage/SessionFlowsBagSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use PhpSpec\ObjectBehavior;
use Sylius\Bundle\FlowBundle\Storage\SessionFlowsBag;
use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag;
use Sylius\Bundle\FlowBundle\Symfony\NamespacedAttributeBag;

final class SessionFlowsBagSpec extends ObjectBehavior
{
Expand Down
18 changes: 13 additions & 5 deletions spec/Storage/SessionStorageSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@

use PhpSpec\ObjectBehavior;
use Sylius\Bundle\FlowBundle\Storage\SessionFlowsBag;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

final class SessionStorageSpec extends ObjectBehavior
{
function let(SessionInterface $session, SessionFlowsBag $bag)
function let(RequestStack $requestStack, SessionInterface $session, SessionFlowsBag $bag)
{
$requestStack->getSession()->willReturn($session);
$session->getBag(SessionFlowsBag::NAME)->willReturn($bag);

$this->beConstructedWith($session);
$this->beConstructedWith($requestStack);

$this->initialize('domain');
}
Expand All @@ -42,7 +44,9 @@ function itd_message_is_mutable($bag)

function it_has_message($bag)
{
$bag->get('domain/message_key', null)->shouldBeCalled();
$bag->get('domain/message_key', null)
->willReturn('test')
->shouldBeCalled();

$this->get('message_key');
}
Expand All @@ -56,14 +60,18 @@ function it_checks_is_the_message_exists($bag)

function it_removes_a_message($bag)
{
$bag->remove('domain/message_key')->shouldBeCalled();
$bag->remove('domain/message_key')
->willReturn('test')
->shouldBeCalled();

$this->remove('message_key');
}

function it_removes_all_messages($bag)
{
$bag->remove('domain/message_key')->shouldBeCalled();
$bag->remove('domain/message_key')
->willReturn('test')
->shouldBeCalled();

$this->remove('message_key');
}
Expand Down
33 changes: 0 additions & 33 deletions src/DependencyInjection/Compiler/RegisterSessionBagsPass.php

This file was deleted.

8 changes: 5 additions & 3 deletions src/Resources/config/container/storages.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@

<parameters>
<parameter key="sylius.process_storage.session.class">Sylius\Bundle\FlowBundle\Storage\SessionStorage</parameter>
<parameter key="sylius.process_storage.session.bag.class">Sylius\Bundle\FlowBundle\Storage\SessionFlowsBag</parameter>
</parameters>

<services>
<service id="sylius.process_storage.session" class="%sylius.process_storage.session.class%">
<argument type="service" id="session" />
<argument type="service" id="request_stack" />
</service>

<service id="Sylius\Bundle\FlowBundle\Symfony\SessionFactory" decorates="session.factory">
<argument type="service" id=".inner"/>
</service>
<service id="sylius.process_storage.session.bag" class="%sylius.process_storage.session.bag.class%" />
</services>

</container>
4 changes: 2 additions & 2 deletions src/Storage/SessionFlowsBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Sylius\Bundle\FlowBundle\Storage;

use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag;
use Sylius\Bundle\FlowBundle\Symfony\NamespacedAttributeBag;

/**
* Separate session bag to store flows data.
Expand All @@ -34,7 +34,7 @@ public function __construct()
/**
* {@inheritdoc}
*/
public function getName()
public function getName(): string
{
return self::NAME;
}
Expand Down
54 changes: 9 additions & 45 deletions src/Storage/SessionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Sylius\Bundle\FlowBundle\Storage;

use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* Session storage.
Expand All @@ -20,81 +20,45 @@
*/
class SessionStorage extends AbstractStorage
{
/**
* Session.
*
* @var SessionInterface
*/
protected $session;

/**
* Constructor.
*
* @param SessionInterface $session
*/
public function __construct(SessionInterface $session)
public function __construct(protected RequestStack $requestStack)
{
$this->session = $session;
}

/**
* {@inheritdoc}
*/
public function get($key, $default = null)
{
return $this->getBag()->get($this->resolveKey($key), $default);
}

/**
* {@inheritdoc}
*/
public function set($key, $value)
{
$this->getBag()->set($this->resolveKey($key), $value);
}

/**
* {@inheritdoc}
*/
public function has($key)
{
return $this->getBag()->has($this->resolveKey($key));
}

/**
* {@inheritdoc}
*/
public function remove($key)
{
$this->getBag()->remove($this->resolveKey($key));
}

/**
* {@inheritdoc}
*/
public function clear()
{
$this->getBag()->remove($this->domain);
}

/**
* Get session flows bag.
*
* @return SessionFlowsBag
*/
private function getBag()
private function getBag(): SessionFlowsBag
{
return $this->session->getBag(SessionFlowsBag::NAME);
$bag = $this->requestStack->getSession()->getBag(SessionFlowsBag::NAME);
\assert($bag instanceof SessionFlowsBag);

return $bag;
}

/**
* Resolve key for current domain.
*
* @param string $key
*
* @return string
*/
private function resolveKey($key)
/** Resolve key for current domain. */
private function resolveKey(string $key): string
{
return $this->domain.'/'.$key;
}
Expand Down
2 changes: 0 additions & 2 deletions src/SyliusFlowBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Sylius\Bundle\FlowBundle;

use Sylius\Bundle\FlowBundle\DependencyInjection\Compiler\RegisterScenariosPass;
use Sylius\Bundle\FlowBundle\DependencyInjection\Compiler\RegisterSessionBagsPass;
use Sylius\Bundle\FlowBundle\DependencyInjection\Compiler\RegisterStepsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
Expand All @@ -31,6 +30,5 @@ public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new RegisterScenariosPass());
$container->addCompilerPass(new RegisterStepsPass());
$container->addCompilerPass(new RegisterSessionBagsPass());
}
}
Loading

0 comments on commit cf8c848

Please sign in to comment.