generated from Setono/SyliusPluginSkeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from Setono/client-metadata-storage
Allow the user to save the tracking information server side instead of in a cookie
- Loading branch information
Showing
20 changed files
with
323 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"symbol-whitelist": [ | ||
"Setono\\ClientBundle\\Context\\ClientContextInterface" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusGoogleAdsPlugin\EventSubscriber; | ||
|
||
use Setono\SyliusGoogleAdsPlugin\TrackingInformation\TrackingInformation; | ||
use Setono\SyliusGoogleAdsPlugin\TrackingInformation\TrackingInformationStorageInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
use Symfony\Component\HttpKernel\Event\ResponseEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
|
||
/** | ||
* This subscriber is responsible for migrating the storage of the tracking information from the cookie to the client metadata. | ||
* This subscriber should only be used when the storage is set to client_metadata | ||
*/ | ||
final class MigrateStorageSubscriber implements EventSubscriberInterface | ||
{ | ||
private ?TrackingInformation $trackingInformation = null; | ||
|
||
public function __construct( | ||
private readonly TrackingInformationStorageInterface $trackingInformationStorage, | ||
private readonly string $cookieName, | ||
) { | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
KernelEvents::REQUEST => ['migrate', 5], // we want to run this before the StoreTrackingInformationSubscriber | ||
KernelEvents::RESPONSE => 'remove', | ||
]; | ||
} | ||
|
||
public function migrate(RequestEvent $event): void | ||
{ | ||
if (!$event->isMainRequest()) { | ||
return; | ||
} | ||
|
||
try { | ||
$this->trackingInformation = TrackingInformation::fromCookie($event->getRequest(), $this->cookieName); | ||
} catch (\Throwable) { | ||
return; | ||
} | ||
|
||
$this->trackingInformationStorage->store($this->trackingInformation); | ||
} | ||
|
||
public function remove(ResponseEvent $event): void | ||
{ | ||
if (null === $this->trackingInformation || !$event->isMainRequest()) { | ||
return; | ||
} | ||
|
||
try { | ||
$cookie = $this->trackingInformation->toCookie($this->cookieName, 1); | ||
} catch (\Throwable) { | ||
return; | ||
} | ||
|
||
$event->getResponse()->headers->setCookie($cookie); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/Resources/config/services/conditional/storage_client_metadata.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
|
||
<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://symfony.com/schema/dic/services" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service id="setono_sylius_google_ads.tracking_information.storage.default" | ||
alias="setono_sylius_google_ads.tracking_information.storage.client_metadata_based"/> | ||
|
||
<service id="setono_sylius_google_ads.tracking_information.storage.client_metadata_based" | ||
class="Setono\SyliusGoogleAdsPlugin\TrackingInformation\ClientMetadataBasedTrackingInformationStorage"> | ||
<argument type="service" id="setono_client.client_context.default"/> | ||
|
||
<call method="setLogger"> | ||
<argument type="service" id="logger"/> | ||
</call> | ||
|
||
<tag name="kernel.event_subscriber"/> | ||
</service> | ||
|
||
<service id="setono_sylius_google_ads.event_subscriber.migrate_storage" | ||
class="Setono\SyliusGoogleAdsPlugin\EventSubscriber\MigrateStorageSubscriber"> | ||
<argument type="service" id="setono_sylius_google_ads.tracking_information.storage.default"/> | ||
<argument>%setono_sylius_google_ads.cookie_name%</argument> | ||
|
||
<tag name="kernel.event_subscriber"/> | ||
</service> | ||
</services> | ||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/TrackingInformation/AbstractTrackingInformationStorage.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusGoogleAdsPlugin\TrackingInformation; | ||
|
||
use Psr\Log\LoggerAwareInterface; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\NullLogger; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Event\ResponseEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
|
||
abstract class AbstractTrackingInformationStorage implements TrackingInformationStorageInterface, EventSubscriberInterface, LoggerAwareInterface | ||
{ | ||
protected LoggerInterface $logger; | ||
|
||
protected ?TrackingInformation $trackingInformation = null; | ||
|
||
public function __construct() | ||
{ | ||
$this->logger = new NullLogger(); | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
KernelEvents::RESPONSE => 'persist', | ||
]; | ||
} | ||
|
||
public function store(Request|TrackingInformation $value): void | ||
{ | ||
if ($value instanceof Request) { | ||
try { | ||
$value = TrackingInformation::fromQuery($value); | ||
} catch (\InvalidArgumentException) { | ||
return; | ||
} | ||
} | ||
|
||
$this->trackingInformation = $value; | ||
} | ||
|
||
abstract public function persist(ResponseEvent $event): void; | ||
|
||
public function setLogger(LoggerInterface $logger): void | ||
{ | ||
$this->logger = $logger; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/TrackingInformation/ClientMetadataBasedTrackingInformationStorage.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusGoogleAdsPlugin\TrackingInformation; | ||
|
||
use Setono\ClientBundle\Context\ClientContextInterface; | ||
use Symfony\Component\HttpKernel\Event\ResponseEvent; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class ClientMetadataBasedTrackingInformationStorage extends AbstractTrackingInformationStorage | ||
{ | ||
public function __construct( | ||
private readonly ClientContextInterface $clientContext, | ||
private readonly string $metadataKey = 'ssga_tracking_information', | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
public function get(): ?TrackingInformation | ||
{ | ||
$clientMetadata = $this->clientContext->getClient()->metadata; | ||
if (!$clientMetadata->has($this->metadataKey)) { | ||
return null; | ||
} | ||
|
||
try { | ||
$data = $clientMetadata->get($this->metadataKey); | ||
Assert::isArray($data); | ||
|
||
return TrackingInformation::fromArray($data); | ||
} catch (\InvalidArgumentException) { | ||
// the data is corrupted, remove it | ||
$clientMetadata->remove($this->metadataKey); | ||
|
||
return null; | ||
} | ||
} | ||
|
||
public function persist(ResponseEvent $event): void | ||
{ | ||
if (null === $this->trackingInformation) { | ||
return; | ||
} | ||
|
||
$this->clientContext->getClient()->metadata->set($this->metadataKey, $this->trackingInformation); | ||
} | ||
} |
Oops, something went wrong.