76 lines
1.9 KiB
PHP
76 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace External\ZNZBundle\Context;
|
|
|
|
use KupShop\KupShopBundle\Util\Compat\SymfonyBridge;
|
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
use Symfony\Contracts\Service\Attribute\Required;
|
|
|
|
class CountryContext extends \KupShop\I18nBundle\Context\CountryContext
|
|
{
|
|
#[Required]
|
|
public RequestStack $requestStack;
|
|
|
|
protected array $domains = [];
|
|
|
|
#[Required]
|
|
public function setDomains(#[Autowire(param: 'shop.domains')] array $domains): CountryContext
|
|
{
|
|
$this->domains = $domains;
|
|
|
|
return $this;
|
|
}
|
|
|
|
protected function validate($id)
|
|
{
|
|
return array_key_exists($id, $this->getAllActive());
|
|
}
|
|
|
|
public function getSupported()
|
|
{
|
|
return $this->getAllActive();
|
|
}
|
|
|
|
public function getAllActive(): array
|
|
{
|
|
$request = $this->getRequest();
|
|
if (!array_key_exists($request->getHost(), $this->domains)) {
|
|
return $this->getAll();
|
|
}
|
|
|
|
if (empty($this->domains[$request->getHost()]['country'])) {
|
|
return $this->getAll();
|
|
}
|
|
|
|
return array_filter($this->getAll(), fn ($x) => in_array($x->getId(), $this->domains[$request->getHost()]['country']));
|
|
}
|
|
|
|
protected function loadActive()
|
|
{
|
|
$request = $this->getRequest();
|
|
if (!array_key_exists($request->getHost(), $this->domains)) {
|
|
return 'CZ';
|
|
}
|
|
|
|
$result = $this->session->get('country');
|
|
if ($result && $this->validate($result)) {
|
|
return $result;
|
|
}
|
|
|
|
return $this->domains[$request->getHost()]['country'][0] ?? 'CZ';
|
|
}
|
|
|
|
private function getRequest(): Request
|
|
{
|
|
if (!($request = $this->requestStack->getMainRequest())) {
|
|
$request = SymfonyBridge::getCurrentRequest();
|
|
}
|
|
|
|
return $request;
|
|
}
|
|
}
|