Skip to content

Commit

Permalink
Add flush to taxon-adapter (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes authored Apr 15, 2021
1 parent 429f08c commit a8a79a1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
14 changes: 13 additions & 1 deletion Adapter/TaxonCategoryAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sulu\Bundle\SyliusConsumerBundle\Adapter;

use Doctrine\ORM\EntityManagerInterface;
use Sulu\Bundle\CategoryBundle\Entity\CategoryInterface;
use Sulu\Bundle\CategoryBundle\Entity\CategoryRepositoryInterface;
use Sulu\Bundle\CategoryBundle\Entity\CategoryTranslationInterface;
Expand All @@ -38,19 +39,30 @@ class TaxonCategoryAdapter implements TaxonAdapterInterface
*/
private $categoryTranslationRepository;

/**
* @var EntityManagerInterface
*/
private $entityManager;

public function __construct(
TaxonCategoryBridgeRepositoryInterface $taxonCategoryBridgeRepository,
CategoryRepositoryInterface $categoryRepository,
CategoryTranslationRepositoryInterface $categoryTranslationRepository
CategoryTranslationRepositoryInterface $categoryTranslationRepository,
EntityManagerInterface $entityManager
) {
$this->taxonCategoryBridgeRepository = $taxonCategoryBridgeRepository;
$this->categoryRepository = $categoryRepository;
$this->categoryTranslationRepository = $categoryTranslationRepository;
$this->entityManager = $entityManager;
}

public function synchronize(TaxonPayload $payload): void
{
$this->handlePayload($payload);

// Needed to use categories in other adapters
// (e.g. category pages with a smart-content filtered by the sylius category)
$this->entityManager->flush();
}

private function handlePayload(TaxonPayload $payload, ?CategoryInterface $parent = null): void
Expand Down
4 changes: 2 additions & 2 deletions Payload/ProductPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public function isEnabled(): bool
return $this->payload->getBoolValue('enabled');
}

public function getMainTaxonId(): int
public function getMainTaxonId(): ?int
{
return $this->payload->getIntValue('mainTaxonId');
return $this->payload->getNullableIntValue('mainTaxonId');
}

/**
Expand Down
1 change: 1 addition & 0 deletions Resources/config/taxon_category_adapter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<argument type="service" id="sulu.repository.taxon_category_bridge"/>
<argument type="service" id="sulu.repository.category"/>
<argument type="service" id="sulu.repository.category_translation"/>
<argument type="service" id="doctrine.orm.entity_manager"/>

<tag name="sulu_sylius_consumer.adapter.taxon"/>
</service>
Expand Down
8 changes: 0 additions & 8 deletions Tests/Application/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
orm:
mappings:
gedmo_tree:
type: xml
prefix: Gedmo\Tree\Entity
dir: "%kernel.project_dir%/../../vendor/gedmo/doctrine-extensions/lib/Gedmo/Tree/Entity"
alias: GedmoTree
is_bundle: false

framework:
router:
Expand Down
4 changes: 3 additions & 1 deletion Tests/Functional/Adapter/TaxonCategoryAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public function testSynchronize(): void
$taxonPayload = new TaxonPayload(1, MockSyliusData::TAXON);

$adapter->synchronize($taxonPayload);
$this->getEntityManager()->flush();

// Adapter flushed the entity-manager - by clearing it we can check if that works correctly
$this->getEntityManager()->clear();

$bridge1 = $this->getEntityManager()->find(TaxonCategoryBridge::class, 1);
$bridge2 = $this->getEntityManager()->find(TaxonCategoryBridge::class, 2);
Expand Down

0 comments on commit a8a79a1

Please sign in to comment.