<commit_before><?php

namespace App\EventListener;

use App\Entity\Referer\Referer;
use App\Service\RefererService;
use App\Traits\LoggerTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * RefererRequestSubscriber.
 */
class RefererRequestSubscriber implements EventSubscriberInterface
{
    use LoggerTrait;

    /** @var RefererService */
    private $refererService;

    /** @var string|null */
    private $newCookie = null;

    /**
     * @param RefererService $refererService
     */
    public function __construct(RefererService $refererService)
    {
        $this->refererService = $refererService;
    }

    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents(): \Generator
    {
        yield KernelEvents::REQUEST => ['onKernelRequest', 1];
        yield KernelEvents::RESPONSE => ['onKernelResponse', 1];
    }

    /**
     * @param ResponseEvent $event
     */
    public function onKernelResponse(ResponseEvent $event): void
    {
        if (!$event->isMasterRequest()) {
            return;
        }

        $request = $event->getRequest();

        if ($request->isXmlHttpRequest()) {
            return;
        }

        if (\is_string($this->newCookie)) {
            $cookie = new Cookie(Referer::COOKIE_KEY, $this->newCookie, \time() + 365 * 24 * 60 * 60);
            $this->newCookie = null;
            $event->getResponse()->headers->setCookie($cookie);
        }
    }

    /**
     * @param RequestEvent $event
     */
    public function onKernelRequest(RequestEvent $event): void
    {
        if (!$event->isMasterRequest()) {
            return;
        }

        $request = $event->getRequest();

        if ($request->isXmlHttpRequest()) {
            return;
        }

        $refererUrl = $request->headers->get('referer', null);
        if (\is_string($refererUrl)) {
            $cookieId = $request->cookies->get(Referer::COOKIE_KEY, null);

            $refererDomain = $this->getInnerSubstring($refererUrl, '/', 2);
            $currentDomain = $request->getHttpHost();
            $pageRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && ($_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0' ||  $_SERVER['HTTP_CACHE_CONTROL'] == 'no-cache');<commit_msg>Remove page refresh.<commit_after><?php

namespace App\EventListener;

use App\Entity\Referer\Referer;
use App\Service\RefererService;
use App\Traits\LoggerTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * RefererRequestSubscriber.
 */
class RefererRequestSubscriber implements EventSubscriberInterface
{
    use LoggerTrait;

    /** @var RefererService */
    private $refererService;

    /** @var string|null */
    private $newCookie = null;

    /**
     * @param RefererService $refererService
     */
    public function __construct(RefererService $refererService)
    {
        $this->refererService = $refererService;
    }

    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents(): \Generator
    {
        yield KernelEvents::REQUEST => ['onKernelRequest', 1];
        yield KernelEvents::RESPONSE => ['onKernelResponse', 1];
    }

    /**
     * @param ResponseEvent $event
     */
    public function onKernelResponse(ResponseEvent $event): void
    {
        if (!$event->isMasterRequest()) {
            return;
        }

        $request = $event->getRequest();

        if ($request->isXmlHttpRequest()) {
            return;
        }

        if (\is_string($this->newCookie)) {
            $cookie = new Cookie(Referer::COOKIE_KEY, $this->newCookie, \time() + 365 * 24 * 60 * 60);
            $this->newCookie = null;
            $event->getResponse()->headers->setCookie($cookie);
        }
    }

    /**
     * @param RequestEvent $event
     */
    public function onKernelRequest(RequestEvent $event): void
    {
        if (!$event->isMasterRequest()) {
            return;
        }

        $request = $event->getRequest();

        if ($request->isXmlHttpRequest()) {
            return;
        }

        $refererUrl = $request->headers->get('referer', null);
        if (\is_string($refererUrl)) {
            $cookieId = $request->cookies->get(Referer::COOKIE_KEY, null);

            $refererDomain = $this->getInnerSubstring($refererUrl, '/', 2);
            $currentDomain = $request->getHttpHost();
            $pageRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && ('max-age=0' === $_SERVER['HTTP_CACHE_CONTROL'] || 'no-cache' == $_SERVER['HTTP_CACHE_CONTROL']);
{'fraction_non_alphanumeric': 0.1404471544715447, 'fraction_numerical': 0.005284552845528455, 'mean_word_length': 2.747905559786748, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3623127', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1438, 'n_words': 247}