<?php
namespace App\EventSubscriber;
use App\Entity\Configuration;
use App\Entity\Product;
use App\Event\ConfigurationCompleteEvent;
use App\Repository\ProductRepository;
use App\Service\GrcService;
use App\Service\ProductFileService;
use App\Service\QuotationService;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Part\DataPart;
class ConfigurationEventSubsctiber implements EventSubscriberInterface
{
public function __construct(
private MailerInterface $mailer,
private GrcService $grcService,
private ContainerBagInterface $params,
private ProductFileService $fileService,
private ProductRepository $productRepository,
) {
}
public static function getSubscribedEvents()
{
return [
ConfigurationCompleteEvent::class => 'onComplete',
];
}
public function onComplete(ConfigurationCompleteEvent $event)
{
$configuration = $event->getConfiguration();
$name = 'AFIMES ' .
$configuration->getChronoItem()->getValue() . ' ' .
$configuration->getProspect()->getCompanyName();
$directory = __DIR__ . '/../../public/quotations/';
$products = $this->getProducts($configuration);
$files = [];
foreach($products as $product) {
$file = $this->fileService->getProductFileByReference($product->getReferenceAFIMES());
if( $file )
$files[$product->getReferenceAFIMES()] = $file;
}
$this->sendToClient($configuration, $directory, $name, $files);
$this->sendToCommercial($configuration, $directory, $name, $files);
$this->grcService->send($configuration);
}
private function sendToClient(Configuration $configuration, string $directory, string $name, array $files): void
{
$email = (new TemplatedEmail())
->from(new Address($this->params->get('app.emails.from'), 'AFIMES'))
->to($configuration->getProspect()->getEmail())
->subject("Devis AFIMES")
->htmlTemplate("emails/configuration/complete.html.twig")
->context([
'configuration' => $configuration
])
->attachFromPath($directory.$name . '.pdf', $name . '.pdf', 'application/pdf');
$file_directory = __DIR__ . '/../../public/products/';
foreach($files as $name => $file) {
$email->attachFromPath($file_directory.$file, $name . '.pdf', 'application/pdf');
}
$this->mailer->send($email);
}
private function sendToCommercial(Configuration $configuration, string $directory, string $name, array $files): void
{
$email = (new TemplatedEmail())
->from(new Address($this->params->get('app.emails.from'), 'AFIMES'))
->to(new Address($this->params->get('app.emails.commercial')))
->subject("Devis AFIMES")
->htmlTemplate("emails/configuration/complete.html.twig")
->context([
'configuration' => $configuration
])
->attachFromPath($directory.$name . '.pdf', $name . '.pdf', 'application/pdf')
->attachFromPath($directory.$name . '.xlsx', $name . '.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$file_directory = __DIR__ . '/../../public/products/';
foreach($files as $name => $file) {
$email->attachFromPath($file_directory.$file, $name . '.pdf', 'application/pdf');
}
$this->mailer->send($email);
}
/**
* @param Configuration $configuration
* @return Product[]
*/
private function getProducts(Configuration $configuration): array
{
$data = $configuration->getData();
$identifiers = $data['products']['identifiers'] != '' ? explode(',', $data['products']['identifiers']) : [];
$products = [];
foreach($identifiers as $identifier) {
if($identifier && $product = $this->productRepository->find($identifier)) {
$products[] = $product;
}
}
return $products ;
}
}