SaveHandler.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Model\ResourceModel;
  7. use Magento\Framework\EntityManager\MetadataPool;
  8. use Magento\Framework\EntityManager\Operation\AttributeInterface;
  9. /**
  10. * Class SaveHandler
  11. */
  12. class SaveHandler implements AttributeInterface
  13. {
  14. /**
  15. * @var Rule
  16. */
  17. protected $ruleResource;
  18. /**
  19. * @var MetadataPool
  20. */
  21. protected $metadataPool;
  22. /**
  23. * @param Rule $ruleResource
  24. * @param MetadataPool $metadataPool
  25. */
  26. public function __construct(
  27. Rule $ruleResource,
  28. MetadataPool $metadataPool
  29. ) {
  30. $this->ruleResource = $ruleResource;
  31. $this->metadataPool = $metadataPool;
  32. }
  33. /**
  34. * Save handler
  35. *
  36. * @param string $entityType
  37. * @param array $entityData
  38. * @param array $arguments
  39. * @return array
  40. * @throws \Exception
  41. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  42. */
  43. public function execute($entityType, $entityData, $arguments = [])
  44. {
  45. $linkField = $this->metadataPool->getMetadata($entityType)->getLinkField();
  46. if (isset($entityData['website_ids'])) {
  47. $websiteIds = $entityData['website_ids'];
  48. if (!is_array($websiteIds)) {
  49. $websiteIds = explode(',', (string)$websiteIds);
  50. }
  51. $this->ruleResource->bindRuleToEntity($entityData[$linkField], $websiteIds, 'website');
  52. }
  53. if (isset($entityData['customer_group_ids'])) {
  54. $customerGroupIds = $entityData['customer_group_ids'];
  55. if (!is_array($customerGroupIds)) {
  56. $customerGroupIds = explode(',', (string)$customerGroupIds);
  57. }
  58. $this->ruleResource->bindRuleToEntity($entityData[$linkField], $customerGroupIds, 'customer_group');
  59. }
  60. return $entityData;
  61. }
  62. }