<?php
declare(strict_types=1);
/**
* @author Mehrez Labidi
*/
namespace App\Security\Voter;
use App\Services\Annonces\SecurityAnnonce;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\HttpFoundation\RequestStack;
class AnnonceVoter extends Voter
{
public const VIEW_OWN_ANNONCE = 'VIEW_OWN_ANNONCE';
private $securityAnnonce;
private $requestStack;
public function __construct(SecurityAnnonce $securityAnnonce, RequestStack $requestStack)
{
$this->securityAnnonce = $securityAnnonce;
$this->requestStack = $requestStack;
}
protected function supports(string $attribute, $subject): bool
{
return $attribute === self::VIEW_OWN_ANNONCE;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$request = $this->requestStack->getCurrentRequest();
if (!$request) {
return false;
}
$idAnnonce = $request->attributes->get('id_annonce');
$typeRapprochement = $request->attributes->get('type_raprochement');
try {
$this->securityAnnonce->annonceCurrentUser($idAnnonce, $typeRapprochement);
return true;
} catch (\Exception $e) {
return false;
}
}
}