Skip to content

Commit b1e5c41

Browse files
authored
Merge pull request #312 from Inchoo/feature/#248-update-visibility-in-getmessageinstorelocale-function
Feature/#248 update visibility in getmessageinstorelocale function
2 parents ef99aa3 + 023a892 commit b1e5c41

File tree

2 files changed

+96
-36
lines changed

2 files changed

+96
-36
lines changed

Diff for: Controller/GeoIP/GetAction.php

+10-36
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
use Magento\Framework\View\Result\LayoutFactory;
3131
use Magento\Store\Api\StoreRepositoryInterface;
3232
use Magento\Store\Model\StoreManagerInterface;
33-
use Magento\Store\Api\Data\StoreInterface;
3433
use Psr\Log\LoggerInterface;
3534
use Magento\Framework\App\Action\Action;
35+
use Fastly\Cdn\Helper\StoreMessage;
3636

3737
/**
3838
* Class GetAction
@@ -62,14 +62,14 @@ class GetAction extends Action
6262
* @var LayoutFactory
6363
*/
6464
private $resultLayoutFactory;
65-
/**
66-
* @var LocaleResolverInterface
67-
*/
68-
private $localeResolver;
6965
/**
7066
* @var LoggerInterface
7167
*/
7268
private $logger;
69+
/**
70+
* @var StoreMessage
71+
*/
72+
private $storeMessage;
7373

7474
/**
7575
* GetAction constructor.
@@ -78,25 +78,25 @@ class GetAction extends Action
7878
* @param StoreRepositoryInterface $storeRepository
7979
* @param StoreManagerInterface $storeManager
8080
* @param LayoutFactory $resultLayoutFactory
81-
* @param LocaleResolverInterface $localeResolver
8281
* @param LoggerInterface $logger
82+
* @param StoreMessage $storeMessage
8383
*/
8484
public function __construct(
8585
Context $context,
8686
Config $config,
8787
StoreRepositoryInterface $storeRepository,
8888
StoreManagerInterface $storeManager,
8989
LayoutFactory $resultLayoutFactory,
90-
LocaleResolverInterface $localeResolver,
91-
LoggerInterface $logger
90+
LoggerInterface $logger,
91+
StoreMessage $storeMessage
9292
) {
9393
parent::__construct($context);
9494
$this->config = $config;
9595
$this->storeRepository = $storeRepository;
9696
$this->storeManager = $storeManager;
9797
$this->resultLayoutFactory = $resultLayoutFactory;
98-
$this->localeResolver = $localeResolver;
9998
$this->logger = $logger;
99+
$this->storeMessage = $storeMessage;
100100

101101
$this->url = $context->getUrl();
102102
}
@@ -145,7 +145,7 @@ public function execute()
145145
case Config::GEOIP_ACTION_DIALOG:
146146
$resultLayout->getLayout()->getUpdate()->load(['geoip_getaction_dialog']);
147147
$resultLayout->getLayout()->getBlock('geoip_getaction')->setMessage(
148-
$this->getMessageInStoreLocale($targetStore)
148+
$this->storeMessage->getMessageInStoreLocale($targetStore)
149149
);
150150
break;
151151
case Config::GEOIP_ACTION_REDIRECT:
@@ -164,30 +164,4 @@ public function execute()
164164
$resultLayout->setHeader("x-esi", "1");
165165
return $resultLayout;
166166
}
167-
168-
/**
169-
* Gets the dialog message in the locale of the target store.
170-
* @param StoreInterface $emulatedStore
171-
* @return string
172-
* @throws \Magento\Framework\Exception\NoSuchEntityException
173-
*/
174-
private function getMessageInStoreLocale(StoreInterface $emulatedStore)
175-
{
176-
$currentStore = $this->storeManager->getStore();
177-
178-
// emulate locale and store of new store to fetch message translation
179-
$this->localeResolver->emulate($emulatedStore->getId());
180-
$this->storeManager->setCurrentStore($emulatedStore->getId());
181-
182-
$message = __(
183-
'You are in the wrong store. Click OK to visit the %1 store.',
184-
[$emulatedStore->getName()]
185-
)->__toString();
186-
187-
// revert locale and store emulation
188-
$this->localeResolver->revert();
189-
$this->storeManager->setCurrentStore($currentStore->getId());
190-
191-
return $message;
192-
}
193167
}

Diff for: Helper/StoreMessage.php

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/**
3+
* Fastly CDN for Magento
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Fastly CDN for Magento End User License Agreement
8+
* that is bundled with this package in the file LICENSE_FASTLY_CDN.txt.
9+
*
10+
* DISCLAIMER
11+
*
12+
* Do not edit or add to this file if you wish to upgrade Fastly CDN to newer
13+
* versions in the future. If you wish to customize this module for your
14+
* needs please refer to http://www.magento.com for more information.
15+
*
16+
* @category Fastly
17+
* @package Fastly_Cdn
18+
* @copyright Copyright (c) 2016 Fastly, Inc. (http://www.fastly.com)
19+
* @license BSD, see LICENSE_FASTLY_CDN.txt
20+
*/
21+
namespace Fastly\Cdn\Helper;
22+
23+
use Magento\Framework\App\Helper\Context;
24+
use Magento\Framework\App\Helper\AbstractHelper;
25+
use Magento\Framework\Exception\NoSuchEntityException;
26+
use Magento\Framework\Locale\ResolverInterface as LocaleResolverInterface;
27+
use Magento\Store\Api\Data\StoreInterface;
28+
use Magento\Store\Model\StoreManagerInterface;
29+
30+
/**
31+
* Class StoreMessage
32+
* @package Fastly\Cdn\Helper
33+
*/
34+
class StoreMessage extends AbstractHelper
35+
{
36+
/**
37+
* @var StoreManagerInterface
38+
*/
39+
private $storeManager;
40+
41+
/***
42+
* @var LocaleResolverInterface
43+
*/
44+
private $localeResolver;
45+
46+
/**
47+
* StoreMessage constructor.
48+
* @param Context $context
49+
* @param StoreManagerInterface $storeManager
50+
* @param LocaleResolverInterface $localeResolver
51+
*/
52+
public function __construct(
53+
Context $context,
54+
StoreManagerInterface $storeManager,
55+
LocaleResolverInterface $localeResolver
56+
) {
57+
parent::__construct($context);
58+
$this->storeManager = $storeManager;
59+
$this->localeResolver = $localeResolver;
60+
}
61+
62+
/**
63+
* @param StoreInterface $emulatedStore
64+
* @return string
65+
* @throws NoSuchEntityException
66+
*/
67+
public function getMessageInStoreLocale(StoreInterface $emulatedStore): string
68+
{
69+
$currentStore = $this->storeManager->getStore();
70+
71+
// emulate locale and store of new store to fetch message translation
72+
$this->localeResolver->emulate($emulatedStore->getId());
73+
$this->storeManager->setCurrentStore($emulatedStore->getId());
74+
75+
$message = (string)__(
76+
'You are in the wrong store. Click OK to visit the %1 store.',
77+
[$emulatedStore->getName()]
78+
);
79+
80+
// revert locale and store emulation
81+
$this->localeResolver->revert();
82+
$this->storeManager->setCurrentStore($currentStore->getId());
83+
84+
return $message;
85+
}
86+
}

0 commit comments

Comments
 (0)