<?php
declare(strict_types = 1);
namespace App\Action;
use App\Infra\Events\SessionMessageEvent;
use App\Responder\RedirectResponder;
use App\Responder\TemplateResponder;
use DrewM\MailChimp\MailChimp;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use App\Infra\Services\MailerService;
class MailchimpAction extends AbstractAction
{
public function __construct(
TemplateResponder $templateResponder,
RedirectResponder $redirectResponder,
EventDispatcherInterface $eventDispatcher,
MailerService $mailerService
)
{
parent::__construct(
$this->templateResponder = $templateResponder,
$this->redirectResponder = $redirectResponder,
$this->eventDispatcher = $eventDispatcher
);
$this->mailerService = $mailerService;
}
/**
* @Route("/mailchimp", name="mailchimp", methods={"GET"})
*/
public function __invoke(Request $request)
{
$email = $request->query->get('email');
$MailChimp = new MailChimp($_ENV['MAILCHIMP_API_KEY']);
$list_id = $_ENV['MAILCHIMP_LIST_ID'];
$result = $MailChimp->post("lists/$list_id/members", [
'email_address' => $email,
'status' => 'subscribed',
]);
if ($result['status'] == 400) {
$this->eventDispatcher->dispatch(
new SessionMessageEvent('error', 'vous êtes déjà sur la liste'),
SessionMessageEvent::SESSION_MESSAGE
);
return $this->redirectResponder->__invoke('index');
} else
$this->eventDispatcher->dispatch(
new SessionMessageEvent('success', 'merci'),
SessionMessageEvent::SESSION_MESSAGE
);
// Send email to Tracol
$this->mailerService->newAboNewsletter($email);
return $this->redirectResponder->__invoke('index');
}
}