Skip to content

Commit

Permalink
Merge pull request #30 from MEEVO-Healthcare-GmbH/master
Browse files Browse the repository at this point in the history
Add env.php configuration and introduce Credit_Client Factory
  • Loading branch information
peterjaap committed Dec 13, 2018
2 parents f8759c6 + 03d8755 commit a523fe7
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 61 deletions.
49 changes: 49 additions & 0 deletions Cache/CredisClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);

namespace Elgentos\LargeConfigProducts\Cache;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\DeploymentConfig;

class CredisClientFactory
{
/**
* @var DeploymentConfig
*/
private $deploymentConfig;

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

public function __construct(DeploymentConfig $deploymentConfig, ScopeConfigInterface $scopeConfig)
{
$this->deploymentConfig = $deploymentConfig;
$this->scopeConfig = $scopeConfig;
}

public function create(): \Credis_Client
{
$cacheSetting = $this->deploymentConfig->get('cache');

$timeout = null;
$persistent = '';
if (isset($cacheSetting['frontend']['elgentos_largeconfigproducts']['backend_options'])) {
$backendOptions = $cacheSetting['frontend']['elgentos_largeconfigproducts']['backend_options'];

$server = $backendOptions['server'];
$database = $backendOptions['database'];
$port = $backendOptions['port'];

} else {
$server = $this->scopeConfig->getValue('elgentos_largeconfigproducts/prewarm/redis_host') ?? 'localhost';

$port = $this->scopeConfig->getValue('elgentos_largeconfigproducts/prewarm/redis_port') ?? 6379;
$database = $this->scopeConfig->getValue('elgentos_largeconfigproducts/prewarm/redis_db_index') ?? 4;
}

return new \Credis_Client($server, $port, $timeout, $persistent, $database);
}
}
20 changes: 9 additions & 11 deletions Controller/Fetch/ProductOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Elgentos\LargeConfigProducts\Controller\Fetch;

use Credis_Client;
use Elgentos\LargeConfigProducts\Cache\CredisClientFactory;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\ConfigurableProduct\Block\Product\View\Type\Configurable as ProductTypeConfigurable;
use Magento\Customer\Model\Session as CustomerSession;
Expand Down Expand Up @@ -49,28 +49,26 @@ class ProductOptions extends Action
* @param ProductRepositoryInterface $productRepository
* @param Registry $coreRegistry
*
* @param CredisClientFactory $credisClientFactory
* @param StoreManagerInterface $storeManager
* @param CustomerSession $customerSession
*
* @internal param Product $catalogProduct
*/
public function __construct(
Context $context,
ProductRepositoryInterface $productRepository,
Registry $coreRegistry,
CredisClientFactory $credisClientFactory,
StoreManagerInterface $storeManager,
CustomerSession $customerSession,
ScopeConfigInterface $scopeConfig
CustomerSession $customerSession
) {
parent::__construct($context);
$this->productRepository = $productRepository;
$this->_coreRegistry = $coreRegistry;
$this->storeManager = $storeManager;
$this->customerSession = $customerSession;
$this->credis = new Credis_Client(
$scopeConfig->getValue('elgentos_largeconfigproducts/prewarm/redis_host') ?? 'localhost',
$scopeConfig->getValue('elgentos_largeconfigproducts/prewarm/redis_port') ?? 6379,
null,
'',
$scopeConfig->getValue('elgentos_largeconfigproducts/prewarm/redis_db_index') ?? 4
);
$this->credis = $credisClientFactory->create();
}

