Skip to content

Commit 25f905a

Browse files
committed
Add logout event
1 parent bbcb5ec commit 25f905a

4 files changed

Lines changed: 110 additions & 0 deletions

File tree

src/BitBagSyliusUserComPlugin.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@
44

55
namespace BitBag\SyliusUserComPlugin;
66

7+
use BitBag\SyliusUserComPlugin\DependencyInjection\LogoutSubscriberPass;
78
use Sylius\Bundle\CoreBundle\Application\SyliusPluginTrait;
9+
use Symfony\Component\DependencyInjection\ContainerBuilder;
810
use Symfony\Component\HttpKernel\Bundle\Bundle;
911

1012
final class BitBagSyliusUserComPlugin extends Bundle
1113
{
1214
use SyliusPluginTrait;
1315

16+
public function build(ContainerBuilder $container): void
17+
{
18+
$container->addCompilerPass(new LogoutSubscriberPass());
19+
}
20+
1421
public function getPath(): string
1522
{
1623
return \dirname(__DIR__);

src/DTO/Event/Logout.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusUserComPlugin\DTO\Event;
13+
14+
use Spinbits\GoogleAnalytics4EventsDtoS\Events\EventInterface;
15+
16+
final class Logout implements EventInterface
17+
{
18+
private const LOGOUT = 'logout';
19+
20+
public function getName(): string
21+
{
22+
return self::LOGOUT;
23+
}
24+
25+
public function __toString(): string
26+
{
27+
return '';
28+
}
29+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BitBag\SyliusUserComPlugin\DependencyInjection;
6+
7+
use BitBag\SyliusUserComPlugin\Security\Handler\LogoutEventAddHandler;
8+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
9+
use Symfony\Component\DependencyInjection\ContainerBuilder;
10+
use Symfony\Component\DependencyInjection\Definition;
11+
use Symfony\Component\DependencyInjection\Reference;
12+
use Symfony\Component\Security\Http\Event\LogoutEvent;
13+
use Webmozart\Assert\Assert;
14+
15+
final class LogoutSubscriberPass implements CompilerPassInterface
16+
{
17+
public function process(ContainerBuilder $container): void
18+
{
19+
if (!$container->hasParameter('sylius_shop.firewall_context_name')) {
20+
return;
21+
}
22+
23+
$firewallName = $container->getParameter('sylius_shop.firewall_context_name');
24+
Assert::string($firewallName);
25+
$securityDispatcherId = sprintf('security.event_dispatcher.%s', $firewallName);
26+
27+
if (!$container->hasDefinition($securityDispatcherId)) {
28+
return;
29+
}
30+
31+
$logoutListener = new Definition(LogoutEventAddHandler::class, [
32+
new Reference('Spinbits\SyliusGoogleAnalytics4Plugin\Storage\EventsBag'),
33+
]);
34+
35+
$logoutListener->addTag('kernel.event_listener', [
36+
'event' => LogoutEvent::class,
37+
'dispatcher' => $securityDispatcherId,
38+
'method' => 'onLogout',
39+
]);
40+
$logoutListener->setPublic(true);
41+
42+
$container->setDefinition('bitbag.handler.user_logout_add_event', $logoutListener);
43+
}
44+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusUserComPlugin\Security\Handler;
13+
14+
use BitBag\SyliusUserComPlugin\DTO\Event\Logout;
15+
use Spinbits\SyliusGoogleAnalytics4Plugin\Storage\EventsBag;
16+
17+
final class LogoutEventAddHandler
18+
{
19+
public function __construct(
20+
private readonly EventsBag $eventsStorage,
21+
) {
22+
}
23+
24+
public function onLogout(): void
25+
{
26+
$this->eventsStorage->setEvent(
27+
new Logout(),
28+
);
29+
}
30+
}

0 commit comments

Comments
 (0)