<?php

namespace KupShop\GTMBundle\Utils\Handlers;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

class NewsletterHandler
{
    public function __construct(
        protected SessionInterface $session,
        protected RequestStack $requestStack,
    ) {
    }

    /**
     * successNewsletterSubscribe.
     */
    public function subscribe(mixed $email = ''): void
    {
        $referer = Request::create($this->requestStack->getMainRequest()?->headers->get('referer') ?? '');
        $this->session->set('newsletterSubscribe', [
            'value' => $email,
            'path' => $referer?->getPathInfo() ?? '',
        ]);
    }
}
