<?php
/*
* @since 1.0.0
* @copyright Copyright (C) 2020 ArtMedia. All rights reserved.
* @website http://artmedia.biz.pl
* @author Arkadiusz Tobiasz
* @email kontakt@artmedia.biz.pl
*/
namespace App\EventSubscriber\Money;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use App\Events\Core\DashboardWidgetAddEvent;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use App\Entity\User;
use App\Settings\SettingsManager;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Money\Payout;
use Twig\Environment;
class WidgetSubscriber implements EventSubscriberInterface
{
private $em;
private $securityChecker;
private $twig;
public function __construct(
Environment $twig,
EntityManagerInterface $em,
AuthorizationCheckerInterface $securityChecker = null
) {
$this->securityChecker = $securityChecker;
$this->em = $em;
$this->twig = $twig;
}
public static function getSubscribedEvents(): array
{
return [
DashboardWidgetAddEvent::NAME => 'onDashboardWidgetAddEvent',
];
}
public function onDashboardWidgetAddEvent(DashboardWidgetAddEvent $event): void
{
$user = $event->getUser();
if (!$user) {
return;
}
if(!in_array(User::ROLE_ADMIN, $user->getRoles()))
{
if (null === $this->securityChecker) {
return;
}
if (!$this->securityChecker->isGranted('ROLE_MODERATOR_PERMISSION', SettingsManager::ADMIN_PAYOUTS)) {
return;
}
}
$payouts = $this->em->getRepository(Payout::class)->findBy([
'status' => Payout::STATUS_WAITING
], [
'createdAt' => 'ASC'
]);
$event->addWidget(
'payouts',
$this->twig->render('money/widget/waiting_payouts.html.twig', [
'payouts' => $payouts,
]),
'top',
1
);
}
}