src/FMT/Domain/Listener/UserStatisticSubscriber.php line 47

Open in your IDE?
  1. <?php
  2. namespace FMT\Domain\Listener;
  3. use FMT\Data\Entity\Transaction;
  4. use FMT\Data\Entity\User;
  5. use FMT\Data\Entity\UserTransaction;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use FMT\Domain\Event\UserEvent;
  8. use FMT\Domain\Event\TransactionEvent;
  9. use FMT\Domain\Repository\UserStatisticRepositoryInterface;
  10. /**
  11.  * Class UserStatisticSubscriber
  12.  * @package FMT\Application\Subscriber
  13.  */
  14. class UserStatisticSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var UserStatisticRepositoryInterface
  18.      */
  19.     private $userStatisticRepository;
  20.     /**
  21.      * UserStatisticSubscriber constructor.
  22.      * @param UserStatisticRepositoryInterface $userStatisticRepository
  23.      */
  24.     public function __construct(UserStatisticRepositoryInterface $userStatisticRepository)
  25.     {
  26.         $this->userStatisticRepository $userStatisticRepository;
  27.     }
  28.     /**
  29.      * @return array
  30.      */
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             TransactionEvent::TRANSACTION_COMPLETED => 'updateTransactionStatistic',
  35.             // todo add event subscriber to "book purchasing"
  36.         ];
  37.     }
  38.     /**
  39.      * @param TransactionEvent $event
  40.      */
  41.     public function updateTransactionStatistic(TransactionEvent $event)
  42.     {
  43.         $sender $event->getTransaction()->getSender();
  44.         $recipient $event->getTransaction()->getRecipient();
  45.         if ($sender instanceof User) {
  46.             $this->userStatisticRepository->updateStudentsFounded($sender);
  47.             $this->userStatisticRepository->updateAmountFounded($sender);
  48.         }
  49.         $type $event->getTransaction()->getSubType();
  50.         if ($type == Transaction::TXN_DONATION || $type == Transaction::TXN_DIRECT_PURCHASE) {
  51.             $this->userStatisticRepository->updateAmountDonatedTo($recipient);
  52.         } 
  53.     }
  54.     /**
  55.      * @param UserEvent $event
  56.      */
  57.     public function updateBooksPurchased(UserEvent $event)
  58.     {
  59.         $this->userStatisticRepository->updateBooksPurchasedFor($event->getUser());
  60.     }
  61. }