Skip to content

Feature/#248 update visibility in getmessageinstorelocale function #312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 10 additions & 36 deletions Controller/GeoIP/GetAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
use Magento\Framework\View\Result\LayoutFactory;
use Magento\Store\Api\StoreRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Api\Data\StoreInterface;
use Psr\Log\LoggerInterface;
use Magento\Framework\App\Action\Action;
use Fastly\Cdn\Helper\StoreMessage;

/**
* Class GetAction
Expand Down Expand Up @@ -62,14 +62,14 @@ class GetAction extends Action
* @var LayoutFactory
*/
private $resultLayoutFactory;
/**
* @var LocaleResolverInterface
*/
private $localeResolver;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var StoreMessage
*/
private $storeMessage;

/**
* GetAction constructor.
Expand All @@ -78,25 +78,25 @@ class GetAction extends Action
* @param StoreRepositoryInterface $storeRepository
* @param StoreManagerInterface $storeManager
* @param LayoutFactory $resultLayoutFactory
* @param LocaleResolverInterface $localeResolver
* @param LoggerInterface $logger
* @param StoreMessage $storeMessage
*/
public function __construct(
Context $context,
Config $config,
StoreRepositoryInterface $storeRepository,
StoreManagerInterface $storeManager,
LayoutFactory $resultLayoutFactory,
LocaleResolverInterface $localeResolver,
LoggerInterface $logger
LoggerInterface $logger,
StoreMessage $storeMessage
) {
parent::__construct($context);
$this->config = $config;
$this->storeRepository = $storeRepository;
$this->storeManager = $storeManager;
$this->resultLayoutFactory = $resultLayoutFactory;
$this->localeResolver = $localeResolver;
$this->logger = $logger;
$this->storeMessage = $storeMessage;

$this->url = $context->getUrl();
}
Expand Down Expand Up @@ -145,7 +145,7 @@ public function execute()
case Config::GEOIP_ACTION_DIALOG:
$resultLayout->getLayout()->getUpdate()->load(['geoip_getaction_dialog']);
$resultLayout->getLayout()->getBlock('geoip_getaction')->setMessage(
$this->getMessageInStoreLocale($targetStore)
$this->storeMessage->getMessageInStoreLocale($targetStore)
);
break;
case Config::GEOIP_ACTION_REDIRECT:
Expand All @@ -164,30 +164,4 @@ public function execute()
$resultLayout->setHeader("x-esi", "1");
return $resultLayout;
}

/**
* Gets the dialog message in the locale of the target store.
* @param StoreInterface $emulatedStore
* @return string
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
private function getMessageInStoreLocale(StoreInterface $emulatedStore)
{
$currentStore = $this->storeManager->getStore();

// emulate locale and store of new store to fetch message translation
$this->localeResolver->emulate($emulatedStore->getId());
$this->storeManager->setCurrentStore($emulatedStore->getId());

$message = __(
'You are in the wrong store. Click OK to visit the %1 store.',
[$emulatedStore->getName()]
)->__toString();

// revert locale and store emulation
$this->localeResolver->revert();
$this->storeManager->setCurrentStore($currentStore->getId());

return $message;
}
}
86 changes: 86 additions & 0 deletions Helper/StoreMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?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\Helper;

use Magento\Framework\App\Helper\Context;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Locale\ResolverInterface as LocaleResolverInterface;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* Class StoreMessage
* @package Fastly\Cdn\Helper
*/
class StoreMessage extends AbstractHelper
{
/**
* @var StoreManagerInterface
*/
private $storeManager;

/***
* @var LocaleResolverInterface
*/
private $localeResolver;

/**
* StoreMessage constructor.
* @param Context $context
* @param StoreManagerInterface $storeManager
* @param LocaleResolverInterface $localeResolver
*/
public function __construct(
Context $context,
StoreManagerInterface $storeManager,
LocaleResolverInterface $localeResolver
) {
parent::__construct($context);
$this->storeManager = $storeManager;
$this->localeResolver = $localeResolver;
}

/**
* @param StoreInterface $emulatedStore
* @return string
* @throws NoSuchEntityException
*/
public function getMessageInStoreLocale(StoreInterface $emulatedStore): string
{
$currentStore = $this->storeManager->getStore();

// emulate locale and store of new store to fetch message translation
$this->localeResolver->emulate($emulatedStore->getId());
$this->storeManager->setCurrentStore($emulatedStore->getId());

$message = (string)__(
'You are in the wrong store. Click OK to visit the %1 store.',
[$emulatedStore->getName()]
);

// revert locale and store emulation
$this->localeResolver->revert();
$this->storeManager->setCurrentStore($currentStore->getId());

return $message;
}
}