<?php
namespace App\Voters;
use App\Entity\Patient\Note\AdditionalNote;
use App\Entity\Person\User;
use App\Exception\Api\ApiException;
use App\Services\DeviceManager;
use App\Services\Licence\CheckLicenceService;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
class AdditionalNoteVoter extends OrganisationVoter
{
private $checkLicenceService;
private $deviceManager;
public function __construct(
AccessDecisionManagerInterface $decisionManager,
CheckLicenceService $checkLicenceService,
DeviceManager $deviceManager
) {
parent::__construct($decisionManager);
$this->checkLicenceService = $checkLicenceService;
$this->deviceManager = $deviceManager;
}
protected function supports($attribute, $subject)
{
return $subject instanceof AdditionalNote && self::EDIT === $attribute;
}
/**
* @param AdditionalNote $subject
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
/** @var User $user */
$user = $token->getUser();
if (!$this->decisionManager->decide($token, [User::ROLE_PRACTITIONER])) {
return false;
}
if ($subject->getCreatedAt()->format('Ymd') !== (new \DateTime())->format('Ymd')) {
return false;
}
if ($user->getClinic() !== $subject->getConsultationNote()->getClinic()) {
return false;
}
try {
$this->checkLicenceService->checkUser($this->deviceManager, $user);
} catch (ApiException $apiException) {
return false;
}
return true;
}
}