Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get default localized catalog #ESPP-218 #53

Merged
merged 1 commit into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion api/migrations/Version20220407091642.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public function up(Schema $schema): void
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE SCHEMA public');
$this->addSql('ALTER TABLE source_field DROP is_system');
$this->addSql('ALTER TABLE source_field DROP default_label');
}
Expand Down
31 changes: 31 additions & 0 deletions api/migrations/Version20220408122945.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20220408122945 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE localized_catalog ADD is_default BOOLEAN DEFAULT \'false\' NOT NULL');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE localized_catalog DROP is_default');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @package Elasticsuite
* @author ElasticSuite Team <elasticsuite@smile.fr>
* @copyright 2022 Smile
* @license Licensed to Smile-SA. All rights reserved. No warranty, explicit or implicit, provided.
* Unauthorized copying of this file, via any medium, is strictly prohibited.
*/

declare(strict_types=1);

namespace Elasticsuite\Catalog\DataPersister;

use ApiPlatform\Core\DataPersister\DataPersisterInterface;
use Doctrine\ORM\EntityManagerInterface;
use Elasticsuite\Catalog\Model\LocalizedCatalog;
use Elasticsuite\Catalog\Repository\LocalizedCatalogRepository;

class LocalizedCatalogDataPersister implements DataPersisterInterface
{
public function __construct(
private EntityManagerInterface $entityManager,
private LocalizedCatalogRepository $localizedCatalogRepository
) {
}

/**
* {@inheritdoc}
*/
public function supports($data): bool
{
return $data instanceof LocalizedCatalog;
}

/**
* {@inheritdoc}
*
* @param LocalizedCatalog $data
*
* @return LocalizedCatalog
*/
public function persist($data)
{
if ($data->isDefault()) {
$this->localizedCatalogRepository->unsetDefaultLocalizedCatalog();
}
$this->entityManager->persist($data);
$this->entityManager->flush();

return $data;
}

/**
* {@inheritdoc}
*
* @param LocalizedCatalog $data
*/
public function remove($data)
{
$this->entityManager->remove($data);
$this->entityManager->flush();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @package Elasticsuite
* @author ElasticSuite Team <elasticsuite@smile.fr>
* @copyright 2022 Smile
* @license Licensed to Smile-SA. All rights reserved. No warranty, explicit or implicit, provided.
* Unauthorized copying of this file, via any medium, is strictly prohibited.
*/

declare(strict_types=1);

namespace Elasticsuite\Catalog\Exception;

use ApiPlatform\Core\Exception\ExceptionInterface;

class NoCatalogException extends \LogicException implements ExceptionInterface
{
public function __construct(
string $message = 'No localized catalog found',
int $code = 0,
?\Throwable $previous = null
) {
parent::__construct($message, $code, $previous);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class LocalizedCatalog

private string $locale;

private bool $isDefault = false;

private Catalog $catalog;

public function getId(): ?int
Expand Down Expand Up @@ -91,6 +93,18 @@ public function setLocale(string $locale): self
return $this;
}

public function isDefault(): bool
{
return $this->isDefault;
}

public function setIsDefault(bool $isDefault): self
{
$this->isDefault = $isDefault;

return $this;
}

public function getCatalog(): ?Catalog
{
return $this->catalog;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,14 @@ public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, LocalizedCatalog::class);
}

public function unsetDefaultLocalizedCatalog(): void
{
$this->createQueryBuilder('')
->update(LocalizedCatalog::class, 'lc')
->set('lc.isDefault', ':isDefault')
->setParameter('isDefault', false)
->getQuery()
->execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
<field name="name" type="string" length="255" nullable="true" unique="false" />
<field name="code" type="string" length="255" nullable="false" unique="true" />
<field name="locale" type="string" length="5" nullable="false" unique="false" />
<field name="isDefault" type="boolean" nullable="false">
<options>
<option name="default">false</option>
</options>
</field>

<unique-constraints>
<unique-constraint columns="code,locale" name="unique_code_locale" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,15 @@ services:
- '@Doctrine\Persistence\ManagerRegistry'
tags:
- { name: doctrine.repository_service }

Elasticsuite\Catalog\DataPersister\LocalizedCatalogDataPersister:
arguments:
- '@doctrine.orm.entity_manager'
- '@Elasticsuite\Catalog\Repository\LocalizedCatalogRepository'
tags:
- { name: api_platform.data_persister }

Elasticsuite\Catalog\Service\DefaultCatalogProvider:
arguments:
- '@Elasticsuite\Catalog\Repository\LocalizedCatalogRepository'

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
Elasticsuite\Catalog\Service\DefaultCatalogProviderTest:
alias: Elasticsuite\Catalog\Service\DefaultCatalogProvider
public: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @package Elasticsuite
* @author ElasticSuite Team <elasticsuite@smile.fr>
* @copyright 2022 Smile
* @license Licensed to Smile-SA. All rights reserved. No warranty, explicit or implicit, provided.
* Unauthorized copying of this file, via any medium, is strictly prohibited.
*/

declare(strict_types=1);

namespace Elasticsuite\Catalog\Service;

use Elasticsuite\Catalog\Exception\NoCatalogException;
use Elasticsuite\Catalog\Model\LocalizedCatalog;
use Elasticsuite\Catalog\Repository\LocalizedCatalogRepository;

class DefaultCatalogProvider
{
/**
* @param LocalizedCatalogRepository $localizedCatalogRepository
*/
public function __construct(
private LocalizedCatalogRepository $localizedCatalogRepository
) {
}

public function getDefaultLocalizedCatalog(): LocalizedCatalog
{
$catalog = $this->localizedCatalogRepository->findOneBy([], ['isDefault' => 'DESC', 'id' => 'ASC']);
if (null === $catalog) {
throw new NoCatalogException();
}

return $catalog;
}
}
Loading