Skip to content

Commit

Permalink
PSR-2 refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
srenon committed Jan 1, 2018
1 parent 7e1156a commit 7de216e
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 115 deletions.
14 changes: 5 additions & 9 deletions Block/Adminhtml/System/Config/Form/Composer/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public function render(\Magento\Framework\Data\Form\Element\AbstractElement $ele
return parent::render($element);
}


/**
* Return element html
*
Expand All @@ -68,13 +67,13 @@ protected function _getElementHtml(\Magento\Framework\Data\Form\Element\Abstract
return 'v' . $this->getVersion();
}


/**
* Get Module version number
*
* @return string
*/
public function getVersion(){
public function getVersion()
{
return $this->getComposerVersion($this->getModuleName());
}

Expand All @@ -101,21 +100,18 @@ public function getComposerVersion($moduleName)
$moduleName
);

try{
try {
$directoryRead = $this->readFactory->create($path);
$composerJsonData = $directoryRead->readFile('composer.json');

if($composerJsonData){
if ($composerJsonData) {
$data = json_decode($composerJsonData);
return !empty($data->version) ? $data->version : __('Unknown');
}

}
catch (\Exception $e){
} catch (\Exception $e) {
//
}

return 'Unknown';

}
}
5 changes: 2 additions & 3 deletions Block/Adminhtml/System/Config/Form/Module/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public function render(\Magento\Framework\Data\Form\Element\AbstractElement $ele
return parent::render($element);
}


