<?php
namespace App\Voter\__Accounting;
use App\Entity\Accounting\Payment;
use App\Entity\Booking\Booking;
use App\Entity\ParameterCodes;
use App\Voter\VoterTrait;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;
class AccountingPaymentVoter extends Voter
{
use VoterTrait;
const LIST = "ACCOUNTING_PAYMENT_LIST";
const CREATE = "ACCOUNTING_PAYMENT_CREATE";
const UPDATE = "ACCOUNTING_PAYMENT_UPDATE";
const DELETE = "ACCOUNTING_PAYMENT_DELETE";
const SEND_ACK = "ACCOUNTING_PAYMENT_SEND_ACK";
protected function supports($attribute, $subject): bool
{
return ($attribute == self::LIST && $subject === null) ||
($attribute == self::CREATE && $subject instanceof Booking) ||
(in_array($attribute, [self::UPDATE, self::DELETE, self::SEND_ACK]) && $subject instanceof Payment);
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$loggedUser = $token->getUser();
if (!$loggedUser instanceof UserInterface) {
return false;
}
switch ($attribute) {
case self::LIST:
return $this->voteOnList($loggedUser);
case self::CREATE:
return $this->voteOnCreate($subject, $loggedUser);
case self::UPDATE:
return $this->voteOnUpdate($subject, $loggedUser);
case self::DELETE:
return $this->voteOnDelete($subject, $loggedUser);
case self::SEND_ACK:
return $this->voteOnSendAck($subject, $loggedUser);
}
return false;
}
private function voteOnList(UserInterface $loggedUser): bool
{
return in_array(self::LIST, $loggedUser->getRoles());
}
private function voteOnCreate(?Booking $booking, UserInterface $loggedUser): bool
{
if (null === $booking) {
return false;
}
return in_array(self::CREATE, $loggedUser->getRoles());
}
private function voteOnUpdate(?Payment $payment, UserInterface $loggedUser): bool
{
if (null === $payment) {
return false;
}
if ($payment->getMode()->getCode() === ParameterCodes::PAYMENT_MODE_OGONE ||
$payment->getMode()->getCode() === ParameterCodes::PAYMENT_MODE_WORLDLINE ||
$payment->getMode()->getCode() === ParameterCodes::PAYMENT_MODE_FLOA) {
return false;
}
return in_array(self::UPDATE, $loggedUser->getRoles());
}
private function voteOnDelete(?Payment $payment, UserInterface $loggedUser): bool
{
if (null === $payment) {
return false;
}
if ($payment->getMode()->getCode() === ParameterCodes::PAYMENT_MODE_OGONE ||
$payment->getMode()->getCode() === ParameterCodes::PAYMENT_MODE_WORLDLINE ||
$payment->getMode()->getCode() === ParameterCodes::PAYMENT_MODE_FLOA) {
return false;
}
return in_array(self::DELETE, $loggedUser->getRoles());
}
private function voteOnSendAck(?Payment $payment, UserInterface $loggedUser): bool
{
if (null === $payment) {
return false;
}
if ($payment->getMode()->getCode() === ParameterCodes::PAYMENT_MODE_OGONE ||
$payment->getMode()->getCode() === ParameterCodes::PAYMENT_MODE_WORLDLINE ||
$payment->getMode()->getCode() === ParameterCodes::PAYMENT_MODE_FLOA) {
return false;
}
return in_array(self::SEND_ACK, $loggedUser->getRoles());
}
}