src/EventSubscriber/ConfigurationEventSubsctiber.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Configuration;
  4. use App\Entity\Product;
  5. use App\Event\ConfigurationCompleteEvent;
  6. use App\Repository\ProductRepository;
  7. use App\Service\GrcService;
  8. use App\Service\ProductFileService;
  9. use App\Service\QuotationService;
  10. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  11. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\Mailer\MailerInterface;
  14. use Symfony\Component\Mime\Address;
  15. use Symfony\Component\Mime\Part\DataPart;
  16. class ConfigurationEventSubsctiber implements EventSubscriberInterface
  17. {
  18.     public function __construct(
  19.         private MailerInterface $mailer,
  20.         private GrcService $grcService,
  21.         private ContainerBagInterface $params,
  22.         private ProductFileService $fileService,
  23.         private ProductRepository $productRepository,
  24.     ) {
  25.     }
  26.     public static function getSubscribedEvents()
  27.     {
  28.         return [
  29.             ConfigurationCompleteEvent::class => 'onComplete',
  30.         ];
  31.     }
  32.     public function onComplete(ConfigurationCompleteEvent $event)
  33.     {
  34.         $configuration $event->getConfiguration();
  35.         $name 'AFIMES ' .
  36.             $configuration->getChronoItem()->getValue() . ' ' .
  37.             $configuration->getProspect()->getCompanyName();
  38.         $directory __DIR__ '/../../public/quotations/';
  39.         $products $this->getProducts($configuration);
  40.         $files = [];
  41.         foreach($products as $product) {
  42.             $file $this->fileService->getProductFileByReference($product->getReferenceAFIMES());
  43.             if( $file )
  44.                 $files[$product->getReferenceAFIMES()] = $file;
  45.         }
  46.         $this->sendToClient($configuration$directory$name$files);
  47.         $this->sendToCommercial($configuration$directory$name$files);
  48.         $this->grcService->send($configuration);
  49.     }
  50.     private function sendToClient(Configuration $configurationstring $directorystring $name, array $files): void
  51.     {
  52.         $email = (new TemplatedEmail())
  53.             ->from(new Address($this->params->get('app.emails.from'), 'AFIMES'))
  54.             ->to($configuration->getProspect()->getEmail())
  55.             ->subject("Devis AFIMES")
  56.             ->htmlTemplate("emails/configuration/complete.html.twig")
  57.             ->context([
  58.                 'configuration' => $configuration
  59.             ])
  60.             ->attachFromPath($directory.$name '.pdf'$name '.pdf''application/pdf');
  61.         $file_directory __DIR__ '/../../public/products/';
  62.         foreach($files as $name => $file) {
  63.             $email->attachFromPath($file_directory.$file$name '.pdf''application/pdf');
  64.         }
  65.         $this->mailer->send($email);
  66.     }
  67.     private function sendToCommercial(Configuration $configurationstring $directorystring $name, array $files): void
  68.     {
  69.         $email = (new TemplatedEmail())
  70.                     ->from(new Address($this->params->get('app.emails.from'), 'AFIMES'))
  71.                     ->to(new Address($this->params->get('app.emails.commercial')))
  72.                     ->subject("Devis AFIMES")
  73.                     ->htmlTemplate("emails/configuration/complete.html.twig")
  74.                     ->context([
  75.                         'configuration' => $configuration
  76.                     ])
  77.                     ->attachFromPath($directory.$name '.pdf'$name '.pdf''application/pdf')
  78.                     ->attachFromPath($directory.$name '.xlsx'$name '.xlsx''application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  79.         $file_directory __DIR__ '/../../public/products/';
  80.         foreach($files as $name => $file) {
  81.             $email->attachFromPath($file_directory.$file$name '.pdf''application/pdf');
  82.         }
  83.         $this->mailer->send($email);
  84.     }
  85.     /**
  86.      * @param Configuration $configuration
  87.      * @return Product[]
  88.      */
  89.     private function getProducts(Configuration $configuration): array
  90.     {
  91.         $data $configuration->getData();
  92.         $identifiers $data['products']['identifiers'] != '' explode(','$data['products']['identifiers']) : [];
  93.         $products = [];
  94.         foreach($identifiers as $identifier) {
  95.             if($identifier && $product $this->productRepository->find($identifier)) {
  96.                 $products[] = $product;
  97.             }
  98.         }
  99.         return $products ;
  100.     }
  101. }