You are here

class WishlistAssignment in Commerce Wishlist 8.3

Default wishlist assignment implementation.

Hierarchy

Expanded class hierarchy of WishlistAssignment

1 string reference to 'WishlistAssignment'
commerce_wishlist.services.yml in ./commerce_wishlist.services.yml
commerce_wishlist.services.yml
1 service uses WishlistAssignment
commerce_wishlist.wishlist_assignment in ./commerce_wishlist.services.yml
Drupal\commerce_wishlist\WishlistAssignment

File

src/WishlistAssignment.php, line 16

Namespace

Drupal\commerce_wishlist
View source
class WishlistAssignment implements WishlistAssignmentInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

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

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The wishlist manager.
   *
   * @var \Drupal\commerce_wishlist\WishlistManagerInterface
   */
  protected $wishlistManager;

  /**
   * Constructs a new WishlistAssignment object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
   *   The event dispatcher.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\commerce_wishlist\WishlistManagerInterface $wishlist_manager
   *   The wishlist manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, ConfigFactoryInterface $config_factory, WishlistManagerInterface $wishlist_manager) {
    $this->entityTypeManager = $entity_type_manager;
    $this->eventDispatcher = $event_dispatcher;
    $this->configFactory = $config_factory;
    $this->wishlistManager = $wishlist_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function assign(WishlistInterface $wishlist, UserInterface $account) {
    if (!empty($wishlist
      ->getOwnerId())) {

      // Skip wishlists which already have an owner.
      return;
    }
    $wishlist
      ->setOwner($account);

    // Update the referenced shipping profile.
    $shipping_profile = $wishlist
      ->getShippingProfile();
    if ($shipping_profile && empty($shipping_profile
      ->getOwnerId())) {
      $shipping_profile
        ->setOwner($account);
      $shipping_profile
        ->save();
    }

    // Notify other modules.
    $event = new WishlistAssignEvent($wishlist, $account);
    $this->eventDispatcher
      ->dispatch(WishlistEvents::WISHLIST_ASSIGN, $event);
    $wishlist
      ->save();
  }

  /**
   * {@inheritdoc}
   */
  public function assignMultiple(array $wishlists, UserInterface $account) {
    $allow_multiple = (bool) $this->configFactory
      ->get('commerce_wishlist.settings')
      ->get('allow_multiple');

    /** @var \Drupal\commerce_wishlist\WishlistStorageInterface $wishlist_storage */
    $wishlist_storage = $this->entityTypeManager
      ->getStorage('commerce_wishlist');
    foreach ($wishlists as $wishlist) {
      $default_wishlist = $wishlist_storage
        ->loadDefaultByUser($account, $wishlist
        ->bundle());

      // Check if multiple wishlists are allowed, in which case we're assigning
      // the wishlist to the given account.
      if ($allow_multiple || !$default_wishlist) {
        $this
          ->assign($wishlist, $account);
        continue;
      }

      // In case a single wishlist is allowed, we need to merge the wishlist
      // items with the default wishlist.
      $this->wishlistManager
        ->merge($wishlist, $default_wishlist);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
WishlistAssignment::$configFactory protected property The config factory.
WishlistAssignment::$entityTypeManager protected property The entity type manager.
WishlistAssignment::$eventDispatcher protected property The event dispatcher.
WishlistAssignment::$wishlistManager protected property The wishlist manager.
WishlistAssignment::assign public function Assigns the anonymous wishlist to the given user account. Overrides WishlistAssignmentInterface::assign
WishlistAssignment::assignMultiple public function Assigns multiple anonymous wishlists to the given user account. Overrides WishlistAssignmentInterface::assignMultiple
WishlistAssignment::__construct public function Constructs a new WishlistAssignment object.