TransactionWrapper.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * Plugin for \Magento\Customer\Api\CustomerRepositoryInterface
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Customer\Model\Plugin\CustomerRepository;
  9. class TransactionWrapper
  10. {
  11. /**
  12. * @var \Magento\Customer\Model\ResourceModel\Customer
  13. */
  14. protected $resourceModel;
  15. /**
  16. * @param \Magento\Customer\Model\ResourceModel\Customer $resourceModel
  17. */
  18. public function __construct(
  19. \Magento\Customer\Model\ResourceModel\Customer $resourceModel
  20. ) {
  21. $this->resourceModel = $resourceModel;
  22. }
  23. /**
  24. * @param \Magento\Customer\Api\CustomerRepositoryInterface $subject
  25. * @param callable $proceed
  26. * @param \Magento\Customer\Api\Data\CustomerInterface $customer
  27. * @param string $passwordHash
  28. * @return \Magento\Customer\Api\Data\CustomerInterface
  29. * @throws \Exception
  30. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  31. */
  32. public function aroundSave(
  33. \Magento\Customer\Api\CustomerRepositoryInterface $subject,
  34. \Closure $proceed,
  35. \Magento\Customer\Api\Data\CustomerInterface $customer,
  36. $passwordHash = null
  37. ) {
  38. $this->resourceModel->beginTransaction();
  39. try {
  40. /** @var $result \Magento\Customer\Api\Data\CustomerInterface */
  41. $result = $proceed($customer, $passwordHash);
  42. $this->resourceModel->commit();
  43. return $result;
  44. } catch (\Exception $e) {
  45. $this->resourceModel->rollBack();
  46. throw $e;
  47. }
  48. }
  49. }