AbstractData.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /**
  3. * Mageplaza
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Mageplaza.com license that is
  8. * available through the world-wide-web at this URL:
  9. * https://www.mageplaza.com/LICENSE.txt
  10. *
  11. * DISCLAIMER
  12. *
  13. * Do not edit or add to this file if you wish to upgrade this extension to newer
  14. * version in the future.
  15. *
  16. * @category Mageplaza
  17. * @package Mageplaza_Core
  18. * @copyright Copyright (c) 2016-2018 Mageplaza (http://www.mageplaza.com/)
  19. * @license https://www.mageplaza.com/LICENSE.txt
  20. */
  21. namespace Mageplaza\Core\Helper;
  22. use Magento\Framework\App\Area;
  23. use Magento\Framework\App\Helper\AbstractHelper;
  24. use Magento\Framework\App\Helper\Context;
  25. use Magento\Framework\App\ObjectManager;
  26. use Magento\Framework\App\ProductMetadataInterface;
  27. use Magento\Framework\Json\Helper\Data as JsonHelper;
  28. use Magento\Framework\ObjectManagerInterface;
  29. use Magento\Framework\UrlInterface;
  30. use Magento\Store\Model\ScopeInterface;
  31. use Magento\Store\Model\StoreManagerInterface;
  32. /**
  33. * Class AbstractData
  34. * @package Mageplaza\Core\Helper
  35. */
  36. class AbstractData extends AbstractHelper
  37. {
  38. const CONFIG_MODULE_PATH = 'mageplaza';
  39. /**
  40. * @type array
  41. */
  42. protected $_data = [];
  43. /**
  44. * @type \Magento\Store\Model\StoreManagerInterface
  45. */
  46. protected $storeManager;
  47. /**
  48. * @type \Magento\Framework\ObjectManagerInterface
  49. */
  50. protected $objectManager;
  51. /**
  52. * @param \Magento\Framework\App\Helper\Context $context
  53. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  54. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  55. */
  56. public function __construct(
  57. Context $context,
  58. ObjectManagerInterface $objectManager,
  59. StoreManagerInterface $storeManager
  60. )
  61. {
  62. $this->objectManager = $objectManager;
  63. $this->storeManager = $storeManager;
  64. parent::__construct($context);
  65. }
  66. /**
  67. * @param null $storeId
  68. * @return bool
  69. */
  70. public function isEnabled($storeId = null)
  71. {
  72. return $this->getConfigGeneral('enabled', $storeId);
  73. }
  74. /**
  75. * @param string $code
  76. * @param null $storeId
  77. * @return mixed
  78. */
  79. public function getConfigGeneral($code = '', $storeId = null)
  80. {
  81. $code = ($code !== '') ? '/' . $code : '';
  82. return $this->getConfigValue(static::CONFIG_MODULE_PATH . '/general' . $code, $storeId);
  83. }
  84. /**
  85. * @param string $field
  86. * @param null $storeId
  87. * @return mixed
  88. */
  89. public function getModuleConfig($field = '', $storeId = null)
  90. {
  91. $field = ($field !== '') ? '/' . $field : '';
  92. return $this->getConfigValue(static::CONFIG_MODULE_PATH . $field, $storeId);
  93. }
  94. /**
  95. * @param $field
  96. * @param null $storeId
  97. * @return mixed
  98. */
  99. public function getConfigValue($field, $storeId = null)
  100. {
  101. return $this->scopeConfig->getValue(
  102. $field,
  103. ScopeInterface::SCOPE_STORE,
  104. $storeId
  105. );
  106. }
  107. /**
  108. * @param $name
  109. * @return null
  110. */
  111. public function getData($name)
  112. {
  113. if (array_key_exists($name, $this->_data)) {
  114. return $this->_data[$name];
  115. }
  116. return null;
  117. }
  118. /**
  119. * @param $name
  120. * @param $value
  121. * @return $this
  122. */
  123. public function setData($name, $value)
  124. {
  125. $this->_data[$name] = $value;
  126. return $this;
  127. }
  128. /**
  129. * @return mixed
  130. */
  131. public function getCurrentUrl()
  132. {
  133. $model = $this->objectManager->get(UrlInterface::class);
  134. return $model->getCurrentUrl();
  135. }
  136. /**
  137. * @param $data
  138. * @return string
  139. * @throws \Zend_Serializer_Exception
  140. */
  141. public function serialize($data)
  142. {
  143. if ($this->versionCompare('2.2.0')) {
  144. return self::jsonEncode($data);
  145. }
  146. return $this->getSerializeClass()->serialize($data);
  147. }
  148. /**
  149. * @param $ver
  150. * @return mixed
  151. */
  152. public function versionCompare($ver)
  153. {
  154. $productMetadata = $this->objectManager->get(ProductMetadataInterface::class);
  155. $version = $productMetadata->getVersion(); //will return the magento version
  156. return version_compare($version, $ver, '>=');
  157. }
  158. /**
  159. * Encode the mixed $valueToEncode into the JSON format
  160. *
  161. * @param mixed $valueToEncode
  162. * @return string
  163. */
  164. public static function jsonEncode($valueToEncode)
  165. {
  166. try {
  167. $encodeValue = self::getJsonHelper()->jsonEncode($valueToEncode);
  168. } catch (\Exception $e) {
  169. $encodeValue = '{}';
  170. }
  171. return $encodeValue;
  172. }
  173. /**
  174. * @return \Magento\Framework\Json\Helper\Data|mixed
  175. */
  176. public static function getJsonHelper()
  177. {
  178. return ObjectManager::getInstance()->get(JsonHelper::class);
  179. }
  180. /**
  181. * @return \Zend_Serializer_Adapter_PhpSerialize|mixed
  182. */
  183. protected function getSerializeClass()
  184. {
  185. return $this->objectManager->get(\Zend_Serializer_Adapter_PhpSerialize::class);
  186. }
  187. /**
  188. * @param $string
  189. * @return mixed
  190. * @throws \Zend_Serializer_Exception
  191. */
  192. public function unserialize($string)
  193. {
  194. if ($this->versionCompare('2.2.0')) {
  195. return self::jsonDecode($string);
  196. }
  197. return $this->getSerializeClass()->unserialize($string);
  198. }
  199. /**
  200. * Decodes the given $encodedValue string which is
  201. * encoded in the JSON format
  202. *
  203. * @param string $encodedValue
  204. * @return mixed
  205. */
  206. public static function jsonDecode($encodedValue)
  207. {
  208. try {
  209. $decodeValue = self::getJsonHelper()->jsonDecode($encodedValue);
  210. } catch (\Exception $e) {
  211. $decodeValue = [];
  212. }
  213. return $decodeValue;
  214. }
  215. /**
  216. * Is Admin Store
  217. *
  218. * @return bool
  219. */
  220. public function isAdmin()
  221. {
  222. /** @var \Magento\Framework\App\State $state */
  223. $state = $this->objectManager->get('Magento\Framework\App\State');
  224. try {
  225. $areaCode = $state->getAreaCode();
  226. } catch (\Exception $e) {
  227. return false;
  228. }
  229. return $areaCode == Area::AREA_ADMINHTML;
  230. }
  231. /**
  232. * @param $path
  233. * @param array $arguments
  234. * @return mixed
  235. */
  236. public function createObject($path, $arguments = [])
  237. {
  238. return $this->objectManager->create($path, $arguments);
  239. }
  240. /**
  241. * @param $path
  242. * @return mixed
  243. */
  244. public function getObject($path)
  245. {
  246. return $this->objectManager->get($path);
  247. }
  248. }