Skip to content

Commit

Permalink
Correct isArea function
Browse files Browse the repository at this point in the history
  • Loading branch information
haitv282 committed Jun 4, 2018
1 parent 834707e commit c254c43
Showing 1 changed file with 47 additions and 68 deletions.
115 changes: 47 additions & 68 deletions Helper/AbstractData.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,9 @@ class AbstractData extends AbstractHelper
protected $backendConfig;

/**
* @var bool
* @var array
*/
protected $isAdminArea;

/**
* @var bool
*/
protected $isFrontendArea;
protected $isArea = [];

/**
* AbstractData constructor.
Expand Down Expand Up @@ -129,7 +124,7 @@ public function getModuleConfig($field = '', $storeId = null)
*/
public function getConfigValue($field, $scopeValue = null, $scopeType = ScopeInterface::SCOPE_STORE)
{
if (!$this->isFrontend() && is_null($scopeValue)) {
if (!$this->isArea() && is_null($scopeValue)) {
/** @var \Magento\Backend\App\Config $backendConfig */
if (!$this->backendConfig) {
$this->backendConfig = $this->objectManager->get('Magento\Backend\App\ConfigInterface');
Expand Down Expand Up @@ -176,6 +171,18 @@ public function getCurrentUrl()
return $model->getCurrentUrl();
}

/**
* @param $ver
* @return mixed
*/
public function versionCompare($ver)
{
$productMetadata = $this->objectManager->get(ProductMetadataInterface::class);
$version = $productMetadata->getVersion(); //will return the magento version

return version_compare($version, $ver, '>=');
}

/**
* @param $data
* @return string
Expand All @@ -191,15 +198,17 @@ public function serialize($data)
}

/**
* @param $ver
* @param $string
* @return mixed
* @throws \Zend_Serializer_Exception
*/
public function versionCompare($ver)
public function unserialize($string)
{
$productMetadata = $this->objectManager->get(ProductMetadataInterface::class);
$version = $productMetadata->getVersion(); //will return the magento version
if ($this->versionCompare('2.2.0')) {
return self::jsonDecode($string);
}

return version_compare($version, $ver, '>=');
return $this->getSerializeClass()->unserialize($string);
}

/**
Expand All @@ -219,36 +228,6 @@ public static function jsonEncode($valueToEncode)
return $encodeValue;
}

/**
* @return \Magento\Framework\Json\Helper\Data|mixed
*/
public static function getJsonHelper()
{
return ObjectManager::getInstance()->get(JsonHelper::class);
}

/**
* @return \Zend_Serializer_Adapter_PhpSerialize|mixed
*/
protected function getSerializeClass()
{
return $this->objectManager->get(\Zend_Serializer_Adapter_PhpSerialize::class);
}

/**
* @param $string
* @return mixed
* @throws \Zend_Serializer_Exception
*/
public function unserialize($string)
{
if ($this->versionCompare('2.2.0')) {
return self::jsonDecode($string);
}

return $this->getSerializeClass()->unserialize($string);
}

/**
* Decodes the given $encodedValue string which is
* encoded in the JSON format
Expand All @@ -272,45 +251,29 @@ public static function jsonDecode($encodedValue)
*
* @return bool
*/
public function isFrontend()
public function isAdmin()
{
if (!isset($this->isFrontendArea)) {
/** @var \Magento\Framework\App\State $state */
$state = $this->objectManager->get('Magento\Framework\App\State');

try {
$areaCode = $state->getAreaCode();

$this->isFrontendArea = ($areaCode == Area::AREA_FRONTEND);
} catch (\Exception $e) {
$this->isFrontendArea = false;
}
}

return $this->isFrontendArea;
return $this->isArea(Area::AREA_ADMINHTML);
}

/**
* Is Admin Store
*
* @return bool
* @param string $area
* @return mixed
*/
public function isAdmin()
public function isArea($area = Area::AREA_FRONTEND)
{
if (!isset($this->isAdminArea)) {
if (!isset($this->isArea[$area])) {
/** @var \Magento\Framework\App\State $state */
$state = $this->objectManager->get('Magento\Framework\App\State');

try {
$areaCode = $state->getAreaCode();

$this->isAdminArea = ($areaCode == Area::AREA_ADMINHTML);
$this->isArea[$area] = ($state->getAreaCode() == $area);
} catch (\Exception $e) {
$this->isAdminArea = false;
$this->isArea[$area] = false;
}
}

return $this->isAdminArea;
return $this->isArea[$area];
}

/**
Expand All @@ -331,4 +294,20 @@ public function getObject($path)
{
return $this->objectManager->get($path);
}

/**
* @return \Magento\Framework\Json\Helper\Data|mixed
*/
public static function getJsonHelper()
{
return ObjectManager::getInstance()->get(JsonHelper::class);
}

/**
* @return \Zend_Serializer_Adapter_PhpSerialize|mixed
*/
protected function getSerializeClass()
{
return $this->objectManager->get(\Zend_Serializer_Adapter_PhpSerialize::class);
}
}

0 comments on commit c254c43

Please sign in to comment.