/**
Expand Down Expand Up @@ -100,7 +98,7 @@ public function getProductOptionInfo($productId)
$customerGroupId = $this->customerSession->getCustomerGroupId();

$cacheKey = 'LCP_PRODUCT_INFO_' . $storeId . '_' . $productId . '_' . $customerGroupId;

if ($this->credis->exists($cacheKey)) {
return $this->credis->get($cacheKey);
}
Expand Down
35 changes: 13 additions & 22 deletions Model/Prewarmer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace Elgentos\LargeConfigProducts\Model;

use Credis_Client;
use Elgentos\LargeConfigProducts\Cache\CredisClientFactory;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\ConfigurableProduct\Block\Product\View\Type\Configurable as ProductTypeConfigurable;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\Area;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\BlockFactory;
use Magento\Store\Model\App\Emulation;
Expand All @@ -25,10 +24,6 @@ class Prewarmer {
* @var Emulation
*/
private $emulation;
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;
/**
* @var Registry
*/
Expand All @@ -42,37 +37,33 @@ class Prewarmer {

/**
* PrewarmerCommand constructor.
*
* @param ProductRepositoryInterface $productRepository
* @param StoreManagerInterface $storeManager
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param Emulation $emulation
* @param CredisClientFactory $credisClientFactory
* @param Registry $coreRegistry
* @param BlockFactory $blockFactory
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
ProductRepositoryInterface $productRepository,
StoreManagerInterface $storeManager,
SearchCriteriaBuilder $searchCriteriaBuilder,
Emulation $emulation,
CredisClientFactory $credisClientFactory,
Registry $coreRegistry,
BlockFactory $blockFactory,
ScopeConfigInterface $scopeConfig
BlockFactory $blockFactory
) {
$this->productRepository = $productRepository;
$this->storeManager = $storeManager;
$this->productRepository = $productRepository;
$this->storeManager = $storeManager;
$this->productRepository = $productRepository;
$this->credis = $credisClientFactory->create();
$this->storeManager = $storeManager;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->emulation = $emulation;
$this->coreRegistry = $coreRegistry;
$this->blockFactory = $blockFactory;

$this->credis = new Credis_Client(
$scopeConfig->getValue('elgentos_largeconfigproducts/prewarm/redis_host') ?? 'localhost',
$scopeConfig->getValue('elgentos_largeconfigproducts/prewarm/redis_port') ?? 6379,
null,
'',
$scopeConfig->getValue('elgentos_largeconfigproducts/prewarm/redis_db_index') ?? 4
);
$this->emulation = $emulation;
$this->coreRegistry = $coreRegistry;
$this->blockFactory = $blockFactory;
}

public function prewarm($productIdsToWarm, $storeCodesToWarm, $force)
Expand Down
26 changes: 15 additions & 11 deletions Plugin/Model/AttributeOptionProviderPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,37 @@
* Date: 21-2-18
* Time: 13:53
*/

namespace Elgentos\LargeConfigProducts\Plugin\Model;

use Elgentos\LargeConfigProducts\Cache\CredisClientFactory;
use Elgentos\LargeConfigProducts\Model\Prewarmer;
use Magento\Catalog\Model\ProductFactory;
use Magento\Framework\App\DeploymentConfig;
use Magento\Store\Model\StoreManagerInterface;
use Credis_Client;
class AttributeOptionProviderPlugin
{
/** @var StoreManagerInterface */
protected $storeManager;

/** @var \Credis_Client|null */
protected $credis;
/**
* AttributeOptionProviderPlugin constructor.
*
* @param StoreManagerInterface $storeManager
* @param CredisClientFactory $credisClientFactory
*/
public function __construct(
StoreManagerInterface $storeManager,
DeploymentConfig $deploymentConfig
CredisClientFactory $credisClientFactory
) {
$cacheSetting = $deploymentConfig->get('cache');
if (isset($cacheSetting['frontend']['default']['backend_options']['server'])) {
$this->credis = new Credis_Client($cacheSetting['frontend']['default']['backend_options']['server']);
$this->credis->select(4);
}
$this->credis = $credisClientFactory->create();
$this->storeManager = $storeManager;
}
public function beforeGetAttributeOptions(\Magento\ConfigurableProduct\Model\AttributeOptionProvider $subject, \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $superAttribute, $productId)
{
public function beforeGetAttributeOptions(
\Magento\ConfigurableProduct\Model\AttributeOptionProvider $subject,
\Magento\Eav\Model\Entity\Attribute\AbstractAttribute $superAttribute,
$productId
) {
/**
* The currentStoreId that is being set in the emulation in PrewarmerCommand is somehow lost in the call
* stack. This plugin uses the store ID found in the Redis DB to re-set the current store so the translated
Expand Down
28 changes: 11 additions & 17 deletions Pricing/Price/LowestPriceOptionsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@

namespace Elgentos\LargeConfigProducts\Pricing\Price;

use Credis_Client;
use Elgentos\LargeConfigProducts\Cache\CredisClientFactory;
use Elgentos\LargeConfigProducts\Model\Prewarmer;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\ResourceModel\Product\LinkedProductSelectBuilderInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Catalog\Model\ResourceModel\Product\LinkedProductSelectBuilderInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Elgentos\LargeConfigProducts\Model\Prewarmer;
use Magento\Framework\App\ResourceConnection;
use Magento\Store\Model\StoreManagerInterface;
use Credis_Client;

class LowestPriceOptionsProvider extends \Magento\ConfigurableProduct\Pricing\Price\LowestPriceOptionsProvider
{
Expand All @@ -28,6 +29,7 @@ class LowestPriceOptionsProvider extends \Magento\ConfigurableProduct\Pricing\Pr
* @var Credis_Client
*/
protected $credis;

/**
* @var ResourceConnection
*/
Expand Down Expand Up @@ -62,23 +64,15 @@ public function __construct(
LinkedProductSelectBuilderInterface $linkedProductSelectBuilder,
CollectionFactory $collectionFactory,
StoreManagerInterface $storeManager,
ScopeConfigInterface $scopeConfig
CredisClientFactory $credisClientFactory
) {
$this->resource = $resourceConnection;
$this->resource = $resourceConnection;
$this->linkedProductSelectBuilder = $linkedProductSelectBuilder;
$this->collectionFactory = $collectionFactory;

$this->credis = new Credis_Client(
$scopeConfig->getValue('elgentos_largeconfigproducts/prewarm/redis_host') ?? 'localhost',
$scopeConfig->getValue('elgentos_largeconfigproducts/prewarm/redis_port') ?? 6379,
null,
'',
$scopeConfig->getValue('elgentos_largeconfigproducts/prewarm/redis_db_index') ?? 4
);
$this->storeManager = $storeManager;
$this->collectionFactory = $collectionFactory;
$this->credis = $credisClientFactory->create();
$this->storeManager = $storeManager;
}


/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit a523fe7

Please sign in to comment.