Skip to content

Commit e9b36c7

Browse files
authored
Merge pull request #8 from BitBagCommerce/feature/UC-16-add-logout-event
[UC-16] Add logout event
2 parents bbcb5ec + c95d121 commit e9b36c7

4 files changed

Lines changed: 117 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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\DependencyInjection;
13+
14+
use BitBag\SyliusUserComPlugin\Security\Handler\LogoutEventAddHandler;
15+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Definition;
18+
use Symfony\Component\DependencyInjection\Reference;
19+
use Symfony\Component\Security\Http\Event\LogoutEvent;
20+
use Webmozart\Assert\Assert;
21+
22+
final class LogoutSubscriberPass implements CompilerPassInterface
23+
{
24+
public function process(ContainerBuilder $container): void
25+
{
26+
if (!$container->hasParameter('sylius_shop.firewall_context_name')) {
27+
return;
28+
}
29+
30+
$firewallName = $container->getParameter('sylius_shop.firewall_context_name');
31+
Assert::string($firewallName);
32+
$securityDispatcherId = sprintf('security.event_dispatcher.%s', $firewallName);
33+
34+
if (!$container->hasDefinition($securityDispatcherId)) {
35+
return;
36+
}
37+
38+
$logoutListener = new Definition(LogoutEventAddHandler::class, [
39+
new Reference('Spinbits\SyliusGoogleAnalytics4Plugin\Storage\EventsBag'),
40+
]);
41+
42+
$logoutListener->addTag('kernel.event_listener', [
43+
'event' => LogoutEvent::class,
44+
'dispatcher' => $securityDispatcherId,
45+
'method' => 'onLogout',
46+
]);
47+
$logoutListener->setPublic(true);
48+
49+
$container->setDefinition('bitbag.handler.user_logout_add_event', $logoutListener);
50+
}
51+
}
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)