diff --git a/Block/Adminhtml/Config/Form/Field/Select/Country.php b/Block/Adminhtml/Config/Form/Field/Select/Country.php
index de69764..6f52c20 100644
--- a/Block/Adminhtml/Config/Form/Field/Select/Country.php
+++ b/Block/Adminhtml/Config/Form/Field/Select/Country.php
@@ -13,14 +13,11 @@
class Country extends Select
{
- private Full $countrySource;
-
public function __construct(
Context $context,
- Full $countrySource,
+ private Full $countrySource,
array $data = []
) {
- $this->countrySource = $countrySource;
parent::__construct($context, $data);
}
diff --git a/Block/Adminhtml/Config/Form/Field/Select/Store.php b/Block/Adminhtml/Config/Form/Field/Select/Store.php
index 1c2d3b2..5015383 100644
--- a/Block/Adminhtml/Config/Form/Field/Select/Store.php
+++ b/Block/Adminhtml/Config/Form/Field/Select/Store.php
@@ -13,11 +13,9 @@
class Store extends Select
{
- private StoreSource $storeSource;
-
public function __construct(
Context $context,
- StoreSource $storeSource,
+ private StoreSource $storeSource,
array $data = []
) {
$this->storeSource = $storeSource;
diff --git a/Block/Adminhtml/Config/Form/Field/Select/Website.php b/Block/Adminhtml/Config/Form/Field/Select/Website.php
index 753c7b4..098c3f3 100644
--- a/Block/Adminhtml/Config/Form/Field/Select/Website.php
+++ b/Block/Adminhtml/Config/Form/Field/Select/Website.php
@@ -13,11 +13,9 @@
class Website extends Select
{
- private StoreSource $storeSource;
-
public function __construct(
Context $context,
- StoreSource $storeSource,
+ private StoreSource $storeSource,
array $data = []
) {
$this->storeSource = $storeSource;
diff --git a/CustomerData/CountryStoreData.php b/CustomerData/CountryStoreData.php
index 3d8aefc..9268cb2 100644
--- a/CustomerData/CountryStoreData.php
+++ b/CustomerData/CountryStoreData.php
@@ -20,33 +20,14 @@
final class CountryStoreData implements SectionSourceInterface
{
- private CountryRegistryInterface $countryRegistry;
-
- private ResolverFactory $countryResolverFactory;
-
- private CountryStoreResolverInterface $countryStoreResolver;
-
- private StoreManagerInterface $storeManager;
-
- private ExtensibleDataObjectConverter $dataObjectConverter;
-
- private LoggerInterface $logger;
-
public function __construct(
- CountryRegistryInterface $countryRegistry,
- ResolverFactory $countryResolverFactory,
- CountryStoreResolverInterface $countryStoreResolver,
- StoreManagerInterface $storeManager,
- ExtensibleDataObjectConverter $dataObjectConverter,
- LoggerInterface $logger
- ) {
- $this->countryRegistry = $countryRegistry;
- $this->countryResolverFactory = $countryResolverFactory;
- $this->countryStoreResolver = $countryStoreResolver;
- $this->storeManager = $storeManager;
- $this->dataObjectConverter = $dataObjectConverter;
- $this->logger = $logger;
- }
+ private CountryRegistryInterface $countryRegistry,
+ private ResolverFactory $countryResolverFactory,
+ private CountryStoreResolverInterface $countryStoreResolver,
+ private StoreManagerInterface $storeManager,
+ private ExtensibleDataObjectConverter $dataObjectConverter,
+ private LoggerInterface $logger
+ ) {}
public function getSectionData(): array
{
@@ -66,7 +47,7 @@ private function isInvalidated(CountryInterface $country): bool
$registeredStore = $this->countryStoreResolver->getStoreAware($country);
$currentStore = $this->storeManager->getStore();
} catch (NoSuchEntityException $e) {
- $this->logger->error($e->getLogMessage(), $e->getTrace());
+ $this->logger->error($e->getLogMessage(), ['exception' => $e]);
return false;
}
diff --git a/Model/Config/Source/Options.php b/Model/Config/Source/Options.php
index 59c3662..a5ca886 100644
--- a/Model/Config/Source/Options.php
+++ b/Model/Config/Source/Options.php
@@ -11,16 +11,7 @@
final class Options implements OptionSourceInterface
{
- /**
- * @var string[][]
- */
- private array $options;
-
- public function __construct(
- array $options
- ) {
- $this->options = $options;
- }
+ public function __construct(private array $options) {}
public function toOptionArray(): array
{
diff --git a/Model/Country.php b/Model/Country.php
index fc258a9..76fbee2 100644
--- a/Model/Country.php
+++ b/Model/Country.php
@@ -15,17 +15,11 @@
final class Country extends AbstractSimpleObject implements CountryInterface
{
- private ListsInterface $localeList;
-
- private ExtensionAttributesFactory $extensionFactory;
-
public function __construct(
- ExtensionAttributesFactory $extensionFactory,
- ListsInterface $localeList,
+ private ExtensionAttributesFactory $extensionFactory,
+ private ListsInterface $localeList,
array $data = []
) {
- $this->localeList = $localeList;
- $this->extensionFactory = $extensionFactory;
parent::__construct($data);
}
diff --git a/Model/CountryRegistry.php b/Model/CountryRegistry.php
index 8be0803..72935e6 100644
--- a/Model/CountryRegistry.php
+++ b/Model/CountryRegistry.php
@@ -17,21 +17,11 @@ final class CountryRegistry implements CountryRegistryInterface
{
private ?CountryInterface $country = null;
- private CountryResolverInterface $countryResolver;
-
- private CountryRepositoryInterface $countryRepository;
-
- private DataPersistorInterface $dataPersistor;
-
public function __construct(
- CountryResolverInterface $countryResolver,
- CountryRepositoryInterface $countryRepository,
- DataPersistorInterface $dataPersistor
- ) {
- $this->countryResolver = $countryResolver;
- $this->countryRepository = $countryRepository;
- $this->dataPersistor = $dataPersistor;
- }
+ private CountryResolverInterface $countryResolver,
+ private CountryRepositoryInterface $countryRepository,
+ private DataPersistorInterface $dataPersistor
+ ) {}
public function get(): CountryInterface
{
diff --git a/Model/CountryRepository.php b/Model/CountryRepository.php
index f2ffce7..3f35a4f 100644
--- a/Model/CountryRepository.php
+++ b/Model/CountryRepository.php
@@ -11,27 +11,20 @@
use Opengento\CountryStore\Api\CountryRepositoryInterface;
use Opengento\CountryStore\Api\Data\CountryInterface;
use Opengento\CountryStore\Api\Data\CountryInterfaceFactory;
+
use function strtoupper;
final class CountryRepository implements CountryRepositoryInterface
{
- private CountryInterfaceFactory $countryFactory;
-
- private ReadExtensions $readExtensions;
-
/**
* @var CountryInterface[]
*/
- private array $countries;
+ private array $countries = [];
public function __construct(
- CountryInterfaceFactory $countryFactory,
- ReadExtensions $readExtensions
- ) {
- $this->countryFactory = $countryFactory;
- $this->readExtensions = $readExtensions;
- $this->countries = [];
- }
+ private CountryInterfaceFactory $countryFactory,
+ private ReadExtensions $readExtensions
+ ) {}
public function get(string $countryCode): CountryInterface
{
diff --git a/Model/CountryResolver.php b/Model/CountryResolver.php
index a921cd4..82eab35 100644
--- a/Model/CountryResolver.php
+++ b/Model/CountryResolver.php
@@ -8,6 +8,7 @@
namespace Opengento\CountryStore\Model;
use Magento\Framework\App\Config\ScopeConfigInterface;
+use Magento\Framework\Exception\NotFoundException;
use Magento\Store\Model\ScopeInterface;
use Opengento\CountryStore\Api\CountryResolverInterface;
use Opengento\CountryStore\Api\Data\CountryInterface;
@@ -19,18 +20,14 @@ final class CountryResolver implements CountryResolverInterface
private const CONFIG_PATH_COUNTRY_STORE_RESOLVER_CODE = 'country/resolver/code';
- private ScopeConfigInterface $scopeConfig;
-
- private ResolverFactory $resolverFactory;
-
public function __construct(
- ScopeConfigInterface $scopeConfig,
- ResolverFactory $resolverFactory
- ) {
- $this->scopeConfig = $scopeConfig;
- $this->resolverFactory = $resolverFactory;
- }
+ private ScopeConfigInterface $scopeConfig,
+ private ResolverFactory $resolverFactory
+ ) {}
+ /**
+ * @throws NotFoundException
+ */
public function getCountry(): CountryInterface
{
return $this->resolverFactory->get($this->resolveCountryResolverCode())->getCountry();
diff --git a/Model/CountryStoreResolver.php b/Model/CountryStoreResolver.php
index 349193a..4db321b 100644
--- a/Model/CountryStoreResolver.php
+++ b/Model/CountryStoreResolver.php
@@ -16,25 +16,16 @@
use Opengento\CountryStore\Api\Data\CountryInterface;
use Opengento\CountryStore\Model\Mapper\CountryStoreMapperInterface;
use Opengento\CountryStore\Model\Store\GetStoreByCountryInterface;
+
use function in_array;
final class CountryStoreResolver implements CountryStoreResolverInterface
{
- private StoreManagerInterface $storeManager;
-
- private CountryStoreMapperInterface $countryStoreMapper;
-
- private GetStoreByCountryInterface $getStoreByCountry;
-
public function __construct(
- StoreManagerInterface $storeManager,
- CountryStoreMapperInterface $countryStoreMapper,
- GetStoreByCountryInterface $getStoreByCountry
- ) {
- $this->storeManager = $storeManager;
- $this->countryStoreMapper = $countryStoreMapper;
- $this->getStoreByCountry = $getStoreByCountry;
- }
+ private StoreManagerInterface $storeManager,
+ private CountryStoreMapperInterface $countryStoreMapper,
+ private GetStoreByCountryInterface $getStoreByCountry
+ ) {}
public function getStoreAware(CountryInterface $country): StoreInterface
{
diff --git a/Model/EntityManager/CountryHydrator.php b/Model/EntityManager/CountryHydrator.php
index a20ef1f..77b5957 100644
--- a/Model/EntityManager/CountryHydrator.php
+++ b/Model/EntityManager/CountryHydrator.php
@@ -11,6 +11,7 @@
use Magento\Framework\Api\AbstractSimpleObject;
use Magento\Framework\EntityManager\HydratorInterface;
use Opengento\CountryStore\Api\Data\CountryInterface;
+
use function is_callable;
use function is_object;
use function method_exists;
diff --git a/Model/Mapper/CountryStoreMapper.php b/Model/Mapper/CountryStoreMapper.php
index e029411..9deba71 100644
--- a/Model/Mapper/CountryStoreMapper.php
+++ b/Model/Mapper/CountryStoreMapper.php
@@ -9,13 +9,14 @@
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\LocalizedException;
-use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Api\Data\WebsiteInterface;
-use Magento\Store\Model\StoreManagerInterface;
+use Magento\Store\Api\StoreRepositoryInterface;
+use Magento\Store\Api\WebsiteRepositoryInterface;
use Opengento\CountryStore\Api\Data\CountryInterface;
use Psr\Log\LoggerInterface;
+
use function array_fill_keys;
use function array_merge_recursive;
@@ -23,31 +24,17 @@ final class CountryStoreMapper implements CountryStoreMapperInterface
{
private const CONFIG_PATH_COUNTRY_STORE_MAP = 'country/information/store';
- private ScopeConfigInterface $scopeConfig;
-
- private SerializerInterface $serializer;
-
- private StoreManagerInterface $storeManager;
-
- private LoggerInterface $logger;
-
private ?array $countryStoreMapper = null;
-
private ?array $countriesByStore = null;
-
private ?array $storesByCountry = null;
public function __construct(
- ScopeConfigInterface $scopeConfig,
- SerializerInterface $serializer,
- StoreManagerInterface $storeManager,
- LoggerInterface $logger
- ) {
- $this->scopeConfig = $scopeConfig;
- $this->serializer = $serializer;
- $this->storeManager = $storeManager;
- $this->logger = $logger;
- }
+ private ScopeConfigInterface $scopeConfig,
+ private SerializerInterface $serializer,
+ private StoreRepositoryInterface $storeRepository,
+ private WebsiteRepositoryInterface $websiteRepository,
+ private LoggerInterface $logger
+ ) {}
public function getCountriesByStore(StoreInterface $store): array
{
@@ -77,17 +64,17 @@ private function loadMapper(): void
$countries = (array) $countryStoreMapper['countries'];
try {
- $store = $this->storeManager->getStore((int) $countryStoreMapper['store']);
- } catch (NoSuchEntityException $e) {
- $this->logger->error($e->getLogMessage(), $e->getTrace());
+ $store = $this->storeRepository->getActiveStoreById((int) $countryStoreMapper['store']);
+ } catch (LocalizedException $e) {
+ $this->logger->error($e->getLogMessage(), ['exception' => $e]);
continue;
}
$this->countriesByStore[$store->getCode()] = $countries;
try {
- $website = $this->storeManager->getWebsite($store->getWebsiteId());
+ $website = $this->websiteRepository->getById((int) $store->getWebsiteId());
} catch (LocalizedException $e) {
- $this->logger->error($e->getLogMessage(), $e->getTrace());
+ $this->logger->error($e->getLogMessage(), ['exception' => $e]);
continue;
}
$storeCountries = array_fill_keys($countries, [$store->getCode()]);
diff --git a/Model/Resolver/CloudFare.php b/Model/Resolver/CloudFare.php
index 18253c6..e9b03f4 100644
--- a/Model/Resolver/CloudFare.php
+++ b/Model/Resolver/CloudFare.php
@@ -19,21 +19,11 @@ final class CloudFare implements CountryResolverInterface
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;
- }
+ private Request $request,
+ private ResolverFactory $resolverFactory,
+ private CountryRepositoryInterface $countryRepository
+ ) {}
public function getCountry(): CountryInterface
{
diff --git a/Model/Resolver/DefaultCountryStore.php b/Model/Resolver/DefaultCountryStore.php
index 238fa0a..60eb5be 100644
--- a/Model/Resolver/DefaultCountryStore.php
+++ b/Model/Resolver/DefaultCountryStore.php
@@ -8,12 +8,14 @@
namespace Opengento\CountryStore\Model\Resolver;
use Magento\Framework\App\Config\ScopeConfigInterface;
+use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;
use Opengento\CountryStore\Api\CountryRepositoryInterface;
use Opengento\CountryStore\Api\CountryResolverInterface;
use Opengento\CountryStore\Api\Data\CountryInterface;
use Opengento\CountryStore\Model\Mapper\CountryStoreMapperInterface;
+
use function in_array;
final class DefaultCountryStore implements CountryResolverInterface
@@ -22,35 +24,20 @@ final class DefaultCountryStore implements CountryResolverInterface
private const CONFIG_PATH_DEFAULT_COUNTRY = 'general/country/default';
- private ScopeConfigInterface $scopeConfig;
-
- private StoreManagerInterface $storeManager;
-
- private CountryStoreMapperInterface $countryStoreMapper;
-
- private CountryRepositoryInterface $countryRepository;
-
public function __construct(
- ScopeConfigInterface $scopeConfig,
- StoreManagerInterface $storeManager,
- CountryStoreMapperInterface $countryStoreMapper,
- CountryRepositoryInterface $countryRepository
- ) {
- $this->scopeConfig = $scopeConfig;
- $this->storeManager = $storeManager;
- $this->countryStoreMapper = $countryStoreMapper;
- $this->countryRepository = $countryRepository;
- }
-
+ private ScopeConfigInterface $scopeConfig,
+ private StoreManagerInterface $storeManager,
+ private CountryStoreMapperInterface $countryStoreMapper,
+ private CountryRepositoryInterface $countryRepository
+ ) {}
+
+ /**
+ * @throws NoSuchEntityException
+ */
public function getCountry(): CountryInterface
{
- $store = $this->storeManager->getStore();
- $storeCountries = $this->countryStoreMapper->getCountriesByStore($store);
- $countryCode = (string) $this->scopeConfig->getValue(
- self::CONFIG_PATH_DEFAULT_COUNTRY,
- ScopeInterface::SCOPE_STORE,
- $store->getCode()
- );
+ $storeCountries = $this->countryStoreMapper->getCountriesByStore($this->storeManager->getStore());
+ $countryCode = $this->scopeConfig->getValue(self::CONFIG_PATH_DEFAULT_COUNTRY, ScopeInterface::SCOPE_STORE);
return $this->countryRepository->get(
$storeCountries && !in_array($countryCode, $storeCountries, true) ? $storeCountries[0] : $countryCode
diff --git a/Model/Resolver/HttpHeaderValue.php b/Model/Resolver/HttpHeaderValue.php
index 56f02c2..2e0f52d 100644
--- a/Model/Resolver/HttpHeaderValue.php
+++ b/Model/Resolver/HttpHeaderValue.php
@@ -17,29 +17,16 @@
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;
+ private const CONFIG_PATH_COUNTRY_HTTP_HEADER_NAME = 'country/resolver/http_header_name';
public function __construct(
- Request $request,
- ScopeConfigInterface $scopeConfig,
- ResolverFactory $resolverFactory,
- CountryRepositoryInterface $countryRepository
- ) {
- $this->request = $request;
- $this->scopeConfig = $scopeConfig;
- $this->resolverFactory = $resolverFactory;
- $this->countryRepository = $countryRepository;
- }
+ private Request $request,
+ private ScopeConfigInterface $scopeConfig,
+ private ResolverFactory $resolverFactory,
+ private CountryRepositoryInterface $countryRepository
+ ) {}
public function getCountry(): CountryInterface
{
diff --git a/Model/Resolver/ResolverFactory.php b/Model/Resolver/ResolverFactory.php
index 27f02fa..cf56303 100644
--- a/Model/Resolver/ResolverFactory.php
+++ b/Model/Resolver/ResolverFactory.php
@@ -7,7 +7,9 @@
namespace Opengento\CountryStore\Model\Resolver;
+use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\ObjectManagerInterface;
+use Magento\Framework\Phrase;
use Opengento\CountryStore\Api\CountryResolverInterface;
/**
@@ -15,23 +17,17 @@
*/
final class ResolverFactory
{
- private ObjectManagerInterface $objectManager;
-
- /**
- * @var string[]
- */
- private array $countryResolvers;
-
public function __construct(
- ObjectManagerInterface $objectManager,
- array $countryResolvers = []
- ) {
- $this->objectManager = $objectManager;
- $this->countryResolvers = $countryResolvers;
- }
+ private ObjectManagerInterface $objectManager,
+ private array $countryResolvers = []
+ ) {}
public function get(string $resolverCode): CountryResolverInterface
{
- return $this->objectManager->get($this->countryResolvers[$resolverCode]);
+ return $this->objectManager->get(
+ $this->countryResolvers[$resolverCode] ?? throw new NotFoundException(
+ new Phrase('"%1" resolver is not declared.', [$resolverCode])
+ )
+ );
}
}
diff --git a/Model/ResourceModel/Country/Relation/ReadHandler.php b/Model/ResourceModel/Country/Relation/ReadHandler.php
index 0660be2..f45e9e5 100644
--- a/Model/ResourceModel/Country/Relation/ReadHandler.php
+++ b/Model/ResourceModel/Country/Relation/ReadHandler.php
@@ -17,28 +17,18 @@
use Magento\Framework\Phrase;
use Opengento\CountryStore\Api\Data\CountryInterface;
use Psr\Log\LoggerInterface;
+
use function is_array;
final class ReadHandler implements ExtensionInterface
{
- private HydratorPool $hydratorPool;
-
- private CollectionFactory $collectionFactory;
-
- private LoggerInterface $logger;
-
- private ?Collection $collection;
+ private ?Collection $collection = null;
public function __construct(
- HydratorPool $hydratorPool,
- CollectionFactory $collectionFactory,
- LoggerInterface $logger
- ) {
- $this->hydratorPool = $hydratorPool;
- $this->collectionFactory = $collectionFactory;
- $this->logger = $logger;
- $this->collection = null;
- }
+ private HydratorPool $hydratorPool,
+ private CollectionFactory $collectionFactory,
+ private LoggerInterface $logger
+ ) {}
/**
* @inheridoc
@@ -59,7 +49,7 @@ public function execute($entity, $arguments = null): CountryInterface
try {
$country = $this->fetchCountry((string)$arguments['code']);
} catch (NoSuchEntityException $e) {
- $this->logger->error($e->getLogMessage(), $e->getTrace());
+ $this->logger->error($e->getLogMessage(), ['exception' => $e]);
return $entity;
}
diff --git a/Model/Store/GetStoreByCountry.php b/Model/Store/GetStoreByCountry.php
index 49d385e..f0858f9 100644
--- a/Model/Store/GetStoreByCountry.php
+++ b/Model/Store/GetStoreByCountry.php
@@ -14,44 +14,32 @@
use Opengento\CountryStore\Api\Data\CountryInterface;
use Opengento\CountryStore\Model\Mapper\CountryStoreMapperInterface;
use Psr\Log\LoggerInterface;
+
use function count;
use function in_array;
final class GetStoreByCountry implements GetStoreByCountryInterface
{
- private StoreManagerInterface $storeManager;
-
- private CountryStoreMapperInterface $countryStoreMapper;
-
- private RelatedWebsites $relatedWebsites;
-
- private LoggerInterface $logger;
-
public function __construct(
- StoreManagerInterface $storeManager,
- CountryStoreMapperInterface $countryStoreMapper,
- RelatedWebsites $relatedWebsites,
- LoggerInterface $logger
- ) {
- $this->storeManager = $storeManager;
- $this->countryStoreMapper = $countryStoreMapper;
- $this->relatedWebsites = $relatedWebsites;
- $this->logger = $logger;
- }
+ private StoreManagerInterface $storeManager,
+ private CountryStoreMapperInterface $countryStoreMapper,
+ private RelatedWebsites $relatedWebsites,
+ private LoggerInterface $logger
+ ) {}
public function getByWebsite(CountryInterface $country, WebsiteInterface $website): StoreInterface
{
$websiteIds = $this->relatedWebsites->getListIds($website);
$websitesCount = count($websiteIds);
- $stores = $this->countryStoreMapper->getStoresByCountry($country, $website);
- $relatedWebsite = null;
- for ($i = 0; $i < $websitesCount && !$stores; $i++) {
- $relatedWebsite = $this->storeManager->getWebsite($websiteIds[$i]);
- $stores = $this->countryStoreMapper->getStoresByCountry($country, $relatedWebsite);
+ for ($i = 0, $stores = []; $i < $websitesCount && !$stores; $i++) {
+ try {
+ $relatedWebsite = $this->storeManager->getWebsite($websiteIds[$i]);
+ $stores = $this->countryStoreMapper->getStoresByCountry($country, $relatedWebsite);
+ } catch (LocalizedException) {}
}
- $website = $stores && $relatedWebsite ? $relatedWebsite : $website;
+ $website = $stores && isset($relatedWebsite) ? $relatedWebsite : $website;
$store = $this->resolveDefaultStore($website);
if ($stores && !in_array($store->getCode(), $stores, true)) {
$store = $this->storeManager->getStore($stores[0]);
@@ -69,7 +57,7 @@ private function resolveDefaultStore(WebsiteInterface $website): StoreInterface
)->getDefaultStoreId()
);
} catch (LocalizedException $e) {
- $this->logger->error($e->getLogMessage(), $e->getTrace());
+ $this->logger->error($e->getLogMessage(), ['exception' => $e]);
}
return $this->storeManager->getDefaultStoreView();
diff --git a/Model/Store/RelatedWebsites.php b/Model/Store/RelatedWebsites.php
index 59fc689..619f923 100644
--- a/Model/Store/RelatedWebsites.php
+++ b/Model/Store/RelatedWebsites.php
@@ -10,30 +10,23 @@
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Store\Api\Data\WebsiteInterface;
+
use function array_map;
use function array_merge;
+use function array_values;
use function in_array;
final class RelatedWebsites
{
private const CONFIG_PATH_COUNTRY_WEBSITE_MAP = 'country/information/website';
- private ScopeConfigInterface $scopeConfig;
-
- private SerializerInterface $serializer;
-
- private array $websiteGroups;
-
- private array $websites;
+ private ?array $websiteGroups = null;
+ private array $websites = [];
public function __construct(
- ScopeConfigInterface $scopeConfig,
- SerializerInterface $serializer
- ) {
- $this->scopeConfig = $scopeConfig;
- $this->serializer = $serializer;
- $this->websites = [];
- }
+ private ScopeConfigInterface $scopeConfig,
+ private SerializerInterface $serializer
+ ) {}
public function getListIds(WebsiteInterface $website): array
{
diff --git a/Test/Unit/CustomerData/CountryStoreDataTest.php b/Test/Unit/CustomerData/CountryStoreDataTest.php
index 6a75a58..ee9d972 100644
--- a/Test/Unit/CustomerData/CountryStoreDataTest.php
+++ b/Test/Unit/CustomerData/CountryStoreDataTest.php
@@ -31,37 +31,14 @@ class CountryStoreDataTest extends TestCase
{
private const DEFAULT_RESOLVER_CLASS = 'Vendor\\Module\\Resolver\\Default';
- /**
- * @var MockObject|CountryRegistryInterface
- */
- private $countryRegistry;
-
- /**
- * @var ObjectManagerInterface|mixed|MockObject
- */
- private $objectManager;
-
+ private MockObject|CountryRegistryInterface $countryRegistry;
+ private MockObject|ObjectManagerInterface $objectManager;
private ResolverFactory $countryResolverFactory;
-
private CountryResolverInterface $countryResolver;
-
- /**
- * @var MockObject|CountryStoreResolverInterface
- */
- private $countryStoreResolver;
-
- /**
- * @var MockObject|StoreManagerInterface
- */
- private $storeManager;
-
- /**
- * @var MockObject|DataObjectProcessor
- */
- private $dataObjectProcessor;
-
+ private MockObject|CountryStoreResolverInterface $countryStoreResolver;
+ private MockObject|StoreManagerInterface $storeManager;
+ private MockObject|DataObjectProcessor $dataObjectProcessor;
private ExtensibleDataObjectConverter $dataObjectConverter;
-
private CountryStoreData $countryData;
protected function setUp(): void
diff --git a/Test/Unit/Model/CountryRegistryTest.php b/Test/Unit/Model/CountryRegistryTest.php
index 16c011f..c02c564 100644
--- a/Test/Unit/Model/CountryRegistryTest.php
+++ b/Test/Unit/Model/CountryRegistryTest.php
@@ -21,21 +21,9 @@
*/
class CountryRegistryTest extends TestCase
{
- /**
- * @var MockObject|CountryRepositoryInterface
- */
- private $countryRepository;
-
- /**
- * @var MockObject|CountryResolverInterface
- */
- private $countryResolver;
-
- /**
- * @var MockObject|DataPersistorInterface
- */
- private $dataPersistor;
-
+ private MockObject|CountryRepositoryInterface $countryRepository;
+ private MockObject|CountryResolverInterface $countryResolver;
+ private MockObject|DataPersistorInterface $dataPersistor;
private CountryRegistry $countryRegistry;
protected function setUp(): void
diff --git a/Test/Unit/Model/CountryRepositoryTest.php b/Test/Unit/Model/CountryRepositoryTest.php
index f49af4c..3a389df 100644
--- a/Test/Unit/Model/CountryRepositoryTest.php
+++ b/Test/Unit/Model/CountryRepositoryTest.php
@@ -19,16 +19,8 @@
*/
class CountryRepositoryTest extends TestCase
{
- /**
- * @var MockObject|CountryInterfaceFactory
- */
- private $countryFactory;
-
- /**
- * @var MockObject|ReadExtensions
- */
- private $readExtensions;
-
+ private MockObject|CountryInterfaceFactory $countryFactory;
+ private MockObject|ReadExtensions $readExtensions;
private CountryRepository $countryRepository;
protected function setUp(): void
diff --git a/Test/Unit/Model/CountryResolverTest.php b/Test/Unit/Model/CountryResolverTest.php
index 15b1ab3..d6ae0d3 100644
--- a/Test/Unit/Model/CountryResolverTest.php
+++ b/Test/Unit/Model/CountryResolverTest.php
@@ -21,16 +21,8 @@
*/
class CountryResolverTest extends TestCase
{
- /**
- * @var MockObject|ScopeConfigInterface
- */
- private $scopeConfig;
-
- /**
- * @var MockObject|ObjectManagerInterface
- */
- private $objectFactory;
-
+ private MockObject|ScopeConfigInterface $scopeConfig;
+ private MockObject|ObjectManagerInterface $objectFactory;
private CountryResolver $countryResolver;
/**
diff --git a/Test/Unit/Model/CountryTest.php b/Test/Unit/Model/CountryTest.php
index ae93cb4..daa6b96 100644
--- a/Test/Unit/Model/CountryTest.php
+++ b/Test/Unit/Model/CountryTest.php
@@ -19,16 +19,8 @@
*/
class CountryTest extends TestCase
{
- /**
- * @var MockObject|ListsInterface
- */
- private $localeList;
-
- /**
- * @var MockObject|ExtensionAttributesFactory
- */
- private $extensionFactory;
-
+ private MockObject|ListsInterface $localeList;
+ private MockObject|ExtensionAttributesFactory $extensionFactory;
private Country $country;
protected function setUp(): void
diff --git a/Test/Unit/Model/Mapper/CountryStoreMapperTest.php b/Test/Unit/Model/Mapper/CountryStoreMapperTest.php
index 9e9c8e5..4889535 100644
--- a/Test/Unit/Model/Mapper/CountryStoreMapperTest.php
+++ b/Test/Unit/Model/Mapper/CountryStoreMapperTest.php
@@ -11,6 +11,8 @@
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Api\Data\WebsiteInterface;
+use Magento\Store\Api\StoreRepositoryInterface;
+use Magento\Store\Api\WebsiteRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;
use Opengento\CountryStore\Api\Data\CountryInterface;
use Opengento\CountryStore\Model\Mapper\CountryStoreMapper;
@@ -23,31 +25,26 @@
*/
class CountryStoreMapperTest extends TestCase
{
- /**
- * @var MockObject|ScopeConfigInterface
- */
- private $scopeConfig;
-
- /**
- * @var MockObject|StoreManagerInterface
- */
- private $storeManager;
-
+ private MockObject|ScopeConfigInterface $scopeConfig;
+ private MockObject|StoreManagerInterface $storeRepository;
+ private MockObject|StoreManagerInterface $websiteRepository;
private CountryStoreMapper $countryStoreMapper;
protected function setUp(): void
{
$this->scopeConfig = $this->getMockForAbstractClass(ScopeConfigInterface::class);
- $this->storeManager = $this->getMockForAbstractClass(StoreManagerInterface::class);
+ $this->storeRepository = $this->getMockForAbstractClass(StoreRepositoryInterface::class);
+ $this->websiteRepository = $this->getMockForAbstractClass(WebsiteRepositoryInterface::class);
$this->countryStoreMapper = new CountryStoreMapper(
$this->scopeConfig,
new Json(),
- $this->storeManager,
+ $this->storeRepository,
+ $this->websiteRepository,
$this->getMockForAbstractClass(LoggerInterface::class)
);
- $this->setupStoreManager();
+ $this->setupStoreRepository();
}
/**
@@ -110,7 +107,7 @@ public function countriesByStoreMapper(): array
];
}
- private function setupStoreManager(): void
+ private function setupStoreRepository(): void
{
$this->scopeConfig->method('getValue')
->with('country/information/store', 'default', null)
@@ -121,12 +118,12 @@ private function setupStoreManager(): void
'"_6":{"countries":["RU"],"store":312},"_7":{"countries":["FR","ES"],"store":313}}'
);
- $this->storeManager->method('getWebsite')->willReturnMap([
+ $this->websiteRepository->method('getById')->willReturnMap([
[1, $this->createWebsiteMock(1, 'website_us')],
[2, $this->createWebsiteMock(2, 'website_eu')],
[3, $this->createWebsiteMock(3, 'website_emea')],
]);
- $this->storeManager->method('getStore')->willReturnMap([
+ $this->storeRepository->method('getActiveStoreById')->willReturnMap([
[111, $this->createStoreMock('store_us_us', 1)],
[112, $this->createStoreMock('store_us_ca', 1)],
[211, $this->createStoreMock('store_eu_fr', 2)],
@@ -135,14 +132,6 @@ private function setupStoreManager(): void
[311, $this->createStoreMock('store_emea_rs', 3)],
[312, $this->createStoreMock('store_emea_ru', 3)],
[313, $this->createStoreMock('store_emea_other', 3)],
- ['store_us_us', $this->createStoreMock('store_us_us', 1)],
- ['store_us_ca', $this->createStoreMock('store_us_ca', 1)],
- ['store_eu_fr', $this->createStoreMock('store_eu_fr', 2)],
- ['store_eu_de', $this->createStoreMock('store_eu_de', 2)],
- ['store_eu_be', $this->createStoreMock('store_eu_be', 2)],
- ['store_emea_rs', $this->createStoreMock('store_emea_rs', 3)],
- ['store_emea_ru', $this->createStoreMock('store_emea_ru', 3)],
- ['store_emea_other', $this->createStoreMock('store_emea_other', 3)],
]);
}
diff --git a/Test/Unit/Model/Resolver/CloudFareTest.php b/Test/Unit/Model/Resolver/CloudFareTest.php
index 2886e76..61c8f3c 100644
--- a/Test/Unit/Model/Resolver/CloudFareTest.php
+++ b/Test/Unit/Model/Resolver/CloudFareTest.php
@@ -23,21 +23,9 @@
*/
class CloudFareTest extends TestCase
{
- /**
- * @var MockObject|Request
- */
- private $request;
-
- /**
- * @var MockObject|CountryResolverInterface
- */
- private $resolver;
-
- /**
- * @var MockObject|CountryRepositoryInterface
- */
- private $countryRepository;
-
+ private MockObject|Request $request;
+ private MockObject|CountryResolverInterface $resolver;
+ private MockObject|CountryRepositoryInterface $countryRepository;
private CloudFare $cloudFareResolver;
protected function setUp(): void
diff --git a/Test/Unit/Model/Resolver/DefaultCountryStoreTest.php b/Test/Unit/Model/Resolver/DefaultCountryStoreTest.php
index 04ba6f7..a0a56ed 100644
--- a/Test/Unit/Model/Resolver/DefaultCountryStoreTest.php
+++ b/Test/Unit/Model/Resolver/DefaultCountryStoreTest.php
@@ -5,7 +5,7 @@
*/
declare(strict_types=1);
-namespace Opengento\CountryStore\Test\Unit\Model\Country\Resolver;
+namespace Opengento\CountryStore\Test\Unit\Model\Resolver;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Api\Data\StoreInterface;
@@ -22,26 +22,10 @@
*/
class DefaultCountryStoreTest extends TestCase
{
- /**
- * @var MockObject|ScopeConfigInterface
- */
- private $scopeConfig;
-
- /**
- * @var MockObject|StoreManagerInterface
- */
- private $storeManager;
-
- /**
- * @var MockObject|CountryStoreMapperInterface
- */
- private $countryStoreMapper;
-
- /**
- * @var MockObject|CountryRepositoryInterface
- */
- private $countryRepository;
-
+ private MockObject|ScopeConfigInterface $scopeConfig;
+ private MockObject|StoreManagerInterface $storeManager;
+ private MockObject|CountryStoreMapperInterface $countryStoreMapper;
+ private MockObject|CountryRepositoryInterface $countryRepository;
private DefaultCountryStore $defaultCountryStore;
protected function setUp(): void
@@ -75,7 +59,7 @@ public function testGetCountry(
->willReturn($countries);
$this->scopeConfig->expects($this->once())
->method('getValue')
- ->with('general/country/default', 'store', $store->getCode())
+ ->with('general/country/default', 'store')
->willReturn($defaultCountryCode);
$this->countryRepository->expects($this->once())
->method('get')
diff --git a/Test/Unit/Model/Resolver/HttpHeaderValueTest.php b/Test/Unit/Model/Resolver/HttpHeaderValueTest.php
index 6ab650e..13a0b74 100644
--- a/Test/Unit/Model/Resolver/HttpHeaderValueTest.php
+++ b/Test/Unit/Model/Resolver/HttpHeaderValueTest.php
@@ -24,26 +24,10 @@
*/
class HttpHeaderValueTest extends TestCase
{
- /**
- * @var MockObject|Request
- */
- private $request;
-
- /**
- * @var MockObject|ScopeConfigInterface
- */
- private $scopeConfig;
-
- /**
- * @var MockObject|CountryResolverInterface
- */
- private $resolver;
-
- /**
- * @var MockObject|CountryRepositoryInterface
- */
- private $countryRepository;
-
+ private MockObject|Request $request;
+ private MockObject|ScopeConfigInterface $scopeConfig;
+ private MockObject|CountryResolverInterface $resolver;
+ private MockObject|CountryRepositoryInterface $countryRepository;
private HttpHeaderValue $httpHeaderValueResolver;
protected function setUp(): void
diff --git a/Test/Unit/Model/Resolver/ResolverFactoryTest.php b/Test/Unit/Model/Resolver/ResolverFactoryTest.php
index e8579fd..3834205 100644
--- a/Test/Unit/Model/Resolver/ResolverFactoryTest.php
+++ b/Test/Unit/Model/Resolver/ResolverFactoryTest.php
@@ -12,6 +12,7 @@
use Opengento\CountryStore\Model\Resolver\ResolverFactory;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
+
use function count;
/**
@@ -19,11 +20,7 @@
*/
class ResolverFactoryTest extends TestCase
{
- /**
- * @var MockObject|ObjectManagerInterface
- */
- private $objectFactory;
-
+ private MockObject|ObjectManagerInterface $objectFactory;
private ResolverFactory $resolverFactory;
protected function setUp(): void
diff --git a/Test/Unit/Model/ResourceModel/Country/Relation/ReadHandlerTest.php b/Test/Unit/Model/ResourceModel/Country/Relation/ReadHandlerTest.php
index 2d972a8..3dc2af7 100644
--- a/Test/Unit/Model/ResourceModel/Country/Relation/ReadHandlerTest.php
+++ b/Test/Unit/Model/ResourceModel/Country/Relation/ReadHandlerTest.php
@@ -24,16 +24,8 @@
*/
class ReadHandlerTest extends TestCase
{
- /**
- * @var MockObject|Collection
- */
- private $collectionMock;
-
- /**
- * @var MockObject|HydratorPool
- */
- private $hydratorPool;
-
+ private MockObject|Collection $collectionMock;
+ private MockObject|HydratorPool $hydratorPool;
private ReadHandler $readHandler;
protected function setUp(): void
@@ -57,7 +49,7 @@ public function testExecute(string $isoAlpha2, string $isoAlpha3): void
$countryMock = $this->getMockBuilder(CountryModel::class)
->disableOriginalConstructor()
- ->setMethodsExcept(['getCountryId', '__call'])
+ ->onlyMethods(['getData'])
->getMock();
$countryMock->expects($this->exactly(3))->method('getData')->willReturnMap([
['country_id', null, $isoAlpha2],
diff --git a/Test/Unit/Model/Store/GetStoreByCountryTest.php b/Test/Unit/Model/Store/GetStoreByCountryTest.php
index 5245dd5..18a0c10 100644
--- a/Test/Unit/Model/Store/GetStoreByCountryTest.php
+++ b/Test/Unit/Model/Store/GetStoreByCountryTest.php
@@ -5,13 +5,15 @@
*/
declare(strict_types=1);
-namespace Opengento\CountryStore\Test\Unit\Model;
+namespace Opengento\CountryStore\Test\Unit\Model\Store;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Store\Api\Data\GroupInterface;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Api\Data\WebsiteInterface;
+use Magento\Store\Api\StoreRepositoryInterface;
+use Magento\Store\Api\WebsiteRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;
use Opengento\CountryStore\Api\Data\CountryInterface;
use Opengento\CountryStore\Model\Mapper\CountryStoreMapper;
@@ -23,36 +25,33 @@
class GetStoreByCountryTest extends TestCase
{
- /**
- * @var MockObject|ScopeConfigInterface
- */
- private $scopeConfig;
-
- /**
- * @var MockObject|StoreManagerInterface
- */
- private $storeManager;
-
+ private MockObject|ScopeConfigInterface $scopeConfig;
+ private MockObject|StoreManagerInterface $storeManager;
+ private MockObject|StoreManagerInterface $storeRepository;
+ private MockObject|StoreManagerInterface $websiteRepository;
private GetStoreByCountry $getStoreByCountry;
protected function setUp(): void
{
$this->scopeConfig = $this->getMockForAbstractClass(ScopeConfigInterface::class);
$this->storeManager = $this->getMockForAbstractClass(StoreManagerInterface::class);
+ $this->storeRepository = $this->getMockForAbstractClass(StoreRepositoryInterface::class);
+ $this->websiteRepository = $this->getMockForAbstractClass(WebsiteRepositoryInterface::class);
$this->getStoreByCountry = new GetStoreByCountry(
$this->storeManager,
new CountryStoreMapper(
$this->scopeConfig,
new Json(),
- $this->storeManager,
+ $this->storeRepository,
+ $this->websiteRepository,
$this->getMockForAbstractClass(LoggerInterface::class)
),
new RelatedWebsites($this->scopeConfig, new Json()),
$this->getMockForAbstractClass(LoggerInterface::class)
);
- $this->setupStoreManager();
+ $this->setupStoreRepository();
}
/**
@@ -84,7 +83,7 @@ public function mapperDataProvider(): array
];
}
- private function setupStoreManager(): void
+ private function setupStoreRepository(): void
{
$this->scopeConfig->method('getValue')->willReturnMap([
['country/information/website', 'default', null, '{"_0":{"websites":[1,3]},"_1":{"websites":[2]}}'],
@@ -99,11 +98,26 @@ private function setupStoreManager(): void
],
]);
+ $this->websiteRepository->method('getById')->willReturnMap([
+ [1, $this->createWebsiteMock(1, 'website_us', 11)],
+ [2, $this->createWebsiteMock(2, 'website_eu', 21)],
+ [3, $this->createWebsiteMock(3, 'website_emea', 31)],
+ ]);
$this->storeManager->method('getWebsite')->willReturnMap([
[1, $this->createWebsiteMock(1, 'website_us', 11)],
[2, $this->createWebsiteMock(2, 'website_eu', 21)],
[3, $this->createWebsiteMock(3, 'website_emea', 31)],
]);
+ $this->storeRepository->method('getActiveStoreById')->willReturnMap([
+ [111, $this->createStoreMock('store_us_us', 1)],
+ [112, $this->createStoreMock('store_us_ca', 1)],
+ [211, $this->createStoreMock('store_eu_fr', 2)],
+ [212, $this->createStoreMock('store_eu_de', 2)],
+ [213, $this->createStoreMock('store_eu_be', 2)],
+ [311, $this->createStoreMock('store_emea_rs', 3)],
+ [312, $this->createStoreMock('store_emea_ru', 3)],
+ [313, $this->createStoreMock('store_emea_fr', 3)],
+ ]);
$this->storeManager->method('getStore')->willReturnMap([
[111, $this->createStoreMock('store_us_us', 1)],
[112, $this->createStoreMock('store_us_ca', 1)],
diff --git a/Test/Unit/Model/Store/RelatedWebsitesTest.php b/Test/Unit/Model/Store/RelatedWebsitesTest.php
index 963484e..3d7359e 100644
--- a/Test/Unit/Model/Store/RelatedWebsitesTest.php
+++ b/Test/Unit/Model/Store/RelatedWebsitesTest.php
@@ -16,11 +16,7 @@
class RelatedWebsitesTest extends TestCase
{
- /**
- * @var MockObject|ScopeConfigInterface
- */
- private $scopeConfig;
-
+ private MockObject|ScopeConfigInterface $scopeConfig;
private RelatedWebsites $relatedWebsites;
protected function setUp(): void
diff --git a/composer.json b/composer.json
index 4cee828..cbf8860 100644
--- a/composer.json
+++ b/composer.json
@@ -10,7 +10,7 @@
"country"
],
"require": {
- "php": "^7.4||^8.0",
+ "php": "^8.1",
"psr/log": "*",
"magento/framework": "*",
"magento/module-config": "*",
@@ -20,9 +20,8 @@
"magento/module-theme": "*"
},
"require-dev": {
- "magento/magento-coding-standard": "^5",
- "magento/marketplace-eqp": "^4.0",
- "roave/security-advisories": "dev-master"
+ "magento/magento-coding-standard": "^33",
+ "roave/security-advisories": "dev-latest"
},
"suggest": {
"opengento/module-country-store-redirect": "This module will redirect the customers regarding their country of origin, on their first visit session.",