Skip to content

Commit

Permalink
Refactored pushing items to prewarm queue to seperate notifier class,…
Browse files Browse the repository at this point in the history
… added plugin for mass update attributes call
  • Loading branch information
peterjaap committed Sep 27, 2018
1 parent d383cf3 commit ea27560
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 40 deletions.
68 changes: 68 additions & 0 deletions Model/PublisherNotifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Elgentos\LargeConfigProducts\Model;

use Magento\Catalog\Model\ProductFactory;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable as ConfigurableResourceModel;
use Rcason\Mq\Api\PublisherInterface;
use Magento\Framework\Module\Manager as ModuleManager;

class PublisherNotifier {

/**
* @var PublisherInterface
*/
private $publisher;
/**
* @var ConfigurableResourceModel
*/
private $configurableResourceModel;
/**
* @var ModuleManager
*/
private $moduleManager;

/**
* @var ProductFactory
*/
private $productFactory;

/**
* @param PublisherInterface $publisher
* @param ConfigurableResourceModel $configurableResourceModel
* @param ModuleManager $moduleManager
* @param ProductFactory $productFactory
*/
public function __construct(
PublisherInterface $publisher,
ConfigurableResourceModel $configurableResourceModel,
ModuleManager $moduleManager,
ProductFactory $productFactory
) {
$this->publisher = $publisher;
$this->configurableResourceModel = $configurableResourceModel;
$this->moduleManager = $moduleManager;
$this->productFactory = $productFactory;
}

/**
* @param $productIds array
*/
public function notify(array $productIds) {
if ($this->moduleManager->isEnabled('Rcason_Mq')) {
foreach ($productIds as $productId) {
$product = $this->productFactory->create()->load($productId);
if ($product->getTypeId() == Configurable::TYPE_CODE) {
$this->publisher->publish('lcp.product.prewarm', $productId);
} elseif ($product->getTypeId() == 'simple') {
$parentIds = $this->configurableResourceModel->getParentIdsByChild($productId);
foreach ($parentIds as $parentId) {
$this->publisher->publish('lcp.product.prewarm', $parentId);
}
}
}
}
}

}
45 changes: 6 additions & 39 deletions Observer/ProductSaveAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,17 @@

namespace Elgentos\LargeConfigProducts\Observer;

use Magento\ConfigurableProduct\Model\Product\Type\Configurable;

use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable as ConfigurableResourceModel;
use Elgentos\LargeConfigProducts\Model\PublisherNotifier;
use Magento\Framework\Event\ObserverInterface;
use Rcason\Mq\Api\PublisherInterface;
use Magento\Framework\Module\Manager as ModuleManager;

class ProductSaveAfter implements ObserverInterface
{
/**
* @var PublisherInterface
*/
private $publisher;
/**
* @var ConfigurableResourceModel
*/
private $configurableResourceModel;
/**
* @var ModuleManager
* ProductSaveAfter constructor.
* @param PublisherNotifier $publisherNotifier
*/
private $moduleManager;

/**
* @param PublisherInterface $publisher
* @param ConfigurableResourceModel $configurableResourceModel
* @param ModuleManager $moduleManager
*/
public function __construct(
PublisherInterface $publisher,
ConfigurableResourceModel $configurableResourceModel,
ModuleManager $moduleManager
) {
$this->publisher = $publisher;
$this->configurableResourceModel = $configurableResourceModel;
$this->moduleManager = $moduleManager;
public function __construct(PublisherNotifier $publisherNotifier) {
$this->publisherNotifier = $publisherNotifier;
}

/**
Expand All @@ -45,15 +21,6 @@ public function __construct(
public function execute(
\Magento\Framework\Event\Observer $observer
) {
if ($this->moduleManager->isEnabled('Rcason_Mq')) {
if ($observer->getProduct()->getTypeId() == Configurable::TYPE_CODE) {
$this->publisher->publish('lcp.product.prewarm', $observer->getProduct()->getId());
} elseif ($observer->getProduct()->getTypeId() == 'simple') {
$parentIds = $this->configurableResourceModel->getParentIdsByChild($observer->getProduct()->getId());
foreach ($parentIds as $parentId) {
$this->publisher->publish('lcp.product.prewarm', $parentId);
}
}
}
$this->publisherNotifier->notify([$observer->getProduct()->getId()]);
}
}
38 changes: 38 additions & 0 deletions Plugin/Product/Action/AfterUpdateAttributesPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Elgentos\LargeConfigProducts\Plugin\Product\Action;

use Elgentos\LargeConfigProducts\Model\PublisherNotifier;
use Magento\Catalog\Model\Product\Action as ProductAction;

class AfterUpdateAttributesPlugin {

/**
* AfterUpdateAttributesPlugin constructor.
* @param PublisherNotifier $publisherNotifier
*/
public function __construct(PublisherNotifier $publisherNotifier) {
$this->publisherNotifier = $publisherNotifier;
}

/**
* @param ProductAction $subject
* @param ProductAction $action
* @param $productIds
* @param $attrData
* @param $storeId
* @return ProductAction
*/
public function afterUpdateAttributes(
ProductAction $subject,
ProductAction $action,
$productIds,
$attrData,
$storeId
) {
$this->publisherNotifier->notify($productIds);

return $action;
}

}
7 changes: 6 additions & 1 deletion etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
<plugin sortOrder="1" name="elgentosLargeConfigProductsLowestPriceOptionsProvider"
type="Elgentos\LargeConfigProducts\Plugin\Pricing\Price\LowestPriceOptionsProviderPlugin"/>
</type>

<type name="Magento\Catalog\Model\Product\Action">
<plugin sortOrder="1" name="elgentosLargeConfigProductsAfterUpdateAttributes"
type="Elgentos\LargeConfigProducts\Plugin\Product\Action\AfterUpdateAttributesPlugin" />
</type>
<preference for="Magento\Framework\View\TemplateEngine\Xhtml\Template"
type="Elgentos\LargeConfigProducts\Model\View\TemplateEngine\Xhtml\Template"></preference>
type="Elgentos\LargeConfigProducts\Model\View\TemplateEngine\Xhtml\Template" />
</config>

0 comments on commit ea27560

Please sign in to comment.