src/Invoice/Security/InvoiceDraftVoter.php line 26
<?phpdeclare(strict_types=1);/*** Copyright (c) 2020 TECLA Consulting Group oü.* All rights reserved.** This unpublished material is proprietary to TECLA Consulting Group oü.* All rights reserved. The methods and* techniques described herein are considered trade secrets* and/or confidential. Reproduction or distribution, in whole* or in part, is forbidden except by express written permission* of TECLA Consulting Group oü.** @author Matúš Sýkorjak <matus@tecla.no>* @copyright 2020 TECLA Consulting Group oü*/namespace App\Invoice\Security;use App\Contact\Security\ContactContextInterface;use App\Invoice\Model\InvoiceDraftInterface;use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;use Symfony\Component\Security\Core\Authorization\Voter\Voter;final class InvoiceDraftVoter extends Voter{public const UPDATE = 'invoice_draft.update';public const COPY = 'invoice_draft.copy';public const EXPORT = 'invoice_draft.export';public const SEND = 'invoice_draft.send';public const CREATE_INVOICE = 'invoice_draft.create_invoice';public const SUPPORTED_ATTRIBUTES = [self::UPDATE,self::COPY,self::EXPORT,self::SEND,self::CREATE_INVOICE,];private ContactContextInterface $contactContext;public function __construct(ContactContextInterface $contactContext){$this->contactContext = $contactContext;}protected function supports($attribute, $subject): bool{if (true === \in_array($attribute, self::SUPPORTED_ATTRIBUTES, true)) {return null === $subject || true === $subject instanceof InvoiceDraftInterface;}return false;}protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool{switch ($attribute) {case self::UPDATE:return $this->canUpdate($subject);case self::COPY:return $this->canCopy($subject);case self::EXPORT:return $this->canExport($subject);case self::SEND:return $this->canSend($subject);case self::CREATE_INVOICE:return $this->canView($subject);}throw new \LogicException('This code should not be reached!');}private function canUpdate(?InvoiceDraftInterface $invoiceDraft): bool{return $this->canView($invoiceDraft);}private function canCopy(?InvoiceDraftInterface $draft): bool{return $this->canView($draft);}private function canExport(?InvoiceDraftInterface $invoiceDraft): bool{return $this->canView($invoiceDraft);}private function canSend(?InvoiceDraftInterface $draft): bool{return $this->canView($draft);}private function canView(?InvoiceDraftInterface $invoiceDraft): bool{if (null === $invoiceDraft) {return false;}$currentContext = $this->contactContext->getCurrent();return null !== $currentContext && $invoiceDraft->getSupplier() === $currentContext;}}