ConditionBuilder.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Model\ResourceModel\Attribute;
  7. use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
  8. use Magento\Framework\EntityManager\EntityMetadataInterface;
  9. use Magento\Store\Api\Data\StoreInterface;
  10. use Magento\Store\Model\Store;
  11. use Magento\Store\Model\StoreManagerInterface;
  12. use Magento\Catalog\Model\ResourceModel\Eav\Attribute as CatalogEavAttribute;
  13. use Magento\Store\Model\Website;
  14. use Magento\Framework\Model\Entity\ScopeInterface;
  15. /**
  16. * Builds scope-related conditions for catalog attributes
  17. *
  18. * Class ConditionBuilder
  19. * @package Magento\Catalog\Model\ResourceModel\Attribute
  20. */
  21. class ConditionBuilder
  22. {
  23. /**
  24. * @var StoreManagerInterface
  25. */
  26. private $storeManager;
  27. /**
  28. * ConditionBuilder constructor
  29. * @param StoreManagerInterface $storeManager
  30. */
  31. public function __construct(StoreManagerInterface $storeManager)
  32. {
  33. $this->storeManager = $storeManager;
  34. }
  35. /**
  36. * Returns conditions for existing attribute actions (update/delete) if attribute scope is "website"
  37. *
  38. * @param AbstractAttribute $attribute
  39. * @param EntityMetadataInterface $metadata
  40. * @param ScopeInterface[] $scopes
  41. * @param string $linkFieldValue
  42. * @return array
  43. */
  44. public function buildExistingAttributeWebsiteScope(
  45. AbstractAttribute $attribute,
  46. EntityMetadataInterface $metadata,
  47. array $scopes,
  48. $linkFieldValue
  49. ) {
  50. $website = $this->getWebsiteForWebsiteScope($scopes);
  51. if (!$website) {
  52. return [];
  53. }
  54. $storeIds = $website->getStoreIds();
  55. $condition = [
  56. $metadata->getLinkField() . ' = ?' => $linkFieldValue,
  57. 'attribute_id = ?' => $attribute->getAttributeId(),
  58. ];
  59. $conditions = [];
  60. foreach ($storeIds as $storeId) {
  61. $identifier = $metadata->getEntityConnection()->quoteIdentifier(Store::STORE_ID);
  62. $condition[$identifier . ' = ?'] = $storeId;
  63. $conditions[] = $condition;
  64. }
  65. return $conditions;
  66. }
  67. /**
  68. * Returns conditions for new attribute action (insert) if attribute scope is "website"
  69. *
  70. * @param AbstractAttribute $attribute
  71. * @param EntityMetadataInterface $metadata
  72. * @param ScopeInterface[] $scopes
  73. * @param string $linkFieldValue
  74. * @return array
  75. */
  76. public function buildNewAttributesWebsiteScope(
  77. AbstractAttribute $attribute,
  78. EntityMetadataInterface $metadata,
  79. array $scopes,
  80. $linkFieldValue
  81. ) {
  82. $website = $this->getWebsiteForWebsiteScope($scopes);
  83. if (!$website) {
  84. return [];
  85. }
  86. $storeIds = $website->getStoreIds();
  87. $condition = [
  88. $metadata->getLinkField() => $linkFieldValue,
  89. 'attribute_id' => $attribute->getAttributeId(),
  90. ];
  91. $conditions = [];
  92. foreach ($storeIds as $storeId) {
  93. $condition[Store::STORE_ID] = $storeId;
  94. $conditions[] = $condition;
  95. }
  96. return $conditions;
  97. }
  98. /**
  99. * @param array $scopes
  100. * @return null|Website
  101. */
  102. private function getWebsiteForWebsiteScope(array $scopes)
  103. {
  104. $store = $this->getStoreFromScopes($scopes);
  105. return $store ? $store->getWebsite() : null;
  106. }
  107. /**
  108. * @param ScopeInterface[] $scopes
  109. * @return StoreInterface|null
  110. */
  111. private function getStoreFromScopes(array $scopes)
  112. {
  113. foreach ($scopes as $scope) {
  114. if (Store::STORE_ID === $scope->getIdentifier()) {
  115. return $this->storeManager->getStore($scope->getValue());
  116. }
  117. }
  118. return null;
  119. }
  120. }