You are here

public function MailchimpWebhookController::endpoint in Mailchimp 8

Same name and namespace in other branches
  1. 2.x src/Controller/MailchimpWebhookController.php \Drupal\mailchimp\Controller\MailchimpWebhookController::endpoint()
1 string reference to 'MailchimpWebhookController::endpoint'
mailchimp.routing.yml in ./mailchimp.routing.yml
mailchimp.routing.yml

File

src/Controller/MailchimpWebhookController.php, line 56

Class

MailchimpWebhookController
Mailchimp Webhook controller.

Namespace

Drupal\mailchimp\Controller

Code

public function endpoint($hash) {
  $return = 0;

  // Return early if the hash in the request does not match.
  $webhook_hash = $this
    ->config('mailchimp.settings')
    ->get('webhook_hash');
  if (!empty($webhook_hash) && $webhook_hash !== $hash) {
    $response = new Response($return, Response::HTTP_FORBIDDEN, [
      'content-type' => 'text/plain',
    ]);
    return $response;
  }
  if (!empty($_POST)) {
    $data = $_POST['data'];
    $type = $_POST['type'];
    switch ($type) {
      case 'unsubscribe':
      case 'profile':
      case 'cleaned':
        mailchimp_get_memberinfo($data['list_id'], $data['email'], TRUE);
        break;
      case 'upemail':
        mailchimp_cache_clear_member($data['list_id'], $data['old_email']);
        mailchimp_get_memberinfo($data['list_id'], $data['new_email'], TRUE);
        break;
      case 'campaign':
        mailchimp_cache_clear_list_activity($data['list_id']);
        mailchimp_cache_clear_campaign($data['id']);
        break;
    }

    // Allow other modules to act on a webhook.
    $this->moduleHandler
      ->invokeAll('mailchimp_process_webhook', [
      $type,
      $data,
    ]);

    // Log event.
    $this->logger
      ->info('Webhook type {type} has been processed.', [
      'type' => $type,
    ]);
    $return = 1;
  }
  $response = new Response($return, Response::HTTP_OK, [
    'content-type' => 'text/plain',
  ]);
  return $response;
}