<?php
namespace FMT\Domain\Listener;
use FMT\Data\Entity\Transaction;
use FMT\Data\Entity\User;
use FMT\Data\Entity\UserTransaction;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use FMT\Domain\Event\UserEvent;
use FMT\Domain\Event\TransactionEvent;
use FMT\Domain\Repository\UserStatisticRepositoryInterface;
/**
* Class UserStatisticSubscriber
* @package FMT\Application\Subscriber
*/
class UserStatisticSubscriber implements EventSubscriberInterface
{
/**
* @var UserStatisticRepositoryInterface
*/
private $userStatisticRepository;
/**
* UserStatisticSubscriber constructor.
* @param UserStatisticRepositoryInterface $userStatisticRepository
*/
public function __construct(UserStatisticRepositoryInterface $userStatisticRepository)
{
$this->userStatisticRepository = $userStatisticRepository;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
TransactionEvent::TRANSACTION_COMPLETED => 'updateTransactionStatistic',
// todo add event subscriber to "book purchasing"
];
}
/**
* @param TransactionEvent $event
*/
public function updateTransactionStatistic(TransactionEvent $event)
{
$sender = $event->getTransaction()->getSender();
$recipient = $event->getTransaction()->getRecipient();
if ($sender instanceof User) {
$this->userStatisticRepository->updateStudentsFounded($sender);
$this->userStatisticRepository->updateAmountFounded($sender);
}
$type = $event->getTransaction()->getSubType();
if ($type == Transaction::TXN_DONATION || $type == Transaction::TXN_DIRECT_PURCHASE) {
$this->userStatisticRepository->updateAmountDonatedTo($recipient);
}
}
/**
* @param UserEvent $event
*/
public function updateBooksPurchased(UserEvent $event)
{
$this->userStatisticRepository->updateBooksPurchasedFor($event->getUser());
}
}