WishlistProvider.php in Commerce Wishlist 8.3
File
src/WishlistProvider.php
View source
<?php
namespace Drupal\commerce_wishlist;
use Drupal\commerce_wishlist\Exception\DuplicateWishlistException;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class WishlistProvider implements WishlistProviderInterface {
use StringTranslationTrait;
protected $wishlistStorage;
protected $currentUser;
protected $wishlistSession;
protected $wishlistData = [];
protected $configFactory;
public function __construct(EntityTypeManagerInterface $entity_type_manager, AccountInterface $current_user, WishlistSessionInterface $wishlist_session, ConfigFactoryInterface $config_factory) {
$this->wishlistStorage = $entity_type_manager
->getStorage('commerce_wishlist');
$this->currentUser = $current_user;
$this->wishlistSession = $wishlist_session;
$this->configFactory = $config_factory;
}
public function createWishlist($wishlist_type, AccountInterface $account = NULL, $name = NULL) {
$account = $account ?: $this->currentUser;
$uid = $account
->id();
$allow_multiple = $this->configFactory
->get('commerce_wishlist.settings')
->get('allow_multiple');
if (empty($allow_multiple) && $this
->getWishlistId($wishlist_type, $account)) {
throw new DuplicateWishlistException("A wishlist for type '{$wishlist_type}' and account '{$uid}' already exists.");
}
$wishlist = $this->wishlistStorage
->create([
'type' => $wishlist_type,
'uid' => $uid,
'name' => $name ?: $this
->t('Wishlist'),
'is_default' => TRUE,
]);
$wishlist
->save();
if ($account
->isAnonymous()) {
$this->wishlistSession
->addWishlistId($wishlist
->id());
}
if (isset($this->wishlistData[$uid])) {
$this->wishlistData[$uid][$wishlist
->id()] = [
'type' => $wishlist_type,
];
}
return $wishlist;
}
public function getWishlist($wishlist_type, AccountInterface $account = NULL) {
$wishlist = NULL;
$wishlist_id = $this
->getWishlistId($wishlist_type, $account);
if ($wishlist_id) {
$wishlist = $this->wishlistStorage
->load($wishlist_id);
}
return $wishlist;
}
public function getWishlistId($wishlist_type, AccountInterface $account = NULL) {
$wishlist_id = NULL;
$wishlist_data = $this
->loadWishlistData($account);
if ($wishlist_data) {
$search = [
'type' => $wishlist_type,
];
$wishlist_id = array_search($search, $wishlist_data);
}
return $wishlist_id;
}
public function getWishlists(AccountInterface $account = NULL) {
$wishlists = [];
$wishlist_ids = $this
->getWishlistIds($account);
if ($wishlist_ids) {
$wishlists = $this->wishlistStorage
->loadMultiple($wishlist_ids);
}
return $wishlists;
}
public function getWishlistIds(AccountInterface $account = NULL) {
$wishlist_data = $this
->loadWishlistData($account);
return array_keys($wishlist_data);
}
protected function loadWishlistData(AccountInterface $account = NULL) {
$account = $account ?: $this->currentUser;
$uid = $account
->id();
if (isset($this->wishlistData[$uid])) {
return $this->wishlistData[$uid];
}
if ($account
->isAuthenticated()) {
$query = $this->wishlistStorage
->getQuery()
->condition('uid', $account
->id())
->sort('is_default', 'DESC')
->sort('wishlist_id', 'DESC')
->accessCheck(FALSE);
$wishlist_ids = $query
->execute();
}
else {
$wishlist_ids = $this->wishlistSession
->getWishlistIds();
}
$this->wishlistData[$uid] = [];
if (!$wishlist_ids) {
return [];
}
$wishlists = $this->wishlistStorage
->loadMultiple($wishlist_ids);
foreach ($wishlists as $wishlist) {
if ($wishlist
->getOwnerId() != $uid) {
continue;
}
$this->wishlistData[$uid][$wishlist
->id()] = [
'type' => $wishlist
->bundle(),
];
}
return $this->wishlistData[$uid];
}
}