src/Invoice/Security/InvoiceVoter.php line 30
<?phpdeclare(strict_types=1);/*** Copyright (c) 2019 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 2019 TECLA Consulting Group oü*/namespace App\Invoice\Security;use App\Contact\Model\ContactInterface;use App\Contact\Security\ContactContextInterface;use App\Invoice\Enum\InvoiceStatus;use App\Invoice\Enum\InvoiceType;use App\Invoice\Model\InvoiceInterface;use LogicException;use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;use Symfony\Component\Security\Core\Authorization\Voter\Voter;final class InvoiceVoter extends Voter{public const CREATE_INVOICE = 'invoice.create_invoice';public const CREATE_CREDIT_NOTE = 'invoice_create_credit_note';public const CREATE_PAYMENT = 'invoice_create_payment';public const CREATE_REPEAT_RULE = 'invoice.create_repeat_rule';public const VIEW = 'invoice_view';public const EXPORT = 'invoice_export';public const COPY = 'invoice_copy';public const SEND = 'invoice_send';public const SUPPORTED_ATTRIBUTES = [self::CREATE_INVOICE,self::CREATE_PAYMENT,self::CREATE_CREDIT_NOTE,self::VIEW,self::COPY,self::EXPORT,self::SEND,self::CREATE_REPEAT_RULE,];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 InvoiceInterface;}return false;}protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool{switch ($attribute) {case self::CREATE_INVOICE:return $this->canCreateInvoice();case self::CREATE_PAYMENT:return $this->canCreatePayment($subject);case self::CREATE_CREDIT_NOTE:return $this->canCreateCreditNote($subject);case self::VIEW:case self::EXPORT:return $this->canAccessInvoice($subject);case self::COPY:return $this->canCopyInvoice($subject);case self::SEND:return $this->canSendInvoice($subject);case self::CREATE_REPEAT_RULE:return $this->canRepeatInvoice($subject);}throw new LogicException('This code should not be reached!');}private function canCreateInvoice(): bool{$currentContext = $this->contactContext->getCurrent();if (null === $currentContext) {return false;}if (ContactInterface::TYPE_COMPANY !== $currentContext->getType()) {return false;}return true;}private function canViewInvoice(?InvoiceInterface $invoice): bool{if (null === $invoice) {return false;}$currentContext = $this->contactContext->getCurrent();return null !== $currentContext && $invoice->getSupplier() === $currentContext;}private function canAccessInvoice(?InvoiceInterface $invoice): bool{if (null === $invoice) {return false;}$currentContext = $this->contactContext->getCurrent();return $this->canViewInvoice($invoice) || (null !== $currentContext && $invoice->getCustomer() === $currentContext);}private function canCreateCreditNote(?InvoiceInterface $invoice): bool{if (null === $invoice) {return $this->canCreateInvoice();}if (false === $this->canViewInvoice($invoice)) {return false;}if (false === InvoiceType::INVOICE()->equals($invoice->getType())) {return false;}if (true === InvoiceStatus::CREDITED()->equals($invoice->getStatus())) {return false;}return true;}private function canCopyInvoice(?InvoiceInterface $invoice): bool{if (false === $this->canViewInvoice($invoice) || null === $invoice) {return false;}return InvoiceType::INVOICE()->equals($invoice->getType());}private function canSendInvoice(?InvoiceInterface $invoice): bool{if (null === $invoice) {return false;}$currentContext = $this->contactContext->getCurrent();return null !== $currentContext && $invoice->getSupplier() === $currentContext;}private function canCreatePayment(?InvoiceInterface $invoice): bool{if (null === $invoice) {return false;}$currentContext = $this->contactContext->getCurrent();returnnull !== $currentContext &&$invoice->getSupplier() === $currentContext &&true === InvoiceType::INVOICE()->equals($invoice->getType()) &&false === InvoiceStatus::PAID()->equals($invoice->getStatus()) &&false === InvoiceStatus::CREDITED()->equals($invoice->getStatus());}private function canRepeatInvoice(?InvoiceInterface $invoice): bool{if (null === $invoice) {return false;}if (false === InvoiceType::INVOICE()->equals($invoice->getType())) {return false;}if (false === $this->isOwner($invoice)) {return false;}return true;}private function isOwner(?InvoiceInterface $invoice): bool{if (null === $invoice) {return false;}$currentContext = $this->contactContext->getCurrent();return null !== $currentContext && $invoice->getSupplier() === $currentContext;}}