Skip to content

Commit

Permalink
Fix catalog code generation on first sync
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreGauthier committed Jan 24, 2024
1 parent b1805eb commit 077537a
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Service/IndexOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function createIndex(string $entityType, ChannelInterface $channel, Local
);
$indexData = [
'entityType' => $entityType,
'localizedCatalog' => $channel->getId() . '_' . $locale->getCode(),
'localizedCatalog' => 'catalog' . $channel->getCode() . '_' . $locale->getCode(),
];

/** @var IndexCreate $index */
Expand Down
5 changes: 3 additions & 2 deletions src/Synchronizer/AbstractSynchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ protected function createOrUpdateEntity(ModelInterface $entity): ModelInterface
if ($this->getIdentity($entity)) {
// Check if entity already exists.
$existingEntity = $this->getEntityFromApi($entity);

if (!$existingEntity) {
// Create it if needed. Also save it locally for later use.
$entity = $this->client->query($this->entityClass, $this->createEntityMethod, $entity);
Expand All @@ -129,10 +130,10 @@ protected function createOrUpdateEntity(ModelInterface $entity): ModelInterface
return $this->entityByCode[$this->getIdentity($entity)];
}

protected function getEntityFromApi(ModelInterface|string $entity): ?ModelInterface
protected function getEntityFromApi(ModelInterface|string|int $entity): ?ModelInterface
{
if ($this->allEntityHasBeenFetch) {
return $this->entityByCode[\is_string($entity) ? $entity : $this->getIdentity($entity)] ?? null;
return $this->entityByCode[\is_scalar($entity) ? $entity : $this->getIdentity($entity)] ?? null;
}

return $this->fetchEntity($entity);
Expand Down
5 changes: 3 additions & 2 deletions src/Synchronizer/CatalogSynchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ public function __construct(
public function getIdentity(ModelInterface $entity): string
{
/** @var Catalog $entity */
return 'catalog' . $entity->getCode();
return (string) $entity->getCode();
}

public function synchronizeAll(): void
{
$this->fetchEntities();

$this->catalogCodes = array_flip($this->getAllEntityCodes());
$this->localizedCatalogSynchronizer->fetchEntities();
$this->localizedCatalogCodes = array_flip($this->localizedCatalogSynchronizer->getAllEntityCodes());
Expand Down Expand Up @@ -95,7 +96,7 @@ public function synchronizeItem(array $params): ?ModelInterface

$catalog = $this->createOrUpdateEntity(
new Catalog([
'code' => (string) $channel->getId(),
'code' => 'catalog' . $channel->getId(),
'name' => $channel->getName(),
])
);
Expand Down
6 changes: 3 additions & 3 deletions src/Synchronizer/LocalizedCatalogSynchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class LocalizedCatalogSynchronizer extends AbstractSynchronizer
public function getIdentity(ModelInterface $entity): string
{
/** @var LocalizedCatalog $entity */
return $entity->getCode();
return (string) $entity->getCode();
}

public function synchronizeAll(): void
Expand All @@ -50,7 +50,7 @@ public function synchronizeItem(array $params): ?ModelInterface
return $this->createOrUpdateEntity(
new LocalizedCatalog([
'name' => $locale->getName(),
'code' => $channel->getId() . '_' . $locale->getCode(),
'code' => $channel->getCode() . '_' . $locale->getCode(),
'locale' => str_replace('-', '_', $locale->getCode()),
'currency' => $channel->getBaseCurrency()->getCode(),
'isDefault' => $locale->getId() == $channel->getDefaultLocale()->getId(),
Expand All @@ -68,7 +68,7 @@ protected function addEntityByIdentity(ModelInterface $entity): void
$this->localizedCatalogByLocale[$entity->getLocale()] = [];
}

$this->localizedCatalogByLocale[$entity->getLocale()][$entity->getId()] = $entity;
$this->localizedCatalogByLocale[$entity->getLocale()][$entity->getCode()] = $entity;
}

public function getLocalizedCatalogByLocale(string $localeCode): array
Expand Down

0 comments on commit 077537a

Please sign in to comment.