-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathGetAction.php
167 lines (155 loc) · 5.58 KB
/
GetAction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* Fastly CDN for Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Fastly CDN for Magento End User License Agreement
* that is bundled with this package in the file LICENSE_FASTLY_CDN.txt.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Fastly CDN to newer
* versions in the future. If you wish to customize this module for your
* needs please refer to http://www.magento.com for more information.
*
* @category Fastly
* @package Fastly_Cdn
* @copyright Copyright (c) 2016 Fastly, Inc. (http://www.fastly.com)
* @license BSD, see LICENSE_FASTLY_CDN.txt
*/
namespace Fastly\Cdn\Controller\GeoIP;
use Fastly\Cdn\Model\Config;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Locale\ResolverInterface as LocaleResolverInterface;
use Magento\Framework\UrlInterface;
use Magento\Framework\View\Result\Layout;
use Magento\Framework\View\Result\LayoutFactory;
use Magento\Store\Api\StoreRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface;
use Magento\Framework\App\Action\Action;
use Fastly\Cdn\Helper\StoreMessage;
/**
* Class GetAction
*
* @package Fastly\Cdn\Controller\GeoIP
*/
class GetAction extends Action
{
const REQUEST_PARAM_COUNTRY = 'country_code';
/**
* @var Config
*/
private $config;
/**
* @var UrlInterface
*/
private $url;
/**
* @var StoreRepositoryInterface
*/
private $storeRepository;
/**
* @var StoreManagerInterface
*/
private $storeManager;
/**
* @var LayoutFactory
*/
private $resultLayoutFactory;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var StoreMessage
*/
private $storeMessage;
/**
* GetAction constructor.
* @param Context $context
* @param Config $config
* @param StoreRepositoryInterface $storeRepository
* @param StoreManagerInterface $storeManager
* @param LayoutFactory $resultLayoutFactory
* @param LoggerInterface $logger
* @param StoreMessage $storeMessage
*/
public function __construct(
Context $context,
Config $config,
StoreRepositoryInterface $storeRepository,
StoreManagerInterface $storeManager,
LayoutFactory $resultLayoutFactory,
LoggerInterface $logger,
StoreMessage $storeMessage
) {
parent::__construct($context);
$this->config = $config;
$this->storeRepository = $storeRepository;
$this->storeManager = $storeManager;
$this->resultLayoutFactory = $resultLayoutFactory;
$this->logger = $logger;
$this->storeMessage = $storeMessage;
$this->url = $context->getUrl();
}
/**
* Get GeoIP action
*
* @return ResponseInterface|ResultInterface|Layout|null
*/
public function execute()
{
$resultLayout = null;
try {
$resultLayout = $this->resultLayoutFactory->create();
$resultLayout->addDefaultHandle();
// get target store from country code
$countryCode = $this->getRequest()->getParam(self::REQUEST_PARAM_COUNTRY);
$storeId = $this->config->getGeoIpMappingForCountry($countryCode);
$targetUrl = $this->getRequest()->getParam('uenc');
if ($storeId !== null) {
// get redirect URL
$redirectUrl = null;
$targetStore = $this->storeRepository->getActiveStoreById($storeId);
$currentStore = $this->storeManager->getStore();
// only generate a redirect URL if current and new store are different
if ($currentStore->getId() != $targetStore->getId()) {
$this->url->setScope($targetStore->getId());
$queryParams = [
'___store' => $targetStore->getCode(),
'___from_store' => $currentStore->getCode()
];
if ($targetUrl) {
$queryParams['uenc'] = $targetUrl;
}
$this->url->addQueryParams($queryParams);
$redirectUrl = $this->url->getUrl('stores/store/switch');
}
// generate output only if redirect should be performed
if ($redirectUrl) {
switch ($this->config->getGeoIpAction()) {
case Config::GEOIP_ACTION_DIALOG:
$resultLayout->getLayout()->getUpdate()->load(['geoip_getaction_dialog']);
$resultLayout->getLayout()->getBlock('geoip_getaction')->setMessage(
$this->storeMessage->getMessageInStoreLocale($targetStore)
);
break;
case Config::GEOIP_ACTION_REDIRECT:
$resultLayout->getLayout()->getUpdate()->load(['geoip_getaction_redirect']);
break;
}
$resultLayout->getLayout()->getBlock('geoip_getaction')->setRedirectUrl($redirectUrl);
}
}
} catch (\Exception $e) {
$this->logger->critical($e->getMessage());
// do not generate output on errors. this is similar to an empty GeoIP mapping for the country.
}
$resultLayout->setHeader("x-esi", "1");
return $resultLayout;
}
}