<?php
/**
* LandofCoder
*
* NOTICE OF LICENSE
*
* This source file is subject to the landofcoder.com license that is
* available through the world-wide-web at this URL:
* https://landofcoder.com/license
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category LandofCoder
* @package Lofmp_CouponCode
* @copyright Copyright (c) 2021 Landofcoder (https://landofcoder.com/)
* @license https://landofcoder.com/LICENSE-1.0.html
*/
namespace Lofmp\CouponCode\Controller;
use Magento\Framework\App\RouterInterface;
use Magento\Framework\App\ActionFactory;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\DataObject;
use Magento\Framework\Event\ManagerInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Url;
class Router implements RouterInterface
{
/**
* @var \Magento\Framework\App\ActionFactory
*/
protected $actionFactory;
/**
* Event manager
* @var \Magento\Framework\Event\ManagerInterface
*/
protected $eventManager;
/**
* Response
* @var \Magento\Framework\App\ResponseInterface
*/
protected $response;
/**
* @var bool
*/
protected $dispatched;
/**
* track order Helper
*/
protected $_dataHelper;
/**
* Store manager
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;
/**
* @param ActionFactory $actionFactory
* @param ResponseInterface $response
* @param ManagerInterface $eventManager
* @param \Lofmp\CouponCode\Helper\Data $dataHelper
* @param StoreManagerInterface $storeManager
*/
public function __construct(
ActionFactory $actionFactory,
ResponseInterface $response,
ManagerInterface $eventManager,
\Lofmp\CouponCode\Helper\Data $dataHelper,
StoreManagerInterface $storeManager
)
{
$this->actionFactory = $actionFactory;
$this->eventManager = $eventManager;
$this->response = $response;
$this->_dataHelper = $dataHelper;
$this->storeManager = $storeManager;
}
/**
* @param RequestInterface $request
* @return \Magento\Framework\App\ActionInterface
*/
public function match(RequestInterface $request)
{
$_dataHelper = $this->_dataHelper;
if (!$this->dispatched) {
$urlKey = trim($request->getPathInfo(), '/');
$origUrlKey = $urlKey;
/** @var Object $condition */
$condition = new DataObject(['url_key' => $urlKey, 'continue' => true]);
$this->eventManager->dispatch(
'lofmp_couponcode_controller_router_match_before',
['router' => $this, 'condition' => $condition]
);
$urlKey = $condition->getUrlKey();
if ($condition->getRedirectUrl()) {
$this->response->setRedirect($condition->getRedirectUrl());
$request->setDispatched(true);
return $this->actionFactory->create(
'Magento\Framework\App\Action\Redirect',
['request' => $request]
);
}
if (!$condition->getContinue()) {
return null;
}
$route = $_dataHelper->getConfig('general_settings/route');
$track_route = $_dataHelper->getConfig('general_settings/track_route');
if( $route !='' && $urlKey == $route )
{
$request->setModuleName('couponcode')
->setControllerName('redeem')
->setActionName('generate');
$request->setAlias(Url::REWRITE_REQUEST_PATH_ALIAS, $urlKey);
$this->dispatched = true;
return $this->actionFactory->create(
'Magento\Framework\App\Action\Forward',
['request' => $request]
);
}
if( $track_route !='' && $urlKey == $track_route )
{
$couponcode = isset($_GET['coupon_code'])?$_GET['coupon_code']:'';
$email = isset($_GET['email'])?$_GET['email']:'';
$request->setModuleName('couponcode')
->setControllerName('track')
->setActionName('trackcode')
->setParam('coupon_code', $couponcode)
->setParam('email', $email);
$request->setAlias(Url::REWRITE_REQUEST_PATH_ALIAS, $origUrlKey);
$this->dispatched = true;
return $this->actionFactory->create(
'Magento\Framework\App\Action\Forward',
['request' => $request]
);
}
}
}
} |