/**
* Return element html
*
Expand All @@ -54,13 +53,13 @@ protected function _getElementHtml(\Magento\Framework\Data\Form\Element\Abstract
return 'v' . $this->getVersion();
}


/**
* Get Module version number
*
* @return string
*/
public function getVersion(){
public function getVersion()
{
$moduleInfo = $this->_moduleList->getOne($this->getModuleName());
return $moduleInfo['setup_version'];
}
Expand Down
4 changes: 1 addition & 3 deletions Block/Data/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Magento\Framework\View\Element\Template\Context;
use MagePal\GoogleTagManager\Model\Cart as GtmCartModel;

/**
* Block : Datalayer for cart view page
*
Expand All @@ -24,7 +25,6 @@ class Cart extends \Magento\Framework\View\Element\Template
*/
protected $gtmCart;


/**
* @param Context $context
* @param GtmCartModel $gtmCart
Expand All @@ -36,7 +36,6 @@ public function __construct(
$this->gtmCart = $gtmCart;
}


/**
* Add product data to datalayer
*
Expand All @@ -51,5 +50,4 @@ protected function _prepareLayout()

return $this;
}

}
13 changes: 6 additions & 7 deletions Block/Data/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,23 @@ protected function _prepareLayout()
/** @var $tm \MagePal\GoogleTagManager\Block\DataLayer */
$tm = $this->getParentBlock();

/** @var $product \Magento\Catalog\Api\Data\ProductInterface */
/** @var $product \Magento\Catalog\Api\Data\ProductInterface */
$category = $this->getCurrentCategory();

if(!$category){
if (!$category) {
return;
}

$tm->addVariable(
'list',
'list',
'category'
);


$titleArray = [];
$breadCrumbs = $this->_catalogData->getBreadcrumbPath();

foreach($breadCrumbs as $breadCrumb){
$titleArray[] = $breadCrumb['label'];
foreach ($breadCrumbs as $breadCrumb) {
$titleArray[] = $breadCrumb['label'];
}

$tm->addVariable(
Expand Down
4 changes: 1 addition & 3 deletions Block/Data/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ class Customer extends \Magento\Framework\View\Element\Template
*/
protected $gtmCustomer;


/**
* @param Context $context
* @param GtmCustomerModel $gtmCustomer
*/
public function __construct(
Context $context,
GtmCustomerModel $gtmCustomer
)
{
) {
$this->gtmCustomer = $gtmCustomer;
}
/**
Expand Down
10 changes: 2 additions & 8 deletions Block/Data/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@ public function __construct(
) {
parent::__construct($context, $data);
$this->orderDataArray = $orderDataArray;

}



/**
* Render information about specified orders and their items
*
Expand All @@ -54,15 +51,12 @@ public function addOrderLayer()
{
$transactions = $this->orderDataArray->setOrderIds($this->getOrderIds())->getOrderLayer();

if(!empty($transactions)){
if (!empty($transactions)) {
/** @var $tm \MagePal\GoogleTagManager\Block\DataLayer */
$tm = $this->getParentBlock();
foreach($transactions as $order){
foreach ($transactions as $order) {
$tm->addAdditionalVariable($order);
}

}

}

}
14 changes: 6 additions & 8 deletions Block/Data/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
namespace MagePal\GoogleTagManager\Block\Data;

use Magento\Catalog\Block\Product\Context;
use \Magento\Catalog\Model\Product\Type;
use Magento\Catalog\Helper\Data;
use Magento\Catalog\Model\Product\Type;

/**
* Block : Product for catalog product view page
Expand Down Expand Up @@ -39,7 +39,6 @@ public function __construct(
parent::__construct($context, $data);
}


/**
* Add product data to datalayer
*
Expand All @@ -51,15 +50,14 @@ protected function _prepareLayout()
$tm = $this->getParentBlock();

$tm->addVariable(
'list',
'list',
'detail'
);
/** @var $product \Magento\Catalog\Api\Data\ProductInterface */

/** @var $product \Magento\Catalog\Api\Data\ProductInterface */
$product = $this->getProduct();

if($product) {

if ($product) {
$tm->addVariable(
'product',
[
Expand All @@ -72,7 +70,7 @@ protected function _prepareLayout()
]
);
}

return $this;
}

Expand Down
7 changes: 3 additions & 4 deletions Block/DataLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

namespace MagePal\GoogleTagManager\Block;


/**
* Google Tag Manager Block
*/
class DataLayer extends DataLayerAbstract {
class DataLayer extends DataLayerAbstract
{

/**
* Render tag manager script
Expand All @@ -27,7 +27,7 @@ protected function _toHtml()
}

/** @var $blockOnepageOrder \MagePal\GoogleTagManager\Block\Data\Order */
if($this->getOrderIds() && $blockOnepageOrder = $this->getChildBlock("magepal_gtm_block_order")){
if ($this->getOrderIds() && $blockOnepageOrder = $this->getChildBlock("magepal_gtm_block_order")) {
$blockOnepageOrder->setOrderIds($this->getOrderIds())->addOrderLayer();
}

Expand All @@ -43,5 +43,4 @@ public function getAccountId()
{
return $this->_gtmHelper->getAccountId();
}

}
40 changes: 14 additions & 26 deletions Block/DataLayerAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Magento\Framework\View\Element\Template\Context;
use MagePal\GoogleTagManager\Helper\Data as GtmHelper;


/**
* Google Tag Manager Block
*/
Expand All @@ -32,22 +31,20 @@ class DataLayerAbstract extends Template
*/
protected $dataLayerEventName = 'magepal_datalayer';


/**
* @var array
*/
protected $_additionalVariables = [];

protected $_variables = [];

protected $_variables = [];

/**
* @param Context $context
* @param GtmHelper $gtmHelper
* @param array $data
*/
public function __construct(
Context $context,
Context $context,
GtmHelper $gtmHelper,
array $data = []
) {
Expand All @@ -56,13 +53,12 @@ public function __construct(
$this->_init();
}


/**
* @return $this
*/
protected function _init()
{
if($this->getShowEcommerceCurrencyCode()){
if ($this->getShowEcommerceCurrencyCode()) {
$this->addVariable('ecommerce', ['currencyCode' => $this->_storeManager->getStore()->getCurrentCurrency()->getCode()]);
}

Expand All @@ -72,7 +68,6 @@ protected function _init()
return $this;
}


/**
* Return data layer json
*
Expand All @@ -85,24 +80,22 @@ public function getDataLayer()
['dataLayer' => $this]
);

if(empty($this->getVariables()) && empty($this->_additionalVariables)){
if (empty($this->getVariables()) && empty($this->_additionalVariables)) {
return null;
}

$result = [];

if(!empty($this->getVariables())){
if (!empty($this->getVariables())) {
$result[] = sprintf("%s.push(%s);\n", $this->getDataLayerName(), json_encode($this->getVariables()));
}


if(!empty($this->_additionalVariables) && is_array($this->_additionalVariables)){

foreach($this->_additionalVariables as $custom){
if (!empty($this->_additionalVariables) && is_array($this->_additionalVariables)) {
foreach ($this->_additionalVariables as $custom) {
$result[] = sprintf("%s.push(%s);\n", $this->getDataLayerName(), json_encode($custom));
}
}

return implode("\n", $result);
}

Expand All @@ -114,7 +107,6 @@ public function getDataLayer()
*/
public function addVariable($name, $value)
{

if (!empty($name)) {
$this->_variables[$name] = $value;
}
Expand All @@ -132,7 +124,6 @@ public function getVariables()
return $this->_variables;
}


/**
* Add variable to the custom push data layer
*
Expand All @@ -142,13 +133,12 @@ public function getVariables()
*/
public function addAdditionalVariable($name, $value = null)
{
if(is_array($name)){
$this->_additionalVariables[] = $name;
}
else{
$this->_additionalVariables[] = [$name => $value];
}

if (is_array($name)) {
$this->_additionalVariables[] = $name;
} else {
$this->_additionalVariables[] = [$name => $value];
}

return $this;
}

Expand All @@ -170,6 +160,4 @@ public function getDataLayerName()
{
return $this->_gtmHelper->getDataLayerName();
}


}
Loading

0 comments on commit 7de216e

Please sign in to comment.