Skip to content

Commit

Permalink
Notifications were not displayed after redirection - Session added
Browse files Browse the repository at this point in the history
  • Loading branch information
m-adamski committed Nov 5, 2024
1 parent cc22d9b commit 19bb655
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
17 changes: 12 additions & 5 deletions src/Helper/NotificationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@
namespace Adamski\Symfony\NotificationBundle\Helper;

use Adamski\Symfony\NotificationBundle\Model\Notification;
use Symfony\Component\HttpFoundation\RequestStack;

class NotificationHelper {
const DEFAULT_NAMESPACE = "_application.notifications";
private array $notifications = [];
const DEFAULT_NAMESPACE = "_application.session.notifications";

public function __construct(
private readonly RequestStack $requestStack,
) {}

/**
* Add notification to the namespace collection.
*
* @param Notification $notification
* @param string $namespace
*
* @return $this
*/
public function add(Notification $notification, string $namespace = self::DEFAULT_NAMESPACE): self {
$this->notifications[$namespace][] = $notification;
$this->requestStack->getSession()->set($namespace, [...$this->get($namespace), $notification]);

return $this;
}
Expand All @@ -25,20 +30,22 @@ public function add(Notification $notification, string $namespace = self::DEFAUL
* Get notification from the namespace collection.
*
* @param string $namespace
*
* @return array
*/
public function get(string $namespace = self::DEFAULT_NAMESPACE): array {
return $this->notifications[$namespace] ?? [];
return $this->requestStack->getSession()->remove($namespace) ?? [];
}

/**
* Clear notifications in the namespace collection.
*
* @param string $namespace
*
* @return $this
*/
public function clear(string $namespace = self::DEFAULT_NAMESPACE): self {
$this->notifications[$namespace] = [];
$this->requestStack->getSession()->set($namespace, []);

return $this;
}
Expand Down
11 changes: 9 additions & 2 deletions src/Model/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ class Notification {
public function __construct(
private Type $type,
private string $message,
) {
}
) {}

public function getType(): Type {
return $this->type;
Expand All @@ -26,4 +25,12 @@ public function setMessage(string $message): Notification {
$this->message = $message;
return $this;
}

public function __serialize(): array {
return [$this->getType(), $this->getMessage()];
}

public function __unserialize(array $data): void {
list($this->type, $this->message) = $data;
}
}
2 changes: 2 additions & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
services:
notification_bundle.helper.notification:
class: Adamski\Symfony\NotificationBundle\Helper\NotificationHelper
arguments:
- '@request_stack'

Adamski\Symfony\NotificationBundle\Helper\NotificationHelper:
alias: notification_bundle.helper.notification
Expand Down
29 changes: 28 additions & 1 deletion tests/Helper/NotificationHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@
use Adamski\Symfony\NotificationBundle\Model\Notification;
use Adamski\Symfony\NotificationBundle\Model\Type;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;

class NotificationHelperTest extends TestCase {
protected array $notificationBag;
protected NotificationHelper $notificationHelper;

protected function setUp(): void {
$this->notificationHelper = new NotificationHelper();
$sessionMock = $this->createMock(Session::class);
$sessionMock->method("set")->willReturnCallback([$this, "setSession"]);
$sessionMock->method("get")->willReturnCallback([$this, "getSession"]);
$sessionMock->method("remove")->willReturnCallback([$this, "removeSession"]);

$requestStackMock = $this->createMock(RequestStack::class);
$requestStackMock->method("getSession")->willReturn($sessionMock);

$this->notificationBag = [];
$this->notificationHelper = new NotificationHelper($requestStackMock);
}

public function testCollection(): void {
Expand Down Expand Up @@ -41,4 +53,19 @@ public function testClear(): void {
$this->notificationHelper->clear();
$this->assertEquals([], $this->notificationHelper->get());
}

public function getSession(string $namespace): array {
return array_key_exists($namespace, $this->notificationBag) ? $this->notificationBag[$namespace] : [];
}

public function removeSession(string $namespace): array {
$sessionValue = $this->getSession($namespace);
$this->notificationBag[$namespace] = [];

return $sessionValue;
}

public function setSession(string $namespace, array $notification): void {
$this->notificationBag[$namespace] = $notification;
}
}

0 comments on commit 19bb655

Please sign in to comment.