<?php declare(strict_types=1);
namespace App\Security\Voter\Message;
use App\Entity\Message\MessageReception;
use App\Entity\User;
use App\Security\Voter\AbstractUserAwareVoter;
use App\Security\Voter\Bid\HasPurchasedOnceVoter;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class MessageAccess extends AbstractUserAwareVoter
{
public const ATTRIBUTE = 'MESSAGE_ACCESS';
private AuthorizationCheckerInterface $authorizationChecker;
public function __construct(AuthorizationCheckerInterface $authorizationChecker)
{
$this->authorizationChecker = $authorizationChecker;
}
/**
* @return array<int, string>
*/
protected function getSupportedAttributes(): array
{
return [self::ATTRIBUTE];
}
protected function supportsObject(?object $object = null): bool
{
return $object instanceof MessageReception;
}
/**
* @param MessageReception|object $object
*/
protected function isGrantedForUser(object $object, User $user): bool
{
if (!$object instanceof MessageReception) {
throw new UnexpectedTypeException($object, MessageReception::class);
}
return $object->getMessage()->isNotification()
|| $this->authorizationChecker->isGranted(
HasPurchasedOnceVoter::ATTRIBUTE,
$object->getBid() ?? $object->createBid()
);
}
}