You are here

class ProductEventSubscriber in Mailchimp E-Commerce 8

Event Subscriber for Commerce Products.

Hierarchy

  • class \Drupal\mailchimp_ecommerce_commerce\EventSubscriber\ProductEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of ProductEventSubscriber

1 string reference to 'ProductEventSubscriber'
mailchimp_ecommerce_commerce.services.yml in modules/mailchimp_ecommerce_commerce/mailchimp_ecommerce_commerce.services.yml
modules/mailchimp_ecommerce_commerce/mailchimp_ecommerce_commerce.services.yml
1 service uses ProductEventSubscriber
mailchimp_ecommerce_commerce.product_event_subscriber in modules/mailchimp_ecommerce_commerce/mailchimp_ecommerce_commerce.services.yml
\Drupal\mailchimp_ecommerce_commerce\EventSubscriber\ProductEventSubscriber

File

modules/mailchimp_ecommerce_commerce/src/EventSubscriber/ProductEventSubscriber.php, line 15

Namespace

Drupal\mailchimp_ecommerce_commerce\EventSubscriber
View source
class ProductEventSubscriber implements EventSubscriberInterface {

  /**
   * The Product Handler.
   *
   * @var \Drupal\mailchimp_ecommerce\ProductHandler
   */
  private $product_handler;

  /**
   * ProductEventSubscriber constructor.
   *
   * @param \Drupal\mailchimp_ecommerce\ProductHandler $product_handler
   *   The Product Handler.
   */
  public function __construct(ProductHandler $product_handler) {
    $this->product_handler = $product_handler;
  }

  /**
   * Respond to event fired after saving a new product.
   */
  public function productInsert(ProductEvent $event) {

    /** @var Product $product */
    $product = $event
      ->getProduct();
    $product_id = $product
      ->get('product_id')->value;
    $title = !empty($product
      ->get('title')->value) ? $product
      ->get('title')->value : '';
    $description = !empty($product
      ->get('body')->value) ? $product
      ->get('body')->value : '';

    // TODO Fix Type
    $type = !empty($product
      ->get('type')->value) ? $product
      ->get('type')->value : '';
    $variants = $this->product_handler
      ->buildProductVariants($product);
    $url = $this->product_handler
      ->buildProductUrl($product);
    $image_url = $this->product_handler
      ->getProductImageUrl($product);
    $this->product_handler
      ->addProduct($product_id, $title, $url, $image_url, $description, $type, $variants);
  }

  /**
   * Respond to event fired after updating an existing product.
   */
  public function productUpdate(ProductEvent $event) {
    $product = $event
      ->getProduct();
    $title = !empty($product
      ->get('title')->value) ? $product
      ->get('title')->value : '';
    $description = !empty($product
      ->get('body')->value) ? $product
      ->get('body')->value : '';

    // TODO Fix Type
    $type = !empty($product
      ->get('type')->value) ? $product
      ->get('type')->value : '';
    $variants = $this->product_handler
      ->buildProductVariants($product);
    $url = $this->product_handler
      ->buildProductUrl($product);
    $image_url = $this->product_handler
      ->getProductImageUrl($product);

    // Update the existing product and variant.
    $this->product_handler
      ->updateProduct($product, $title, $url, $image_url, $description, $type, $variants);
  }

  /**
   * Respond to event fired after deleting a product.
   */
  public function productDelete(ProductEvent $event) {

    // TODO: Process deleted product.
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[ProductEvents::PRODUCT_INSERT][] = [
      'productInsert',
    ];
    $events[ProductEvents::PRODUCT_UPDATE][] = [
      'productUpdate',
    ];
    $events[ProductEvents::PRODUCT_DELETE][] = [
      'productDelete',
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ProductEventSubscriber::$product_handler private property The Product Handler.
ProductEventSubscriber::getSubscribedEvents public static function
ProductEventSubscriber::productDelete public function Respond to event fired after deleting a product.
ProductEventSubscriber::productInsert public function Respond to event fired after saving a new product.
ProductEventSubscriber::productUpdate public function Respond to event fired after updating an existing product.
ProductEventSubscriber::__construct public function ProductEventSubscriber constructor.