<?php
namespace App\Voter;
use App\Entity\InvoiceBatch;
use App\Model\InvoiceBatchFactory;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class InvoiceBatchVoter extends Voter
{
/**
* @var InvoiceBatchFactory
*/
private $invoiceBatchFactory;
/**
* InvoiceBatchVoter constructor.
*/
public function __construct(InvoiceBatchFactory $invoiceBatchFactory)
{
$this->invoiceBatchFactory = $invoiceBatchFactory;
}
public const EDIT = 'edit';
/**
* @param string $attribute
* @param mixed $subject
*/
protected function supports($attribute, $subject): bool
{
// if the attribute isn't one we support, return false
if (! in_array($attribute, [
self::EDIT
])) {
return false;
}
if (! $subject instanceof InvoiceBatch) {
return false;
}
return true;
}
/**
* @param string $attribute
* @param mixed $subject
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user) {
return false; // User not logged in
}
$invoiceBatch = $this->invoiceBatchFactory->createModel($subject);
if ($invoiceBatch->isProcessed()) {
return false;
}
return true;
}
}