diff --git a/Block/Adminhtml/System/Config/Fieldset/Hint.php b/Block/Adminhtml/System/Config/Fieldset/Hint.php index 98e8f143..78bebbcd 100644 --- a/Block/Adminhtml/System/Config/Fieldset/Hint.php +++ b/Block/Adminhtml/System/Config/Fieldset/Hint.php @@ -29,22 +29,30 @@ class Hint extends \Magento\Backend\Block\Template implements \Magento\Framework * @var \Magento\Backend\Block\Template\Context */ private $_context; + /** + * @var \Ebizmarts\MailChimp\Model\Config\ModuleVersion + */ + private $_moduleVersion; /** + * Hint constructor. * @param \Magento\Backend\Block\Template\Context $context * @param \Magento\Framework\App\ProductMetadataInterface $productMetaData * @param \Ebizmarts\MailChimp\Helper\Data $helper + * @param \Ebizmarts\MailChimp\Model\Config\ModuleVersion $moduleVersion * @param array $data */ public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Framework\App\ProductMetadataInterface $productMetaData, \Ebizmarts\MailChimp\Helper\Data $helper, + \Ebizmarts\MailChimp\Model\Config\ModuleVersion $moduleVersion, array $data = [] ) { parent::__construct($context, $data); $this->_metaData = $productMetaData; $this->_helper = $helper; + $this->_moduleVersion = $moduleVersion; $this->_context = $context; } /** @@ -55,28 +63,10 @@ public function render(\Magento\Framework\Data\Form\Element\AbstractElement $ele { return $this->toHtml(); } - public function getPxParams() - { - - $extension = "MailChimp;{$this->getModuleVersion()}"; - $mageEdition = $this->_metaData->getEdition(); - switch ($mageEdition) { - case 'Community': - $mageEdition = 'CE'; - break; - case 'Enterprise': - $mageEdition = 'EE'; - break; - } - $mageVersion = $this->_metaData->getVersion(); - $mage = "Magento {$mageEdition};{$mageVersion}"; - $hash = md5($extension . '_' . $mage . '_' . $extension); - return "ext=$extension&mage={$mage}&ctrl={$hash}"; - } public function getModuleVersion() { - return $this->_helper->getModuleVersion(); + return $this->_moduleVersion->getModuleVersion('Ebizmarts_MailChimp'); } public function getHasApiKey() { $apikey = $this->_helper->getApiKey($this->_context->getStoreManager()->getStore()->getId()); diff --git a/Model/Config/ModuleVersion.php b/Model/Config/ModuleVersion.php new file mode 100644 index 00000000..f565cc63 --- /dev/null +++ b/Model/Config/ModuleVersion.php @@ -0,0 +1,60 @@ + + * @copyright Ebizmarts (http://ebizmarts.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + */ + +namespace Ebizmarts\MailChimp\Model\Config; + +use Magento\Framework\Component\ComponentRegistrar; +use Magento\Framework\Component\ComponentRegistrarInterface; +use Magento\Framework\Exception\FileSystemException; +use Magento\Framework\Filesystem\Directory\ReadFactory; + +class ModuleVersion +{ + const COMPOSER_FILE_NAME = 'composer.json'; + /** + * @var ComponentRegistrarInterface + */ + private $componentRegistrar; + /** + * @var ReadFactory + */ + private $readFactory; + + /** + * ModuleVersion constructor. + * @param ComponentRegistrarInterface $componentRegistrar + * @param ReadFactory $readFactory + */ + public function __construct(ComponentRegistrarInterface $componentRegistrar, ReadFactory $readFactory) { + $this->componentRegistrar = $componentRegistrar; + $this->readFactory = $readFactory; + } + public function getModuleVersion($moduleName) : string + { + $emptyVersionNumber = ''; + $composerJsonData = null; + try { + $path = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName); + $directoryRead = $this->readFactory->create($path); + $composerJsonData = $directoryRead->readFile(self::COMPOSER_FILE_NAME); + } catch(\LogicException $pathException) { + return $emptyVersionNumber; + } catch(FileSystemException $fsException) { + return $emptyVersionNumber; + } + $jsonData = json_decode($composerJsonData); + if ($jsonData === null) { + return $emptyVersionNumber; + } + return $jsonData->version ?? $emptyVersionNumber; + } +} +