MailchimpWebhookController.php in Mailchimp 2.x
File
src/Controller/MailchimpWebhookController.php
View source
<?php
namespace Drupal\mailchimp\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MailchimpWebhookController extends ControllerBase {
protected $moduleHandler;
protected $logger;
public function __construct(ModuleHandlerInterface $moduleHandler, LoggerInterface $logger) {
$this->moduleHandler = $moduleHandler;
$this->logger = $logger;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('module_handler'), $container
->get('logger.factory')
->get('mailchimp'));
}
public function endpoint($hash) {
$return = 0;
$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;
}
$this->moduleHandler
->invokeAll('mailchimp_process_webhook', [
$type,
$data,
]);
$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;
}
}