<?php
namespace App\EventListener;
use App\Component\Configuration\Util\Config;
use App\Entity\Company;
use App\Entity\Mail;
use App\Event\InternFormSubmitEvent;
use App\Sendgrid\MailerInterface;
use App\Util\StringToEntityUtil;
use Gedmo\Translatable\TranslatableListener;
use League\Flysystem\Adapter\Local;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class InternFormSubmitEventListener extends FormSubmitEventListener
{
protected Local $fileAdapter;
/**
* InternFormSubmitEventListener constructor.
*/
public function __construct(
MailerInterface $mailer,
ParameterBagInterface $parameterBag,
StringToEntityUtil $stringToEntityUtil,
?TranslatableListener $translatableListener,
Config $config,
Local $fileAdapter
) {
parent::__construct($mailer, $parameterBag, $stringToEntityUtil, $translatableListener, $config);
$this->fileAdapter = $fileAdapter;
}
public function onSubmit(InternFormSubmitEvent $event)
{
$entry = $event->getEntry();
$files = [];
if ($entry->getResumeFileName() && $this->fileAdapter->has($entry->getResumeFileName())) {
$files[] = $this->fileAdapter->getPathPrefix().$entry->getResumeFileName();
}
if ($entry->getMotivationFileName() && $this->fileAdapter->has($entry->getMotivationFileName())) {
$files[] = $this->fileAdapter->getPathPrefix().$entry->getMotivationFileName();
}
$departments = implode(', ', array_map(function (Company $company) {
return $company->getName();
}, $entry->getDepartment()->toArray()));
if ($mail = $this->getMail('site_intern_form_mail_visitor')) {
$this->mailer
->mail(
$mail,
$this->getCompanyEmailSender(),
[$entry->getEmail()],
[
'intern' => $entry,
'departments' => $departments,
]
);
}
if ($mail = $this->getMail('site_intern_form_mail_site_owner')) {
$this->mailer
->mail(
$mail,
$this->getCompanyEmailSender(),
[$this->getCompanyEmailaddress()],
[
'intern' => $entry,
'departments' => $departments,
],
[],
[],
$files
);
}
}
/**
* {@inheritDoc}
*/
protected function getMail(string $mailId): ?Mail
{
return $this->config->get($mailId);
}
/**
* {@inheritDoc}
*/
public function getCompanyEmailSender(): array
{
if ($sender = $this->config->get('site_intern_form_sender')) {
if ($senderName = $this->config->get('site_intern_form_sender_name')) {
return [$sender => $senderName];
}
return [$sender => $this->config->get('site_company_name')];
}
return parent::getCompanyEmailSender();
}
/**
* {@inheritDoc}
*/
public function getCompanyEmailaddress(): string
{
if ($receiver = $this->config->get('site_intern_form_receiver')) {
return $receiver;
}
return parent::getCompanyEmailaddress();
}
}