Skip to content

Commit

Permalink
Merge pull request #762 from mailchimp/Issue759-2.3
Browse files Browse the repository at this point in the history
closes #759 for magento 2.3
  • Loading branch information
gonzaloebiz authored Jun 14, 2019
2 parents e459878 + 77ecef9 commit fd6b2a9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 19 deletions.
28 changes: 9 additions & 19 deletions Block/Adminhtml/System/Config/Fieldset/Hint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
/**
Expand All @@ -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());
Expand Down
60 changes: 60 additions & 0 deletions Model/Config/ModuleVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Ebizmarts_MailChimp Magento JS component
*
* @category Ebizmarts
* @package Ebizmarts_MailChimp
* @author Ebizmarts Team <info@ebizmarts.com>
* @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;
}
}

0 comments on commit fd6b2a9

Please sign in to comment.