Skip to content

Commit

Permalink
changed directory structure
Browse files Browse the repository at this point in the history
  • Loading branch information
MateuszMesek committed Jan 11, 2023
1 parent 7d59612 commit 6b846e8
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 136 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php declare(strict_types=1);

namespace MateuszMesek\DocumentDataCatalogProduct\AttributeValueResolver;
namespace MateuszMesek\DocumentDataCatalogProduct\Model\AttributeValueResolver;

use Magento\Catalog\Model\Product;
use Magento\Eav\Api\Data\AttributeInterface;
use MateuszMesek\DocumentDataApi\InputInterface;
use MateuszMesek\DocumentDataCatalogProduct\Data\Input;
use MateuszMesek\DocumentDataEavApi\AttributeValueResolverInterface;
use MateuszMesek\DocumentDataApi\Model\InputInterface;
use MateuszMesek\DocumentDataCatalogProduct\Model\Data\Input;
use MateuszMesek\DocumentDataEavApi\Model\AttributeValueResolverInterface;

class DefaultResolver implements AttributeValueResolverInterface
{
public function resolver(AttributeInterface $attribute, InputInterface $input)
public function resolver(AttributeInterface $attribute, InputInterface $input): mixed
{
if (!$input instanceof Input) {
return null;
Expand Down
26 changes: 26 additions & 0 deletions Model/Command/GetDocumentData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types=1);

namespace MateuszMesek\DocumentDataCatalogProduct\Model\Command;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\Product;
use MateuszMesek\DocumentDataApi\Model\Command\GetDocumentDataInterface;
use MateuszMesek\DocumentDataApi\Model\Data\DocumentDataInterface;
use MateuszMesek\DocumentDataCatalogProduct\Model\Data\InputFactory;

class GetDocumentData
{
public function __construct(
private readonly InputFactory $inputFactory,
private readonly GetDocumentDataInterface $getDocumentData
)
{
}

public function execute(ProductInterface $product): ?DocumentDataInterface
{
$input = $this->inputFactory->create(['product' => $product]);

return $this->getDocumentData->execute(Product::ENTITY, $input);
}
}
77 changes: 77 additions & 0 deletions Model/Command/GetDocumentDataByProductIdAndStoreId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php declare(strict_types=1);

namespace MateuszMesek\DocumentDataCatalogProduct\Model\Command;

use Magento\Catalog\Model\ProductFactory;
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
use Magento\Catalog\Model\ResourceModel\Product\Website\Link as ProductWebsiteLinkResource;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use MateuszMesek\DocumentDataApi\Model\Data\DocumentDataInterface;

class GetDocumentDataByProductIdAndStoreId
{
public function __construct(
private readonly ProductWebsiteLinkResource $productWebsiteLinkResource,
private readonly StoreManagerInterface $storeManager,
private readonly ProductResource $productResource,
private readonly ProductFactory $productFactory,
private readonly GetDocumentData $getDocumentData
)
{
}

public function execute(int $productId, int $storeId): ?DocumentDataInterface
{
if (!$this->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)
);
}
}
9 changes: 3 additions & 6 deletions src/Data/Input.php → Model/Data/Input.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
<?php declare(strict_types=1);

namespace MateuszMesek\DocumentDataCatalogProduct\Data;
namespace MateuszMesek\DocumentDataCatalogProduct\Model\Data;

use Magento\Catalog\Api\Data\ProductInterface;
use MateuszMesek\DocumentDataApi\InputInterface;
use MateuszMesek\DocumentDataApi\Model\InputInterface;

class Input implements InputInterface
{
private ProductInterface $product;

public function __construct(
ProductInterface $product
private readonly ProductInterface $product
)
{
$this->product = $product;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
File renamed without changes.
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -16,7 +19,7 @@
"registration.php"
],
"psr-4": {
"MateuszMesek\\DocumentDataCatalogProduct\\": "src"
"MateuszMesek\\DocumentDataCatalogProduct\\": ""
}
}
}
31 changes: 16 additions & 15 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MateuszMesek\DocumentData\Command\GetDocumentNodes\ResolverPool">
<type name="MateuszMesek\DocumentData\Model\Command\GetDocumentNodes\ResolverPool">
<arguments>
<argument name="documents" xsi:type="array">
<item name="catalog_product" xsi:type="string">MateuszMesek\DocumentDataCatalogProduct\Command\GetDocumentNodes\CompositeResolver</item>
<item name="catalog_product" xsi:type="string">MateuszMesek\DocumentDataCatalogProduct\Model\Command\GetDocumentNodes\CompositeResolver</item>
</argument>
</arguments>
</type>
<virtualType name="MateuszMesek\DocumentDataCatalogProduct\Command\GetDocumentNodes\CompositeResolver" type="MateuszMesek\DocumentData\Command\GetDocumentNodes\CompositeResolver">
<virtualType name="MateuszMesek\DocumentDataCatalogProduct\Model\Command\GetDocumentNodes\CompositeResolver" type="MateuszMesek\DocumentData\Model\Command\GetDocumentNodes\CompositeResolver">
<arguments>
<argument name="resolvers" xsi:type="array">
<item name="config" xsi:type="string">MateuszMesek\DocumentDataCatalogProduct\Command\GetDocumentNodes\Config</item>
<item name="attribute" xsi:type="string">MateuszMesek\DocumentDataCatalogProduct\Command\GetDocumentNodes\AttributeResolver</item>
<item name="config" xsi:type="string">MateuszMesek\DocumentDataCatalogProduct\Model\Command\GetDocumentNodes\Config</item>
<item name="attribute" xsi:type="string">MateuszMesek\DocumentDataCatalogProduct\Model\Command\GetDocumentNodes\AttributeResolver</item>
</argument>
</arguments>
</virtualType>
<virtualType name="MateuszMesek\DocumentDataCatalogProduct\Command\GetDocumentNodes\Config" type="MateuszMesek\DocumentData\Command\GetDocumentNodes\ConfigResolver">
<virtualType name="MateuszMesek\DocumentDataCatalogProduct\Model\Command\GetDocumentNodes\Config" type="MateuszMesek\DocumentData\Model\Command\GetDocumentNodes\ConfigResolver">
<arguments>
<argument name="documentName" xsi:type="const">Magento\Catalog\Model\Product::ENTITY</argument>
<argument name="documentName" xsi:type="string">catalog_product</argument>
</arguments>
</virtualType>
<virtualType name="MateuszMesek\DocumentDataCatalogProduct\Command\GetDocumentNodes\AttributeResolver" type="MateuszMesek\DocumentDataEav\DocumentNodesResolver">
<virtualType name="MateuszMesek\DocumentDataCatalogProduct\Model\Command\GetDocumentNodes\AttributeResolver" type="MateuszMesek\DocumentDataEav\Model\DocumentNodesResolver">
<arguments>
<argument name="documentName" xsi:type="string">catalog_product</argument>
<argument name="entityType" xsi:type="const">Magento\Catalog\Model\Product::ENTITY</argument>
<argument name="attributeValueResolver" xsi:type="object">MateuszMesek\DocumentDataCatalogProduct\AttributeValueResolver</argument>
<argument name="attributeReturnTypeResolver" xsi:type="object">MateuszMesek\DocumentDataCatalogProduct\AttributeReturnTypeResolver</argument>
<argument name="attributeValueResolver" xsi:type="object">MateuszMesek\DocumentDataCatalogProduct\Model\AttributeValueResolver</argument>
<argument name="attributeReturnTypeResolver" xsi:type="object">MateuszMesek\DocumentDataCatalogProduct\Model\AttributeReturnTypeResolver</argument>
</arguments>
</virtualType>
<virtualType name="MateuszMesek\DocumentDataCatalogProduct\AttributeValueResolver" type="MateuszMesek\DocumentDataEav\AttributeValueResolver\ByAttributeCode">
<virtualType name="MateuszMesek\DocumentDataCatalogProduct\Model\AttributeValueResolver" type="MateuszMesek\DocumentDataEav\Model\AttributeValueResolver\ByAttributeCode">
<arguments>
<argument name="defaultResolver" xsi:type="object">MateuszMesek\DocumentDataCatalogProduct\AttributeValueResolver\DefaultResolver</argument>
<argument name="defaultResolver" xsi:type="object">MateuszMesek\DocumentDataCatalogProduct\Model\AttributeValueResolver\DefaultResolver</argument>
</arguments>
</virtualType>
<virtualType name="MateuszMesek\DocumentDataCatalogProduct\AttributeReturnTypeResolver" type="MateuszMesek\DocumentDataEav\AttributeReturnTypeResolver\ByAttributeCode"/>
<type name="MateuszMesek\DocumentDataCatalogProduct\Command\GetDocumentData">
<virtualType name="MateuszMesek\DocumentDataCatalogProduct\Model\AttributeReturnTypeResolver" type="MateuszMesek\DocumentDataEav\Model\AttributeReturnTypeResolver\ByAttributeCode"/>
<type name="MateuszMesek\DocumentDataCatalogProduct\Model\Command\GetDocumentData">
<plugin name="MateuszMesek_DocumentDataCatalogProduct" type="MateuszMesek\DocumentDataCatalogProduct\Plugin\ModifyRepository\OnGetDocumentData"/>
</type>
<type name="Magento\Catalog\Api\CategoryRepositoryInterface">
Expand All @@ -42,7 +43,7 @@
<type name="Magento\Catalog\Api\ProductRepositoryInterface">
<plugin name="MateuszMesek_DocumentDataCatalogProduct" type="MateuszMesek\DocumentDataCatalogProduct\Plugin\ModifyRepository\OnProductRepository"/>
</type>
<type name="MateuszMesek\DocumentDataApi\Command\GetDocumentNodeValueInterface">
<type name="MateuszMesek\DocumentDataApi\Model\Command\GetDocumentNodeValueInterface">
<plugin name="MateuszMesek_DocumentDataCatalogProduct" type="MateuszMesek\DocumentDataCatalogProduct\Plugin\ModifyRepository\GetDocumentNodeValue"/>
</type>
</config>
3 changes: 3 additions & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
<module name="MateuszMesek_DocumentDataCatalogProduct">
<sequence>
<module name="Magento_Catalog"/>
<module name="Magento_Eav"/>
<module name="Magento_Store"/>
<module name="MateuszMesek_DocumentData"/>
<module name="MateuszMesek_DocumentDataEav"/>
</sequence>
</module>
</config>
Loading

0 comments on commit 6b846e8

Please sign in to comment.