<commit_before><?php

namespace Bangnation\UserBundle\Listener;

use Symfony\Component\Security\Core\SecurityContext;
use Doctrine\Bundle\DoctrineBundle\Registry as Doctrine;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use DateTime;
use Bangnation\UserBundle\Entity\User;

class Activity
{
    protected $context;
    
    protected $em;
    
    public function __construct(SecurityContext $context, Doctrine $doctrine)
    {
        $this->context = $context;
        $this->em = $doctrine->getEntityManager();
    }
    
    /**
     * On each request we want to update the user's last activity datetime
     *
     * @param \Symfony\Component\HttpKernel\Event\FilterControllerEvent $event
     * @return void
     */
    public function onCoreController(FilterControllerEvent $event)
    {
        if ($this->context->getToken()) {
            $user = $this->context->getToken()->getUser();
            if($user instanceof User)
            {
                //here we can update the user as necessary
                $user->setLastActivity(new DateTime());
                $this->em->persist($user);
                $this->em->flush($user);
            }
        }
    }
}<commit_msg>Reduced the amount of sql inserts for user last activity<commit_after><?php

namespace Bangnation\UserBundle\Listener;

use Symfony\Component\Security\Core\SecurityContext;
use Doctrine\Bundle\DoctrineBundle\Registry as Doctrine;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use DateTime;
use Bangnation\UserBundle\Entity\User;

class Activity
{
    protected $context;
    
    protected $em;
    
    public function __construct(SecurityContext $context, Doctrine $doctrine)
    {
        $this->context = $context;
        $this->em = $doctrine->getEntityManager();
    }
    
    /**
     * On each request we want to update the user's last activity datetime
     *
     * @param \Symfony\Component\HttpKernel\Event\FilterControllerEvent $event
     * @return void
     */
    public function onCoreController(FilterControllerEvent $event)
    {
        // If you use partial controller like {% render %} you call 
        // onCoreController each time and have many sql inserts. This avoids 
        // that issue.
        if ($event->getRequestType() !== \Symfony\Component\HttpKernel\HttpKernel::MASTER_REQUEST) {
            return;
        };

        if ($this->context->getToken()) {
            $user = $this->context->getToken()->getUser();
            if($user instanceof User)
            {
                //here we can update the user as necessary
                $user->setLastActivity(new DateTime());
                $this->em->persist($user);
                $this->em->flush($user);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.1108695652173913, 'fraction_numerical': 0.0, 'mean_word_length': 2.404438964241677, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 30, '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': '5317729', 'n_tokens_mistral': 747, 'n_tokens_neox': 729, 'n_words': 176}