<?php
namespace App\Subscriber\UserGroups;
use App\Entity\UserGroupScore;
use App\Event\UserGroups\GroupScoreUpdateEvent;
use App\Service\UserGroups\GroupScoreService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UserGroupScoreSubscriber implements EventSubscriberInterface
{
/**
* @var GroupScoreService
*/
protected $service;
/**
* UserGroupScoreSubscriber constructor.
*/
public function __construct(GroupScoreService $service)
{
$this->service = $service;
}
public static function getSubscribedEvents(): array
{
return [
GroupScoreUpdateEvent::class => [
['onScoreUpdate', 0],
],
];
}
/**
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function onScoreUpdate(GroupScoreUpdateEvent $event)
{
$groupScore = new UserGroupScore();
$groupScore->setUserGroup($event->getGroup());
$groupScore->setScore($event->getScore());
$this->service->storeEntity($groupScore);
}
}