Commit 9c7be94e authored by Daniel Pepin's avatar Daniel Pepin
Browse files

Issue #2536510: Setup webhook event subscriber/route to handle incoming webhooks.

parent 6b9da233
Loading
Loading
Loading
Loading
+7 −0
Changes for shopify.routing.yml: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
# ShopifyProduct routing definition
shopify.webhook:
  path: '/shopify/webhook'
  defaults:
    _controller: '\Drupal\shopify\Controller\ShopifyWebhook::handleIncomingWebhook'
  requirements:
    _permission: 'access content'

shopify.admin:
  path: '/admin/config/system/shopify'
  defaults:

shopify.services.yml

0 → 100644
+5 −0
Changes for shopify.services.yml: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
services:
  shopify.webhook_subscriber:
    class: Drupal\shopify\Event\ShopifyWebhookSubscriber
    tags:
      - {name: event_subscriber}
 No newline at end of file
+36 −0
Changes for src/Controller/ShopifyWebhook.php: 36 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php
/**
 * @file
 * Contains the default webhook controller.
 */
namespace Drupal\shopify\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\shopify\Event\ShopifyWebhookEvent;
use Symfony\Component\HttpFoundation\Response;

/**
 * Class ShopifyWebhook
 *
 * Provides the route functionality for stripe_api.webhook route.
 */
class ShopifyWebhook extends ControllerBase {

  /**
   * Captures the incoming webhook request.
   */
  public function handleIncomingWebhook() {
    $client = shopify_api_client();
    $data = $client->getIncomingWebhook($validate = TRUE);
    $topic = \Drupal::request()->headers->get('x-shopify-topic');

    // Dispatch the webhook event.
    $dispatcher = \Drupal::service('event_dispatcher');
    $e = new ShopifyWebhookEvent($topic, $data);
    $dispatcher->dispatch('shopify.webhook', $e);

    // Everything is okay.
    return new Response('Okay', Response::HTTP_OK);
  }

}
+32 −0
Changes for src/Event/ShopifyWebhookEvent.php: 32 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php
/**
 * @file
 * Contains the Stripe API webhook event object.
 */
namespace Drupal\shopify\Event;

use Symfony\Component\EventDispatcher\Event;

/**
 * Class ShopifyWebhookEvent
 *
 * Provides the Shopify Webhook Event.
 */
class ShopifyWebhookEvent extends Event {

  public $topic, $data;

  /**
   * Sets the default values for the event.
   *
   * @param string $topic
   *   Webhook topic (event name).
   * @param \stdClass $data
   *   Webhook event data.
   */
  public function __construct($topic, \stdClass $data) {
    $this->topic = $topic;
    $this->data = $data;
  }

}
+101 −0
Changes for src/Event/ShopifyWebhookSubscriber.php: 101 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php
/**
 * @file
 * Contains webhook subscriber functionality.
 */
namespace Drupal\shopify\Event;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Class ShopifyWebhookSubscriber
 *
 * Provides the webhook subscriber functionality.
 */
class ShopifyWebhookSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  static function getSubscribedEvents() {
    $events['shopify.webhook'][] = ['onIncomingWebhook'];
    return $events;
  }

  /**
   * Process an incoming webhook.
   *
   * @param \Drupal\shopify\Event\ShopifyWebhookEvent $event
   *   Logs an incoming webhook of the setting is on.
   */
  public function onIncomingWebhook(ShopifyWebhookEvent $event) {
    $config = \Drupal::config('shopify.webhooks');
    if ($config->get('log_webhooks')) {
      // Log this incoming webhook data.
      \Drupal::logger('shopify.webhook')->info(t('<strong>Topic:</strong> @topic<br />
      <strong>Data:</strong> @data.', [
        '@topic' => $event->topic,
        '@data' => var_export($event->data, TRUE),
      ]));
    }
    $method = 'webhook_' . str_replace('/', '_', $event->topic);
    if (method_exists($this, $method)) {
      $this->{$method}($event->data);
    }
  }

  /**
   * Handle updating of products.
   *
   * @param \stdClass $data
   */
  private function webhook_products_update(\stdClass $data) {
    // @todo: Needs functionality.
  }

  /**
   * Handle creating of products.
   *
   * @param \stdClass $data
   */
  private function webhook_products_create(\stdClass $data) {
    // @todo: Needs functionality.
  }

  /**
   * Handle deleting of products.
   *
   * @param \stdClass $data
   */
  private function webhook_products_delete(\stdClass $data) {
    // @todo: Needs functionality.
  }

  /**
   * Handle creating of collections.
   *
   * @param \stdClass $data
   */
  private function webhook_collections_create(\stdClass $data) {
    // @todo: Needs functionality.
  }

  /**
   * Handle updating of collections.
   *
   * @param \stdClass $data
   */
  private function webhook_collections_update(\stdClass $data) {
    // @todo: Needs functionality.
  }

  /**
   * Handle deleting of collections.
   *
   * @param \stdClass $data
   */
  private function webhook_collections_delete(\stdClass $data) {
    // @todo: Needs functionality.
  }

}