AttributePersistor.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Model\ResourceModel;
  7. use Magento\Catalog\Model\ResourceModel\Attribute\ConditionBuilder;
  8. use Magento\Eav\Api\AttributeRepositoryInterface;
  9. use Magento\Framework\App\ObjectManager;
  10. use Magento\Framework\EntityManager\EntityMetadataInterface;
  11. use Magento\Framework\EntityManager\MetadataPool;
  12. use Magento\Framework\Locale\FormatInterface;
  13. use Magento\Framework\Model\Entity\ScopeInterface;
  14. use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
  15. use Magento\Catalog\Model\ResourceModel\Eav\Attribute as CatalogEavAttribute;
  16. class AttributePersistor extends \Magento\Eav\Model\ResourceModel\AttributePersistor
  17. {
  18. /**
  19. * @var ConditionBuilder
  20. */
  21. private $conditionBuilder;
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function __construct(
  26. FormatInterface $localeFormat,
  27. AttributeRepositoryInterface $attributeRepository,
  28. MetadataPool $metadataPool,
  29. ConditionBuilder $conditionBuilder = null
  30. ) {
  31. parent::__construct($localeFormat, $attributeRepository, $metadataPool);
  32. $this->conditionBuilder = $conditionBuilder ?: ObjectManager::getInstance()->get(ConditionBuilder::class);
  33. }
  34. /**
  35. * @param ScopeInterface $scope
  36. * @param AbstractAttribute $attribute
  37. * @param bool $useDefault
  38. * @return string
  39. */
  40. protected function getScopeValue(ScopeInterface $scope, AbstractAttribute $attribute, $useDefault = false)
  41. {
  42. if ($attribute instanceof CatalogEavAttribute) {
  43. $useDefault = $useDefault || $attribute->isScopeGlobal();
  44. }
  45. return parent::getScopeValue($scope, $attribute, $useDefault);
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. protected function buildUpdateConditions(
  51. AbstractAttribute $attribute,
  52. EntityMetadataInterface $metadata,
  53. array $scopes,
  54. $linkFieldValue
  55. ) {
  56. if ($this->isWebsiteAttribute($attribute)) {
  57. return $this->conditionBuilder->buildExistingAttributeWebsiteScope(
  58. $attribute,
  59. $metadata,
  60. $scopes,
  61. $linkFieldValue
  62. );
  63. }
  64. return parent::buildUpdateConditions($attribute, $metadata, $scopes, $linkFieldValue);
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. protected function buildInsertConditions(
  70. AbstractAttribute $attribute,
  71. EntityMetadataInterface $metadata,
  72. array $scopes,
  73. $linkFieldValue
  74. ) {
  75. if ($this->isWebsiteAttribute($attribute)) {
  76. return $this->conditionBuilder->buildNewAttributesWebsiteScope(
  77. $attribute,
  78. $metadata,
  79. $scopes,
  80. $linkFieldValue
  81. );
  82. }
  83. return parent::buildInsertConditions($attribute, $metadata, $scopes, $linkFieldValue);
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. protected function buildDeleteConditions(
  89. AbstractAttribute $attribute,
  90. EntityMetadataInterface $metadata,
  91. array $scopes,
  92. $linkFieldValue
  93. ) {
  94. if ($this->isWebsiteAttribute($attribute)) {
  95. return $this->conditionBuilder->buildExistingAttributeWebsiteScope(
  96. $attribute,
  97. $metadata,
  98. $scopes,
  99. $linkFieldValue
  100. );
  101. }
  102. return parent::buildDeleteConditions($attribute, $metadata, $scopes, $linkFieldValue);
  103. }
  104. /**
  105. * @param AbstractAttribute $attribute
  106. * @return bool
  107. */
  108. private function isWebsiteAttribute(AbstractAttribute $attribute)
  109. {
  110. return $attribute instanceof CatalogEavAttribute && $attribute->isScopeWebsite();
  111. }
  112. }