Skip to content

Commit

Permalink
[MID-21] Made getAttributeValue method more Magento-friendly. Removed…
Browse files Browse the repository at this point in the history
…e useless class fields
  • Loading branch information
Mavlyan committed Sep 13, 2017
1 parent 326e154 commit 163bfbb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 79 deletions.
94 changes: 37 additions & 57 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,26 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
/**@var \Magento\Catalog\Model\Product */
protected $_tempProduct = null;

/**@var \Magento\Eav\Model\Config */
protected $_eavConfig;

/**@var \Magento\Catalog\Model\ResourceModel\Product */
protected $_resourceProduct;

/**@var \Magento\Store\Model\StoreManagerInterface */
protected $_storeManager;

/**@var \Magento\Catalog\Api\ProductRepositoryInterface */
protected $_productRepository;

public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Mygento\Base\Model\Logger\LoggerFactory $loggerFactory,
\Mygento\Base\Model\Logger\HandlerFactory $handlerFactory,
\Magento\Framework\Encryption\Encryptor $encryptor,
\Magento\Framework\HTTP\Client\Curl $curl,
\Magento\Eav\Model\Config $eavConfig,
\Magento\Catalog\Model\ResourceModel\Product $resourceProduct,
\Magento\Store\Model\StoreManagerInterface $storeManager
\Magento\Catalog\Model\ProductRepository $productRepository
) {
parent::__construct($context);
$this->_loggerFactory = $loggerFactory;
$this->_handlerFactory = $handlerFactory;
$this->_encryptor = $encryptor;
$this->_curlClient = $curl;
$this->_eavConfig = $eavConfig;
$this->_resourceProduct = $resourceProduct;
$this->_storeManager = $storeManager;

$this->_loggerFactory = $loggerFactory;
$this->_handlerFactory = $handlerFactory;
$this->_encryptor = $encryptor;
$this->_curlClient = $curl;
$this->_productRepository = $productRepository;

$this->_logger = $this->_loggerFactory->create(['name' => $this->_code]);
$handler = $this->_handlerFactory->create(['name' => $this->_code]);
$handler = $this->_handlerFactory->create(['name' => $this->_code]);
$this->_logger->setHandlers([$handler]);
}

Expand Down Expand Up @@ -109,7 +99,12 @@ public function getConfig($configPath)
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}


public function getProduct($productId)
{
return $this->_productRepository->getById($productId);
}

/**
*
* @param type $url
Expand Down Expand Up @@ -190,43 +185,28 @@ public function getCode()
{
return $this->_code;
}

public function getAttributeValue($param, $productId, $prefix = '')

/**Fetch attribute code from $pathToParam and then get it from product
* @param $pathToParam config path like module/general/param
* @param $productId
*
* @return mixed attribute value
*/
public function getAttrValueByParam($pathToParam, $productId)
{
$attributeCode = $this->getConfig($prefix . $param);

//$this->addLog('attr for ' . $param . ' -> ' . $attributeCode);

if ('0' != $attributeCode && 0 !== $attributeCode) {
$entityType = $this->_resourceProduct->getEntityType();
$attribute = $this->_eavConfig->getAttribute($entityType, $attributeCode);
$attributeMode = $attribute->getFrontendInput();
if ('select' == $attributeMode) {
//need to use product model
//TODO: Avoid ObjectManager
if (!$this->_tempProduct) {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$this->_tempProduct = $objectManager->get('Magento\Catalog\Model\Product')
->load($productId);
}
$product = $this->_tempProduct;
$value = $product->getAttributeText($attributeCode);
} else {
//just raw DB data
$value = $this->_resourceProduct->getAttributeRawValue(
$productId,
$attributeCode,
$this->_storeManager->getStore()
);

if (is_array($value) && isset($value[$attributeCode])) {
$value = $value[$attributeCode];
}
}
} else {
$value = $this->getConfig($prefix . $param . '_default');
$attributeCode = $this->getConfig($pathToParam);
if ('0' == $attributeCode || 0 === $attributeCode) {
return $this->getConfig($pathToParam . '_default');
}


return $this->getAttributeValue($attributeCode, $productId);
}

public function getAttributeValue($attributeCode, $productId)
{
$product = $this->getProduct($productId);
$value = $product->getAttributeText($attributeCode) ?: $product->getData($attributeCode);

return $value;
}
}
31 changes: 11 additions & 20 deletions Helper/Discount.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,15 @@
*/
namespace Mygento\Base\Helper;

//TODO: FIgure out - do we need to extend this parent helper?
/**
* Module helper
*/
class Discount extends \Mygento\Base\Helper\Data
{

/** @var \Magento\Catalog\Model\ResourceModel\Product */
protected $_productResource;

/** @var \Magento\Eav\Model\Config */
protected $_eavConfig;

/** @var \Magento\Store\Model\StoreManagerInterface */
protected $_storeManager;
/**@var \Magento\Catalog\Model\ProductRepository */
protected $_productRepository;

/**
* Constructor
Expand All @@ -40,25 +35,20 @@ public function __construct(
\Mygento\Base\Model\Logger\HandlerFactory $handlerFactory,
\Magento\Framework\Encryption\Encryptor $encryptor,
\Magento\Framework\HTTP\Client\Curl $curl,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Catalog\Model\ResourceModel\Product $productResource,
\Magento\Eav\Model\Config $eavConfig
) {

\Magento\Catalog\Model\ProductRepository $productRepository
)
{

parent::__construct(
$context,
$loggerFactory,
$handlerFactory,
$encryptor,
$curl,
$eavConfig,
$productResource,
$storeManager
$productRepository
);

$this->_productResource = $productResource;
$this->_eavConfig = $eavConfig;
$this->_storeManager = $storeManager;
$this->_productRepository = $productRepository;
}

/**
Expand Down Expand Up @@ -280,6 +270,7 @@ protected function addTaxValue($taxAttributeCode, $entity, $item)

$store = $storeId ? $this->_storeManager->getStore($storeId) : $this->_storeManager->getStore();

//TODO: Use parent helper here
$taxValue = $this->_productResource->getAttributeRawValue(
$item->getProductId(),
$taxAttributeCode,
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mygento/base",
"type": "magento2-module",
"version": "2.0.8",
"version": "2.0.9",
"license": "OSL-3.0",
"homepage": "https://github.com/mygento/base",
"description": "Mygento Base",
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Mygento_Base" setup_version="2.0.8"></module>
<module name="Mygento_Base" setup_version="2.0.9"></module>
</config>

0 comments on commit 163bfbb

Please sign in to comment.