PHP code example of setono / client-bundle

1. Go to this page and download the library: Download setono/client-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

setono / client-bundle example snippets


use Setono\Client\Client;

final class YourController extends AbstractController
{
    public function index(Client $client): Response
    {
        return $this->render('your_template.html.twig', [
            'id' => $client->id,
            'some_metadata' => $client->metadata->get('some_metadata_key'), // this call will initialize the metadata object from the database
        ]);
    }
}

use Setono\Client\Client;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Setono\ClientBundle\Context\ClientContextInterface;

final class YourEventSubscriber implements EventSubscriberInterface
{
    public function __construct(private readonly ClientContextInterface $clientContext)
    {}
    
    public static function getSubscribedEvents(): array
    {
        return [
            KernelEvents::REQUEST => 'setMetadata',
        ];
    }
    
    public function setMetadata(RequestEvent $event): void
    {
        if (!$event->isMainRequest() || !$event->getRequest()->query->has('gclid')) {
            return;
        }
        
        $this->clientContext->getClient()->metadata->set('google_click_id', $event->getRequest()->query->get('gclid'));
    }
}

use Setono\ClientBundle\CookieProvider\CookieProviderInterface;

final class YourService
{
    public function __construct(private readonly CookieProviderInterface $cookieProvider)
    {}
    
    public function call(): void
    {
        $cookie = $this->cookieProvider->getCookie();
        if(null === $cookie) {
            return; // no cookie found
        }
        
        $clientId = $cookie->clientId; // the client id
        $created = $cookie->firstSeenAt; // the timestamp when the client was first seen
        $lastSeen = $cookie->lastSeenAt; // the timestamp when the client was last seen
    }
}
shell
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate