src/Action/MailchimpAction.php line 24

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. class MailchimpAction extends AbstractAction
  12. {
  13.     public function __construct(
  14.         TemplateResponder $templateResponder,
  15.         RedirectResponder $redirectResponder,
  16.         EventDispatcherInterface $eventDispatcher
  17.     )
  18.     {
  19.         parent::__construct(
  20.             $this->templateResponder,
  21.             $this->redirectResponder
  22.         );
  23.     }
  24.     /**
  25.      * @Route("/mailchimp", name="mailchimp", methods={"GET"})
  26.      */
  27.     public function __invoke(Request $request)
  28.     {
  29.         $email $request->query->get('email');
  30.         $MailChimp = new MailChimp($_ENV['MAILCHIMP_API_KEY']);
  31.         $list_id $_ENV['MAILCHIMP_LIST_ID'];
  32.         $result $MailChimp->post("lists/$list_id/members", [
  33.             'email_address' => $email,
  34.             'status'        => 'subscribed',
  35.         ]);
  36.         if ($result['status'] == 400) {
  37.             $this->eventDispatcher->dispatch(
  38.                 new SessionMessageEvent('error''vous êtes déjà sur la liste'),
  39.                 SessionMessageEvent::SESSION_MESSAGE
  40.             );
  41.             return $this->redirectResponder->__invoke('index');
  42.         } else
  43.         $this->eventDispatcher->dispatch(
  44.             new SessionMessageEvent('success''merci'),
  45.             SessionMessageEvent::SESSION_MESSAGE
  46.         );
  47.         return $this->redirectResponder->__invoke('index');
  48.     }
  49. }