Commit c9664d48 authored by Fernando Andrés Muñoz Bravo's avatar Fernando Andrés Muñoz Bravo Committed by Fernando Andrés Muñoz Bravo
Browse files

Issue #3284459 by waspper, KondratievaS: Provide basic event subscribers

parent 516d7982
Loading
Loading
Loading
Loading
+57 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\auctioneer_commerce\Event;

use Drupal\commerce_order\Entity\OrderItemInterface;
use Drupal\auctioneer\Entity\BidInterface;
use Symfony\Component\EventDispatcher\Event;

/**
 * Defines the bid add to cart event.
 */
class BidAddToCartEvent extends Event {

  const EVENT_NAME = 'auctioneer.bid.event.add_to_cart';

  /**
   * The bid entity to be processed.
   *
   * @var \Drupal\auctioneer\Entity\BidInterface
   */
  protected $bid;

  /**
   * The order item entity to be processed.
   *
   * @var \Drupal\commerce_order\Entity\OrderItemInterface
   */
  protected $orderItem;

  /**
   * Constructor method.
   *
   * @param \Drupal\auctioneer\Entity\BidInterface $bid
   *   The bid object.
   * @param \Drupal\commerce_order\Entity\OrderItemInterface $order_item
   *   The order item object.
   */
  public function __construct(BidInterface $bid, OrderItemInterface $order_item) {
    $this->bid = $bid;
    $this->orderItem = $order_item;
  }

  /**
   * Get bid object.
   */
  public function bid() {
    return $this->bid;
  }

  /**
   * Get order item object.
   */
  public function orderItem() {
    return $this->orderItem;
  }

}
+16 −2
Original line number Diff line number Diff line
@@ -16,7 +16,9 @@ use Drupal\commerce_price\Price;
use Drupal\auctioneer\Entity\AuctionInterface;
use Drupal\auctioneer\Entity\BidInterface;
use Drupal\auctioneer\AuctionTypeManagerInterface;
use Drupal\auctioneer_commerce\Event\BidAddToCartEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
 * Provides the add to cart form to the winner's one.
@@ -74,6 +76,13 @@ class BidAddToCartForm extends FormBase {
   */
  protected $orderTypeResolver;

  /**
   * The event dispatcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * {@inheritdoc}
   */
@@ -82,7 +91,8 @@ class BidAddToCartForm extends FormBase {
    RouteMatchInterface $route_match,
    CartManagerInterface $cart_manager,
    CartProviderInterface $cart_provider,
    OrderTypeResolverInterface $order_type_resolver
    OrderTypeResolverInterface $order_type_resolver,
    EventDispatcherInterface $event_dispatcher
  ) {
    $this->userStorage = $auction_type_manager->entityTypeManager()->getStorage('user');
    // Our needed elements itself.
@@ -91,6 +101,7 @@ class BidAddToCartForm extends FormBase {
    $this->cartManager = $cart_manager;
    $this->cartProvider = $cart_provider;
    $this->orderTypeResolver = $order_type_resolver;
    $this->eventDispatcher = $event_dispatcher;
  }

  /**
@@ -102,7 +113,8 @@ class BidAddToCartForm extends FormBase {
      $container->get('current_route_match'),
      $container->get('commerce_cart.cart_manager'),
      $container->get('commerce_cart.cart_provider'),
      $container->get('commerce_order.chain_order_type_resolver')
      $container->get('commerce_order.chain_order_type_resolver'),
      $container->get('event_dispatcher')
    );
  }

@@ -283,6 +295,8 @@ class BidAddToCartForm extends FormBase {
            $cart = $this->cartProvider->createCart($order_type_id, $items['store'], $cart_owner);
          }
          if ($order_item = $this->cartManager->addOrderItem($cart, $order_item)) {
            $event = new BidAddToCartEvent($this->bid, $order_item);
            $this->eventDispatcher->dispatch(BidAddToCartEvent::EVENT_NAME, $event);
            $this->messenger()->addMessage($this->t("Bid was successfully added to the owner's cart."));
          }
        }
+1 −1
Original line number Diff line number Diff line
services:
  auctioneer_sniper.manager:
    class: Drupal\auctioneer_sniper\AuctionSniperManager
    arguments: ['@auctioneer_datetime_range.auction_type.manager']
    arguments: ['@auctioneer_datetime_range.auction_type.manager', '@event_dispatcher']
+16 −1
Original line number Diff line number Diff line
@@ -10,6 +10,8 @@ use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\auctioneer\Entity\AuctionInterface;
use Drupal\auctioneer\Entity\AuctionTypeInterface;
use Drupal\auctioneer_datetime_range\DatetimeRangeAuctionTypeManagerInterface;
use Drupal\auctioneer_sniper\Event\RequestAuctionLifetimeUpdateEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
 * Provides managing service for auction sniping operations.
@@ -25,14 +27,24 @@ class AuctionSniperManager implements AuctionSniperManagerInterface {
   */
  protected $datetimeAuctionTypeManager;

  /**
   * The event dispatcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * Constructor method.
   *
   * @param \Drupal\auctioneer_datetime_range\DatetimeRangeAuctionTypeManagerInterface $datetime_auction_type_manager
   *   The auction type manager.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
   *   The event dispatcher.
   */
  public function __construct(DatetimeRangeAuctionTypeManagerInterface $datetime_auction_type_manager) {
  public function __construct(DatetimeRangeAuctionTypeManagerInterface $datetime_auction_type_manager, EventDispatcherInterface $event_dispatcher) {
    $this->datetimeAuctionTypeManager = $datetime_auction_type_manager;
    $this->eventDispatcher = $event_dispatcher;
  }

  /**
@@ -184,6 +196,9 @@ class AuctionSniperManager implements AuctionSniperManagerInterface {
            }
          }
          $auction->save();
          // Dispatching event.
          $event = new RequestAuctionLifetimeUpdateEvent($auction, $new_values);
          $this->eventDispatcher->dispatch(RequestAuctionLifetimeUpdateEvent::EVENT_NAME, $event);
        }
      }
    }
+56 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\auctioneer_sniper\Event;

use Drupal\auctioneer\Entity\AuctionInterface;
use Symfony\Component\EventDispatcher\Event;

/**
 * Defines the lifetime update event.
 */
class RequestAuctionLifetimeUpdateEvent extends Event {

  const EVENT_NAME = 'auctioneer.auction.event.lifetime_update';

  /**
   * The auction entity to be processed.
   *
   * @var \Drupal\auctioneer\Entity\AuctionInterface
   */
  protected $auction;

  /**
   * The updated dates.
   *
   * @par array
   */
  protected $requestDates;

  /**
   * Constructor method.
   *
   * @param \Drupal\auctioneer\Entity\AuctionInterface $auction
   *   The auction object.
   * @param array $request_dates
   *   The updated datetime items.
   */
  public function __construct(AuctionInterface $auction, array $request_dates) {
    $this->auction = $auction;
    $this->requestDates = $request_dates;
  }

  /**
   * Get auction object.
   */
  public function auction() {
    return $this->auction;
  }

  /**
   * Get updated dates.
   */
  public function requestDates() {
    return $this->requestDates;
  }

}
Loading