StoreViewService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogUrlRewrite\Service\V1;
  7. use Magento\Eav\Model\Config;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\Framework\EntityManager\MetadataPool;
  10. use Magento\Catalog\Api\Data\ProductInterface;
  11. /**
  12. * Store view service
  13. */
  14. class StoreViewService
  15. {
  16. /**
  17. * @var \Magento\Eav\Model\Config
  18. */
  19. protected $eavConfig;
  20. /**
  21. * @var \Magento\Framework\DB\Adapter\AdapterInterface
  22. */
  23. protected $connection;
  24. /**
  25. * @var MetadataPool
  26. */
  27. protected $metadataPool;
  28. /**
  29. * @param Config $eavConfig
  30. * @param \Magento\Framework\App\ResourceConnection $resource
  31. */
  32. public function __construct(
  33. Config $eavConfig,
  34. ResourceConnection $resource
  35. ) {
  36. $this->eavConfig = $eavConfig;
  37. $this->connection = $resource->getConnection();
  38. }
  39. /**
  40. * Check that entity has overridden url key for specific store
  41. *
  42. * @param int $storeId
  43. * @param int $entityId
  44. * @param string $entityType
  45. * @throws \InvalidArgumentException
  46. * @return bool
  47. */
  48. public function doesEntityHaveOverriddenUrlKeyForStore($storeId, $entityId, $entityType)
  49. {
  50. return $this->doesEntityHaveOverriddenUrlAttributeForStore($storeId, $entityId, $entityType, 'url_key');
  51. }
  52. /**
  53. * Check that entity has overridden url path for specific store
  54. *
  55. * @param int $storeId
  56. * @param int $entityId
  57. * @param string $entityType
  58. * @throws \InvalidArgumentException
  59. * @return bool
  60. */
  61. public function doesEntityHaveOverriddenUrlPathForStore($storeId, $entityId, $entityType)
  62. {
  63. return $this->doesEntityHaveOverriddenUrlAttributeForStore($storeId, $entityId, $entityType, 'url_path');
  64. }
  65. /**
  66. * Check that entity has overridden url attribute for specific store
  67. *
  68. * @param int $storeId
  69. * @param int $entityId
  70. * @param string $entityType
  71. * @param mixed $attributeName
  72. * @throws \InvalidArgumentException
  73. * @return bool
  74. */
  75. protected function doesEntityHaveOverriddenUrlAttributeForStore($storeId, $entityId, $entityType, $attributeName)
  76. {
  77. $attribute = $this->eavConfig->getAttribute($entityType, $attributeName);
  78. if (!$attribute) {
  79. throw new \InvalidArgumentException(sprintf('Cannot retrieve attribute for entity type "%s"', $entityType));
  80. }
  81. $linkFieldName = $attribute->getEntity()->getLinkField();
  82. if (!$linkFieldName) {
  83. $linkFieldName = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
  84. }
  85. $select = $this->connection->select()
  86. ->from(['e' => $attribute->getEntity()->getEntityTable()], [])
  87. ->join(
  88. ['e_attr' => $attribute->getBackendTable()],
  89. "e.{$linkFieldName} = e_attr.{$linkFieldName}",
  90. 'store_id'
  91. )->where('e_attr.attribute_id = ?', $attribute->getId())
  92. ->where('e.entity_id = ?', $entityId);
  93. return in_array($storeId, $this->connection->fetchCol($select));
  94. }
  95. /**
  96. * Get product metadata pool
  97. *
  98. * @return \Magento\Framework\EntityManager\MetadataPool
  99. */
  100. private function getMetadataPool()
  101. {
  102. if (!$this->metadataPool) {
  103. $this->metadataPool = \Magento\Framework\App\ObjectManager::getInstance()
  104. ->get(\Magento\Framework\EntityManager\MetadataPool::class);
  105. }
  106. return $this->metadataPool;
  107. }
  108. }