src/Action/MailchimpAction.php line 61

Open in your IDE?
  1. <?php
  2. declare(strict_types 1);
  3. namespace App\Action;
  4. use App\Infra\Events\SessionMessageEvent;
  5. use App\Responder\RedirectResponder;
  6. use App\Responder\TemplateResponder;
  7. use DrewM\MailChimp\MailChimp;
  8. use Psr\EventDispatcher\EventDispatcherInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use App\Infra\Services\MailerService;
  12. class MailchimpAction extends AbstractAction
  13. {
  14.     public function __construct(
  15.         TemplateResponder $templateResponder,
  16.         RedirectResponder $redirectResponder,
  17.         EventDispatcherInterface $eventDispatcher,
  18.         MailerService $mailerService
  19.     )
  20.     {
  21.         parent::__construct(
  22.             $this->templateResponder $templateResponder,
  23.             $this->redirectResponder $redirectResponder,
  24.             $this->eventDispatcher $eventDispatcher
  25.         );
  26.         $this->mailerService $mailerService;
  27.     }
  28.     /**
  29.      * @Route("/mailchimp", name="mailchimp", methods={"GET"})
  30.      */
  31.     public function __invoke(Request $request)
  32.     {
  33.         $email $request->query->get('email');
  34.         $MailChimp = new MailChimp($_ENV['MAILCHIMP_API_KEY']);
  35.         $list_id $_ENV['MAILCHIMP_LIST_ID'];
  36.         $result $MailChimp->post("lists/$list_id/members", [
  37.             'email_address' => $email,
  38.             'status'        => 'subscribed',
  39.         ]);
  40.         if ($result['status'] == 400) {
  41.             $this->eventDispatcher->dispatch(
  42.                 new SessionMessageEvent('error''vous êtes déjà sur la liste'),
  43.                 SessionMessageEvent::SESSION_MESSAGE
  44.             );
  45.             return $this->redirectResponder->__invoke('index');
  46.         } else
  47.         $this->eventDispatcher->dispatch(
  48.             new SessionMessageEvent('success''merci'),
  49.             SessionMessageEvent::SESSION_MESSAGE
  50.         );
  51.         // Send email to Tracol
  52.         $this->mailerService->newAboNewsletter($email);
  53.         return $this->redirectResponder->__invoke('index');
  54.     }
  55. }