diff --git a/src/AttributeValueResolver/DefaultResolver.php b/Model/AttributeValueResolver/DefaultResolver.php similarity index 65% rename from src/AttributeValueResolver/DefaultResolver.php rename to Model/AttributeValueResolver/DefaultResolver.php index ecdc2e7..833b13a 100644 --- a/src/AttributeValueResolver/DefaultResolver.php +++ b/Model/AttributeValueResolver/DefaultResolver.php @@ -1,16 +1,16 @@ inputFactory->create(['product' => $product]); + + return $this->getDocumentData->execute(Product::ENTITY, $input); + } +} diff --git a/Model/Command/GetDocumentDataByProductIdAndStoreId.php b/Model/Command/GetDocumentDataByProductIdAndStoreId.php new file mode 100644 index 0000000..15ad3b9 --- /dev/null +++ b/Model/Command/GetDocumentDataByProductIdAndStoreId.php @@ -0,0 +1,77 @@ +isProductAvailableInStore($productId, $storeId)) { + return null; + } + + $product = $this->productFactory->create(); + $product->setStoreId($storeId); + + $this->productResource->load($product, $productId); + + return $this->getDocumentData->execute($product); + } + + /** + * @param int $productId + * @param int $storeId + * @return bool + */ + private function isProductAvailableInStore(int $productId, int $storeId): bool + { + return in_array( + $this->getWebsiteIdByStoreId($storeId), + $this->getWebsiteIdsByProductId($productId), + true + ); + } + + /** + * @param int $storeId + * @return int + */ + private function getWebsiteIdByStoreId(int $storeId): int + { + try { + return (int)$this->storeManager->getStore($storeId)->getWebsiteId(); + } catch (NoSuchEntityException $exception) { + return Store::DEFAULT_STORE_ID; + } + } + + /** + * @param int $productId + * @return int[] + */ + private function getWebsiteIdsByProductId(int $productId): array + { + return array_map( + 'intval', + $this->productWebsiteLinkResource->getWebsiteIdsByProductId($productId) + ); + } +} diff --git a/src/Data/Input.php b/Model/Data/Input.php similarity index 69% rename from src/Data/Input.php rename to Model/Data/Input.php index e355b74..7cbccd6 100644 --- a/src/Data/Input.php +++ b/Model/Data/Input.php @@ -1,19 +1,16 @@ product = $product; } /** diff --git a/src/Plugin/ModifyRepository/GetDocumentNodeValue.php b/Plugin/ModifyRepository/GetDocumentNodeValue.php similarity index 55% rename from src/Plugin/ModifyRepository/GetDocumentNodeValue.php rename to Plugin/ModifyRepository/GetDocumentNodeValue.php index a7b262e..8cbf378 100644 --- a/src/Plugin/ModifyRepository/GetDocumentNodeValue.php +++ b/Plugin/ModifyRepository/GetDocumentNodeValue.php @@ -2,28 +2,24 @@ namespace MateuszMesek\DocumentDataCatalogProduct\Plugin\ModifyRepository; -use MateuszMesek\DocumentDataApi\Command\GetDocumentNodeValueInterface; -use MateuszMesek\DocumentDataApi\Data\DocumentDataInterface; -use MateuszMesek\DocumentDataApi\Data\DocumentNodeInterface; -use MateuszMesek\DocumentDataApi\InputInterface; -use MateuszMesek\DocumentDataCatalogProduct\Data\Input; +use MateuszMesek\DocumentDataApi\Model\Command\GetDocumentNodeValueInterface; +use MateuszMesek\DocumentDataApi\Model\Data\DocumentNodeInterface; +use MateuszMesek\DocumentDataApi\Model\InputInterface; +use MateuszMesek\DocumentDataCatalogProduct\Model\Data\Input; class GetDocumentNodeValue { - private State $state; - public function __construct( - State $state + private readonly State $state ) { - $this->state = $state; } public function aroundExecute( GetDocumentNodeValueInterface $getDocumentNodeValue, - callable $proceed, - DocumentNodeInterface $documentNode, - InputInterface $input + callable $proceed, + DocumentNodeInterface $documentNode, + InputInterface $input ) { if (!$input instanceof Input) { diff --git a/src/Plugin/ModifyRepository/OnCategoryRepository.php b/Plugin/ModifyRepository/OnCategoryRepository.php similarity index 77% rename from src/Plugin/ModifyRepository/OnCategoryRepository.php rename to Plugin/ModifyRepository/OnCategoryRepository.php index 640edde..91ecd3e 100644 --- a/src/Plugin/ModifyRepository/OnCategoryRepository.php +++ b/Plugin/ModifyRepository/OnCategoryRepository.php @@ -7,20 +7,17 @@ class OnCategoryRepository { - private State $state; - public function __construct( - State $state + private readonly State $state ) { - $this->state = $state; } public function aroundGet( CategoryRepositoryInterface $categoryRepository, - callable $proceed, - $categoryId, - $storeId = null + callable $proceed, + $categoryId, + $storeId = null ): CategoryInterface { $product = $this->state->getProduct(); diff --git a/src/Plugin/ModifyRepository/OnGetDocumentData.php b/Plugin/ModifyRepository/OnGetDocumentData.php similarity index 65% rename from src/Plugin/ModifyRepository/OnGetDocumentData.php rename to Plugin/ModifyRepository/OnGetDocumentData.php index db0c94d..89f72eb 100644 --- a/src/Plugin/ModifyRepository/OnGetDocumentData.php +++ b/Plugin/ModifyRepository/OnGetDocumentData.php @@ -3,23 +3,20 @@ namespace MateuszMesek\DocumentDataCatalogProduct\Plugin\ModifyRepository; use Magento\Catalog\Api\Data\ProductInterface; -use MateuszMesek\DocumentDataApi\Data\DocumentDataInterface; -use MateuszMesek\DocumentDataCatalogProduct\Command\GetDocumentData; +use MateuszMesek\DocumentDataApi\Model\Data\DocumentDataInterface; +use MateuszMesek\DocumentDataCatalogProduct\Model\Command\GetDocumentData; class OnGetDocumentData { - private State $state; - public function __construct( - State $state + private readonly State $state ) { - $this->state = $state; } public function aroundExecute( - GetDocumentData $getDocumentData, - callable $proceed, + GetDocumentData $getDocumentData, + callable $proceed, ProductInterface $product ): ?DocumentDataInterface { diff --git a/src/Plugin/ModifyRepository/OnProductRepository.php b/Plugin/ModifyRepository/OnProductRepository.php similarity index 70% rename from src/Plugin/ModifyRepository/OnProductRepository.php rename to Plugin/ModifyRepository/OnProductRepository.php index 3efb95c..ae5fd49 100644 --- a/src/Plugin/ModifyRepository/OnProductRepository.php +++ b/Plugin/ModifyRepository/OnProductRepository.php @@ -7,22 +7,19 @@ class OnProductRepository { - private State $state; - public function __construct( - State $state + private readonly State $state ) { - $this->state = $state; } public function aroundGet( ProductRepositoryInterface $productRepository, - callable $proceed, - $sku, - $editMode = false, - $storeId = null, - $forceReload = false + callable $proceed, + $sku, + $editMode = false, + $storeId = null, + $forceReload = false ): ProductInterface { $product = $this->state->getProduct(); @@ -42,11 +39,11 @@ public function aroundGet( public function aroundGetById( ProductRepositoryInterface $productRepository, - callable $proceed, - $productId, - $editMode = false, - $storeId = null, - $forceReload = false + callable $proceed, + $productId, + $editMode = false, + $storeId = null, + $forceReload = false ): ProductInterface { $product = $this->state->getProduct(); diff --git a/src/Plugin/ModifyRepository/State.php b/Plugin/ModifyRepository/State.php similarity index 100% rename from src/Plugin/ModifyRepository/State.php rename to Plugin/ModifyRepository/State.php diff --git a/composer.json b/composer.json index 4110916..f775b4c 100644 --- a/composer.json +++ b/composer.json @@ -3,10 +3,13 @@ "description": "N/A", "type": "magento2-module", "require": { + "php": ">=8.1", "magento/framework": "*", "magento/module-catalog": "*", - "mateuszmesek-magento2/module-document-data": "*", - "mateuszmesek-magento2/module-document-data-eav": ">=0.1.2" + "magento/module-eav": "*", + "magento/module-store": "*", + "mateuszmesek-magento2/module-document-data": "^1.0", + "mateuszmesek-magento2/module-document-data-eav": "^1.0" }, "license": [ "MIT" @@ -16,7 +19,7 @@ "registration.php" ], "psr-4": { - "MateuszMesek\\DocumentDataCatalogProduct\\": "src" + "MateuszMesek\\DocumentDataCatalogProduct\\": "" } } } diff --git a/etc/di.xml b/etc/di.xml index a70a94d..9259f30 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -1,39 +1,40 @@ - + - MateuszMesek\DocumentDataCatalogProduct\Command\GetDocumentNodes\CompositeResolver + MateuszMesek\DocumentDataCatalogProduct\Model\Command\GetDocumentNodes\CompositeResolver - + - MateuszMesek\DocumentDataCatalogProduct\Command\GetDocumentNodes\Config - MateuszMesek\DocumentDataCatalogProduct\Command\GetDocumentNodes\AttributeResolver + MateuszMesek\DocumentDataCatalogProduct\Model\Command\GetDocumentNodes\Config + MateuszMesek\DocumentDataCatalogProduct\Model\Command\GetDocumentNodes\AttributeResolver - + - Magento\Catalog\Model\Product::ENTITY + catalog_product - + + catalog_product Magento\Catalog\Model\Product::ENTITY - MateuszMesek\DocumentDataCatalogProduct\AttributeValueResolver - MateuszMesek\DocumentDataCatalogProduct\AttributeReturnTypeResolver + MateuszMesek\DocumentDataCatalogProduct\Model\AttributeValueResolver + MateuszMesek\DocumentDataCatalogProduct\Model\AttributeReturnTypeResolver - + - MateuszMesek\DocumentDataCatalogProduct\AttributeValueResolver\DefaultResolver + MateuszMesek\DocumentDataCatalogProduct\Model\AttributeValueResolver\DefaultResolver - - + + @@ -42,7 +43,7 @@ - + diff --git a/etc/module.xml b/etc/module.xml index 857b322..9e3fd54 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -3,7 +3,10 @@ + + + diff --git a/src/Command/GetDocumentData.php b/src/Command/GetDocumentData.php deleted file mode 100644 index 63a2602..0000000 --- a/src/Command/GetDocumentData.php +++ /dev/null @@ -1,31 +0,0 @@ -inputFactory = $inputFactory; - $this->getDocumentData = $getDocumentData; - } - - public function execute(ProductInterface $product): ?DocumentDataInterface - { - $input = $this->inputFactory->create(['product' => $product]); - - return $this->getDocumentData->execute(Product::ENTITY, $input); - } -} diff --git a/src/Command/GetDocumentDataByProductIdAndStoreId.php b/src/Command/GetDocumentDataByProductIdAndStoreId.php deleted file mode 100644 index e198fc7..0000000 --- a/src/Command/GetDocumentDataByProductIdAndStoreId.php +++ /dev/null @@ -1,35 +0,0 @@ -productResource = $productResource; - $this->productFactory = $productFactory; - $this->getDocumentData = $getDocumentData; - } - - public function execute(int $productId, int $storeId): ?DocumentDataInterface - { - $product = $this->productFactory->create(); - $product->setStoreId($storeId); - - $this->productResource->load($product, $productId); - - return $this->getDocumentData->execute($product); - } -}