init
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\Model\MixStepPricing;
|
||||
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class WebsiteResolver
|
||||
{
|
||||
private const WEBSITE_CODE_MAP = [
|
||||
'us' => 'us_site',
|
||||
'au' => 'au',
|
||||
'eu' => 'eu',
|
||||
'uk' => 'uk',
|
||||
];
|
||||
|
||||
private array $resolved = [];
|
||||
|
||||
public function __construct(private readonly StoreManagerInterface $storeManager)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
public function resolve(string $alias): array
|
||||
{
|
||||
if ($alias !== strtolower($alias) || !isset(self::WEBSITE_CODE_MAP[$alias])) {
|
||||
throw new LocalizedException(__('Website must be one of: us, au, eu, uk.'));
|
||||
}
|
||||
if (isset($this->resolved[$alias])) {
|
||||
return $this->resolved[$alias];
|
||||
}
|
||||
|
||||
try {
|
||||
$website = $this->storeManager->getWebsite(self::WEBSITE_CODE_MAP[$alias]);
|
||||
} catch (\Throwable) {
|
||||
throw new LocalizedException(__('Magento website for "%1" is not configured.', $alias));
|
||||
}
|
||||
|
||||
$storeIds = array_values(array_filter(
|
||||
array_map('intval', $website->getStoreIds()),
|
||||
static fn(int $storeId): bool => $storeId > 0
|
||||
));
|
||||
if ($storeIds === []) {
|
||||
throw new LocalizedException(__('Website "%1" has no Store Views.', $alias));
|
||||
}
|
||||
|
||||
return $this->resolved[$alias] = [
|
||||
'alias' => $alias,
|
||||
'code' => (string)$website->getCode(),
|
||||
'website_id' => (int)$website->getId(),
|
||||
'store_ids' => $storeIds,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user