SaveHandler.php 1.9 KB

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