src/Invoice/Form/Dto/Factory/InvoiceEmailDraftCreateFormDtoFactory.php line 49

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Copyright (c) 2020 TECLA Consulting Group oü.
  5.  * All rights reserved.
  6.  *
  7.  * This unpublished material is proprietary to TECLA Consulting Group oü.
  8.  * All rights reserved. The methods and
  9.  * techniques described herein are considered trade secrets
  10.  * and/or confidential. Reproduction or distribution, in whole
  11.  * or in part, is forbidden except by express written permission
  12.  * of TECLA Consulting Group oü.
  13.  *
  14.  * @author    Matúš Sýkorjak <matus@tecla.no>
  15.  * @copyright 2020 TECLA Consulting Group oü
  16.  */
  17. namespace App\Invoice\Form\Dto\Factory;
  18. use App\Contact\Security\ContactContextInterface;
  19. use App\Invoice\Form\Dto\InvoiceEmailDraftCreateFormDto;
  20. use App\Invoice\Model\InvoiceInterface;
  21. use App\Invoice\Query\InvoiceEmail\FindLatestByInvoiceQueryInterface;
  22. use App\Invoice\Util\DefaultInvoiceEmailMessageGeneratorInterface;
  23. final class InvoiceEmailDraftCreateFormDtoFactory implements InvoiceEmailDraftCreateFormDtoFactoryInterface
  24. {
  25.     private ContactContextInterface $contactContext;
  26.     private DefaultInvoiceEmailMessageGeneratorInterface $defaultMessageGenerator;
  27.     private EmailRecipientFormDtoFactoryInterface $recipientDtoFactory;
  28.     private FindLatestByInvoiceQueryInterface $latestByInvoiceQuery;
  29.     public function __construct(
  30.         ContactContextInterface $contactContext,
  31.         DefaultInvoiceEmailMessageGeneratorInterface $defaultMessageGenerator,
  32.         EmailRecipientFormDtoFactoryInterface $recipientDtoFactory,
  33.         FindLatestByInvoiceQueryInterface $latestByInvoiceQuery
  34.     ) {
  35.         $this->contactContext $contactContext;
  36.         $this->defaultMessageGenerator $defaultMessageGenerator;
  37.         $this->recipientDtoFactory $recipientDtoFactory;
  38.         $this->latestByInvoiceQuery $latestByInvoiceQuery;
  39.     }
  40.     public function create(array $recipients = [], array $recipientsCc = [], array $recipientsBcc = [], string $message null): InvoiceEmailDraftCreateFormDto
  41.     {
  42.         return new InvoiceEmailDraftCreateFormDto(
  43.             $recipients,
  44.             $recipientsCc,
  45.             $recipientsBcc,
  46.             $message
  47.         );
  48.     }
  49.     public function createDefault(InvoiceInterface $invoice): InvoiceEmailDraftCreateFormDto
  50.     {
  51.         if (null !== ($latestSentInvoice $this->latestByInvoiceQuery->execute($invoice))) {
  52.             $recipients $this->recipientDtoFactory->createFromCollection($latestSentInvoice->getRecipients());
  53.             $recipientsCc $this->recipientDtoFactory->createFromCollection($latestSentInvoice->getRecipientsCc());
  54.         } elseif (false === empty($invoice->getRecipients())) {
  55.             $recipients $this->recipientDtoFactory->createFromCollection($invoice->getRecipients());
  56.             $recipientsCc $this->recipientDtoFactory->createFromCollection($invoice->getRecipientsCc());
  57.         } else {
  58.             $recipients = [$this->recipientDtoFactory->create($invoice->getCustomer()->getContactEmail()?->getEmail())];
  59.             $recipientsCc = [$this->recipientDtoFactory->create(
  60.                 $this->contactContext->getBase()?->getContactEmail()?->getEmail() ??
  61.                 $invoice->getSupplier()->getContactEmail()?->getEmail()
  62.             )];
  63.         }
  64.         return $this->create(
  65.             $recipients,
  66.             $recipientsCc,
  67.             [],
  68.             $this->defaultMessageGenerator->generate($invoice)
  69.         );
  70.     }
  71. }