src/Voters/TreatmentNoteVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Voters;
  3. use App\Entity\Patient\Note\TreatmentNote;
  4. use App\Entity\Person\User;
  5. use App\Exception\Api\ApiException;
  6. use App\Services\DeviceManager;
  7. use App\Services\Licence\CheckLicenceService;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  10. class TreatmentNoteVoter extends OrganisationVoter
  11. {
  12.     private $checkLicenceService;
  13.     private $deviceManager;
  14.     public function __construct(
  15.         AccessDecisionManagerInterface $decisionManager,
  16.         CheckLicenceService $checkLicenceService,
  17.         DeviceManager $deviceManager
  18.     ) {
  19.         parent::__construct($decisionManager);
  20.         $this->checkLicenceService $checkLicenceService;
  21.         $this->deviceManager $deviceManager;
  22.     }
  23.     protected function supports($attribute$subject)
  24.     {
  25.         return $subject instanceof TreatmentNote && self::EDIT === $attribute;
  26.     }
  27.     /**
  28.      * @param TreatmentNote $subject
  29.      */
  30.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  31.     {
  32.         /** @var User $user */
  33.         $user $token->getUser();
  34.         if (!$this->decisionManager->decide($token, [User::ROLE_PRACTITIONER])) {
  35.             return false;
  36.         }
  37.         if ($subject->getCreatedAt()->format('Ymd') !== (new \DateTime())->format('Ymd')) {
  38.             return false;
  39.         }
  40.         if ($user->getClinic() !== $subject->getConsultationNote()->getClinic()) {
  41.             return false;
  42.         }
  43.         try {
  44.             $this->checkLicenceService->checkUser($this->deviceManager$user);
  45.         } catch (ApiException $apiException) {
  46.             return false;
  47.         }
  48.         return true;
  49.     }
  50. }