WishlistProvider.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Wishlist\Controller;
  8. use Magento\Framework\App\RequestInterface;
  9. class WishlistProvider implements WishlistProviderInterface
  10. {
  11. /**
  12. * @var \Magento\Wishlist\Model\Wishlist
  13. */
  14. protected $wishlist;
  15. /**
  16. * @var \Magento\Wishlist\Model\WishlistFactory
  17. */
  18. protected $wishlistFactory;
  19. /**
  20. * @var \Magento\Customer\Model\Session
  21. */
  22. protected $customerSession;
  23. /**
  24. * @var \Magento\Framework\Message\ManagerInterface
  25. */
  26. protected $messageManager;
  27. /**
  28. * @var \Magento\Framework\App\RequestInterface
  29. */
  30. protected $request;
  31. /**
  32. * @param \Magento\Wishlist\Model\WishlistFactory $wishlistFactory
  33. * @param \Magento\Customer\Model\Session $customerSession
  34. * @param \Magento\Framework\Message\ManagerInterface $messageManager
  35. * @param RequestInterface $request
  36. */
  37. public function __construct(
  38. \Magento\Wishlist\Model\WishlistFactory $wishlistFactory,
  39. \Magento\Customer\Model\Session $customerSession,
  40. \Magento\Framework\Message\ManagerInterface $messageManager,
  41. RequestInterface $request
  42. ) {
  43. $this->request = $request;
  44. $this->wishlistFactory = $wishlistFactory;
  45. $this->customerSession = $customerSession;
  46. $this->messageManager = $messageManager;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  51. */
  52. public function getWishlist($wishlistId = null)
  53. {
  54. if ($this->wishlist) {
  55. return $this->wishlist;
  56. }
  57. try {
  58. if (!$wishlistId) {
  59. $wishlistId = $this->request->getParam('wishlist_id');
  60. }
  61. $customerId = $this->customerSession->getCustomerId();
  62. $wishlist = $this->wishlistFactory->create();
  63. if (!$wishlistId && !$customerId) {
  64. return $wishlist;
  65. }
  66. if ($wishlistId) {
  67. $wishlist->load($wishlistId);
  68. } elseif ($customerId) {
  69. $wishlist->loadByCustomerId($customerId, true);
  70. }
  71. if (!$wishlist->getId() || $wishlist->getCustomerId() != $customerId) {
  72. throw new \Magento\Framework\Exception\NoSuchEntityException(
  73. __('The requested Wish List doesn\'t exist.')
  74. );
  75. }
  76. } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
  77. $this->messageManager->addError($e->getMessage());
  78. return false;
  79. } catch (\Exception $e) {
  80. $this->messageManager->addException($e, __('We can\'t create the Wish List right now.'));
  81. return false;
  82. }
  83. $this->wishlist = $wishlist;
  84. return $wishlist;
  85. }
  86. }