<?php

namespace App\EventListener;

use App\Entity\Domain\Stat\Check;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Symfony\Component\DependencyInjection\ContainerInterface;

class StatUpdaterListener
{
    /**
     * @var ContainerInterface
     */
    private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function postPersist(LifecycleEventArgs $args)
    {
        $entity = $args->getObject();

        if (!$entity instanceof Check) {
            return;
        }

        $service = $this->container->get('App\Service\Domain\StatService');

        $stat = $entity->getStat()->setValue($entity->getValue())->setCheckedAt(new \DateTime());
        $service->save($stat);
    }
}
