-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add geoip resolvers & prevent fatal error
- Loading branch information
1 parent
e875be9
commit 8fd2483
Showing
13 changed files
with
383 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
/** | ||
* Copyright © OpenGento, All rights reserved. | ||
* See LICENSE bundled with this library for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Opengento\CountryStore\Model\Resolver; | ||
|
||
use Magento\Framework\HTTP\PhpEnvironment\Request; | ||
use Opengento\CountryStore\Api\CountryRepositoryInterface; | ||
use Opengento\CountryStore\Api\CountryResolverInterface; | ||
use Opengento\CountryStore\Api\Data\CountryInterface; | ||
use Opengento\CountryStore\Model\CountryResolver; | ||
|
||
final class CloudFare implements CountryResolverInterface | ||
{ | ||
public const RESOLVER_CODE = 'cloudFare'; | ||
|
||
public const CF_HTTP_HEADER_IPCOUNTRY = 'HTTP_CF_IPCOUNTRY'; | ||
|
||
private Request $request; | ||
|
||
private ResolverFactory $resolverFactory; | ||
|
||
private CountryRepositoryInterface $countryRepository; | ||
|
||
public function __construct( | ||
Request $request, | ||
ResolverFactory $resolverFactory, | ||
CountryRepositoryInterface $countryRepository | ||
) { | ||
$this->request = $request; | ||
$this->resolverFactory = $resolverFactory; | ||
$this->countryRepository = $countryRepository; | ||
} | ||
|
||
public function getCountry(): CountryInterface | ||
{ | ||
$countryCode = (string) $this->request->getServerValue(self::CF_HTTP_HEADER_IPCOUNTRY); | ||
|
||
return $countryCode | ||
? $this->countryRepository->get($countryCode) | ||
: $this->resolverFactory->get(CountryResolver::DEFAULT_COUNTRY_RESOLVER_CODE)->getCountry(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/** | ||
* Copyright © OpenGento, All rights reserved. | ||
* See LICENSE bundled with this library for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Opengento\CountryStore\Model\Resolver; | ||
|
||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Framework\HTTP\PhpEnvironment\Request; | ||
use Magento\Store\Model\ScopeInterface; | ||
use Opengento\CountryStore\Api\CountryRepositoryInterface; | ||
use Opengento\CountryStore\Api\CountryResolverInterface; | ||
use Opengento\CountryStore\Api\Data\CountryInterface; | ||
use Opengento\CountryStore\Model\CountryResolver; | ||
|
||
final class HttpHeaderValue implements CountryResolverInterface | ||
{ | ||
private const CONFIG_PATH_COUNTRY_HTTP_HEADER_NAME = 'country/resolver/http_header_name'; | ||
|
||
public const RESOLVER_CODE = 'httpHeaderValue'; | ||
|
||
private Request $request; | ||
|
||
private ScopeConfigInterface $scopeConfig; | ||
|
||
private ResolverFactory $resolverFactory; | ||
|
||
private CountryRepositoryInterface $countryRepository; | ||
|
||
public function __construct( | ||
Request $request, | ||
ScopeConfigInterface $scopeConfig, | ||
ResolverFactory $resolverFactory, | ||
CountryRepositoryInterface $countryRepository | ||
) { | ||
$this->request = $request; | ||
$this->scopeConfig = $scopeConfig; | ||
$this->resolverFactory = $resolverFactory; | ||
$this->countryRepository = $countryRepository; | ||
} | ||
|
||
public function getCountry(): CountryInterface | ||
{ | ||
$countryCode = (string) $this->request->getServerValue( | ||
$this->scopeConfig->getValue(self::CONFIG_PATH_COUNTRY_HTTP_HEADER_NAME, ScopeInterface::SCOPE_WEBSITE) | ||
); | ||
|
||
return $countryCode | ||
? $this->countryRepository->get($countryCode) | ||
: $this->resolverFactory->get(CountryResolver::DEFAULT_COUNTRY_RESOLVER_CODE)->getCountry(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.