AccessChangeQuoteControl.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model\QuoteRepository\Plugin;
  7. use Magento\Quote\Api\ChangeQuoteControlInterface;
  8. use Magento\Framework\Exception\StateException;
  9. use Magento\Quote\Api\CartRepositoryInterface;
  10. use Magento\Quote\Api\Data\CartInterface;
  11. /**
  12. * The plugin checks if the user has ability to change the quote.
  13. */
  14. class AccessChangeQuoteControl
  15. {
  16. /**
  17. * @var ChangeQuoteControlInterface $changeQuoteControl
  18. */
  19. private $changeQuoteControl;
  20. /**
  21. * @param ChangeQuoteControlInterface $changeQuoteControl
  22. */
  23. public function __construct(ChangeQuoteControlInterface $changeQuoteControl)
  24. {
  25. $this->changeQuoteControl = $changeQuoteControl;
  26. }
  27. /**
  28. * Checks if change quote's customer id is allowed for current user.
  29. *
  30. * @param CartRepositoryInterface $subject
  31. * @param CartInterface $quote
  32. * @throws StateException if Guest has customer_id or Customer's customer_id not much with user_id
  33. * or unknown user's type
  34. * @return void
  35. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  36. */
  37. public function beforeSave(CartRepositoryInterface $subject, CartInterface $quote)
  38. {
  39. if (! $this->changeQuoteControl->isAllowed($quote)) {
  40. throw new StateException(__("Invalid state change requested"));
  41. }
  42. }
  43. }