Skip to content

Commit

Permalink
Magento coding standards fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Joris Wagter committed Jul 10, 2024
1 parent d86d75a commit 3c34d66
Show file tree
Hide file tree
Showing 20 changed files with 530 additions and 162 deletions.
20 changes: 20 additions & 0 deletions Api/DataLayerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,31 @@

interface DataLayerInterface
{
/**
* Get the event name
*
* @return string the event name
*/
public function getEvent(): string;

/**
* Get the e-commerce Data Layer
*
* @return array containing the e-commerce data
*/
public function getEcommerce(): array;

/**
* Get the user_data Data Layer
*
* @return array containing the user data
*/
public function getUserData(): array;

/**
* Get the Data Layer as an associative array
*
* @return array
*/
public function getDataLayer(): array;
}
38 changes: 36 additions & 2 deletions Block/DataLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,29 @@
use Taggrs\DataLayer\Api\DataLayerInterface;
use Taggrs\DataLayer\Helper\UserDataHelper;

/**
* Use this class to create view blocks for generating a DataLayer
*/
abstract class DataLayer extends Template implements DataLayerInterface
{

/**
* @var UserDataHelper to get User Data from the session
*/
private UserDataHelper $userDataHelper;


/**
* @var string block template
*/
public $_template = 'data-layer.phtml';

/**
* Class constructor
*
* @param UserDataHelper $userDataHelper
* @param Template\Context $context
* @param array $data
*/
public function __construct(
UserDataHelper $userDataHelper,
Template\Context $context,
Expand All @@ -23,6 +38,11 @@ public function __construct(
$this->userDataHelper = $userDataHelper;
}

/**
* Get the Google Tag Manager URL from the system configuration
*
* @return string
*/
public function getGtmUrl(): string
{
if ($gtmUrl = $this->_scopeConfig->getValue('taggrs_datalayer/gtm/gtm_url')) {
Expand All @@ -32,12 +52,21 @@ public function getGtmUrl(): string
return 'www.googletagmanager.com';
}


/**
* Get JSON-encoded Data Layer
*
* @return string
*/
public function getDataLayerJson(): string
{
return json_encode($this->getDataLayer());
}

/**
* Get Data Layer as associative array
*
* @return array
*/
public function getDataLayer(): array
{
return [
Expand All @@ -46,6 +75,11 @@ public function getDataLayer(): array
];
}

/**
* Get user data from session
*
* @return array
*/
public function getUserData(): array
{
return $this->userDataHelper->getUserData();
Expand Down
41 changes: 35 additions & 6 deletions Block/Event/BeginCheckout.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,33 @@
namespace Taggrs\DataLayer\Block\Event;

use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Element\Template;
use Taggrs\DataLayer\Block\DataLayer;
use Taggrs\DataLayer\Helper\ProductViewDataHelper;
use Taggrs\DataLayer\Helper\QuoteDataHelper;
use Taggrs\DataLayer\Helper\UserDataHelper;

/**
* Generates a Data Layer for the begin_checkout event on the Checkout Index Page
*/
class BeginCheckout extends DataLayer
{

/**
* @var QuoteDataHelper to retrieve data from the current quote
*/
private QuoteDataHelper $quoteDataHelper;

/**
* Class constructor
*
* @param QuoteDataHelper $quoteDataHelper
* @param UserDataHelper $userDataHelper
* @param Template\Context $context
* @param array $data
*/
public function __construct(
QuoteDataHelper $quoteDataHelper,
UserDataHelper $userDataHelper,
Expand All @@ -25,19 +41,32 @@ public function __construct(
$this->quoteDataHelper = $quoteDataHelper;
}


/**
* Get the event name
*
* @return string the event name
*/
public function getEvent(): string
{
return 'begin_checkout';
}

/**
* Get the e-commerce Data Layer
*
* @return array containing the e-commerce data
*/
public function getEcommerce(): array
{
$currency = $this->_storeManager
->getStore()
->getCurrentCurrency()
->getCode()
;
try {
$currency = $this->_storeManager
->getStore()
->getCurrentCurrency()
->getCode()
;
} catch ( NoSuchEntityException|LocalizedException $e ) {
$currency = null;
}

$total = (float)$this->quoteDataHelper->getQuote()->getGrandTotal();
$items = $this->quoteDataHelper->getItemsFromQuote();
Expand Down
50 changes: 43 additions & 7 deletions Block/Event/CategoryViewItemList.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,43 @@

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Element\Template;
use Taggrs\DataLayer\Block\DataLayer;
use Taggrs\DataLayer\Helper\ProductViewDataHelper;
use Taggrs\DataLayer\Helper\UserDataHelper;

/**
* Generate a Data Layer for the view_item_list event on the Catalog Category Page
*/
class CategoryViewItemList extends DataLayer
{

/**
* @var RequestInterface the request object
*/
private RequestInterface $request;

/**
* @var ProductViewDataHelper to retrieve product details
*/
private ProductViewDataHelper $productHelper;

/**
* @var CategoryRepositoryInterface to retrieve category objects from the database
*/
private CategoryRepositoryInterface $categoryRepository;


/**
* Class constructor
*
* @param RequestInterface $request
* @param ProductViewDataHelper $productHelper
* @param CategoryRepositoryInterface $categoryRepository
* @param UserDataHelper $userDataHelper
* @param Template\Context $context
* @param array $data
*/
public function __construct(
RequestInterface $request,
ProductViewDataHelper $productHelper,
Expand All @@ -34,21 +56,35 @@ public function __construct(
$this->categoryRepository = $categoryRepository;
}


/**
* Get the event name
*
* @return string the event name
*/
public function getEvent(): string
{
return 'view_item_list';
}

/**
* Get the e-commerce Data Layer
*
* @return array containing the e-commerce data
*/
public function getEcommerce(): array
{
$category = $this->categoryRepository->get($this->request->getParam('id'));
$collection = $this->productHelper->getCurrentProductCollection();
$items = $this->productHelper->getItemsByCollection($collection);
try {
$category = $this->categoryRepository->get( $this->request->getParam( 'id' ) );
$collection = $this->productHelper->getCurrentProductCollection();
$items = $this->productHelper->getItemsByCollection($collection);
} catch ( NoSuchEntityException $e ) {
$items = [];
$category = null;
}

return [
'item_list_id' => $category->getId(),
'item_list_name' => $category->getName(),
'item_list_id' => $category !== null ? $category->getId() : null,
'item_list_name' => $category !== null ? $category->getName() : null,
'items' => $items,
'user_data' => $this->getUserData(),
];
Expand Down
61 changes: 0 additions & 61 deletions Block/Event/DataLayer.php

This file was deleted.

Loading

0 comments on commit 3c34d66

Please sign in to comment.