Skip to content

Commit

Permalink
Merge pull request #74 from synolia/fix/crash-on-add-taxon-to-product
Browse files Browse the repository at this point in the history
Fix crash when the product has the same category multiple times
  • Loading branch information
oallain authored Aug 3, 2021
2 parents 816b4cf + c286495 commit e4e873d
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 8 deletions.
14 changes: 13 additions & 1 deletion src/Repository/ChannelRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Sylius\Component\Core\Model\ChannelInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

/**
* @method ChannelInterface|null find($id, $lockMode = null, $lockVersion = null)
* @method ChannelInterface|null findOneBy(array $criteria, array $orderBy = null)
* @method ChannelInterface[] findAll()
* @method ChannelInterface[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*
* @extends ServiceEntityRepository<ChannelInterface>
*/
final class ChannelRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry, ParameterBagInterface $parameterBag)
{
parent::__construct($registry, $parameterBag->get('sylius.model.channel.class'));
/** @var class-string<ChannelInterface> $entityClass */
$entityClass = $parameterBag->get('sylius.model.channel.class');

parent::__construct($registry, $entityClass);
}

public function findByCurrencyCode(string $currencyCode): iterable
Expand Down
14 changes: 13 additions & 1 deletion src/Repository/ProductAttributeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Sylius\Component\Product\Model\ProductAttributeInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

/**
* @method ProductAttributeInterface|null find($id, $lockMode = null, $lockVersion = null)
* @method ProductAttributeInterface|null findOneBy(array $criteria, array $orderBy = null)
* @method ProductAttributeInterface[] findAll()
* @method ProductAttributeInterface[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*
* @extends ServiceEntityRepository<ProductAttributeInterface>
*/
final class ProductAttributeRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry, ParameterBagInterface $parameterBag)
{
parent::__construct($registry, $parameterBag->get('sylius.model.product_attribute.class'));
/** @var class-string<ProductAttributeInterface> $entityClass */
$entityClass = $parameterBag->get('sylius.model.product_attribute.class');

parent::__construct($registry, $entityClass);
}

/**
Expand Down
14 changes: 13 additions & 1 deletion src/Repository/ProductOptionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Sylius\Component\Product\Model\ProductOption;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

/**
* @method ProductOption|null find($id, $lockMode = null, $lockVersion = null)
* @method ProductOption|null findOneBy(array $criteria, array $orderBy = null)
* @method ProductOption[] findAll()
* @method ProductOption[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*
* @extends ServiceEntityRepository<ProductOption>
*/
final class ProductOptionRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry, ParameterBagInterface $parameterBag)
{
parent::__construct($registry, $parameterBag->get('sylius.model.product_option.class'));
/** @var class-string<ProductOption> $entityClass */
$entityClass = $parameterBag->get('sylius.model.product_option.class');

parent::__construct($registry, $entityClass);
}

public function getRemovedOptionIds(array $codes): array
Expand Down
14 changes: 13 additions & 1 deletion src/Repository/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Sylius\Component\Core\Model\ProductInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

/**
* @method ProductInterface|null find($id, $lockMode = null, $lockVersion = null)
* @method ProductInterface|null findOneBy(array $criteria, array $orderBy = null)
* @method ProductInterface[] findAll()
* @method ProductInterface[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*
* @extends ServiceEntityRepository<ProductInterface>
*/
final class ProductRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry, ParameterBagInterface $parameterBag)
{
parent::__construct($registry, $parameterBag->get('sylius.model.product.class'));
/** @var class-string<ProductInterface> $entityClass */
$entityClass = $parameterBag->get('sylius.model.product.class');

parent::__construct($registry, $entityClass);
}

public function findProductsUsingCategories(array $ids): iterable
Expand Down
13 changes: 12 additions & 1 deletion src/Repository/ProductTaxonRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,22 @@
use Sylius\Component\Core\Model\ProductTaxonInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

/**
* @method ProductTaxonInterface|null find($id, $lockMode = null, $lockVersion = null)
* @method ProductTaxonInterface|null findOneBy(array $criteria, array $orderBy = null)
* @method ProductTaxonInterface[] findAll()
* @method ProductTaxonInterface[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*
* @extends ServiceEntityRepository<ProductTaxonInterface>
*/
final class ProductTaxonRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry, ParameterBagInterface $parameterBag)
{
parent::__construct($registry, $parameterBag->get('sylius.model.product_taxon.class'));
/** @var class-string<ProductTaxonInterface> $entityClass */
$entityClass = $parameterBag->get('sylius.model.product_taxon.class');

parent::__construct($registry, $entityClass);
}

public function getProductTaxonIds(ProductInterface $product): array
Expand Down
14 changes: 13 additions & 1 deletion src/Repository/TaxonRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@

use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Sylius\Component\Core\Model\TaxonInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

/**
* @method TaxonInterface|null find($id, $lockMode = null, $lockVersion = null)
* @method TaxonInterface|null findOneBy(array $criteria, array $orderBy = null)
* @method TaxonInterface[] findAll()
* @method TaxonInterface[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*
* @extends ServiceEntityRepository<TaxonInterface>
*/
final class TaxonRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry, ParameterBagInterface $parameterBag)
{
parent::__construct($registry, $parameterBag->get('sylius.model.taxon.class'));
/** @var class-string<TaxonInterface> $entityClass */
$entityClass = $parameterBag->get('sylius.model.taxon.class');

parent::__construct($registry, $entityClass);
}

public function getMissingCategoriesIds(array $codes): iterable
Expand Down
1 change: 1 addition & 0 deletions src/Task/Attribute/DeleteEntityTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ private function removeUnusedAttributes(array $attributeCodes): void
return $data['id'];
}, $attributesIdsArray);

/** @var class-string $productAttributeClass */
$productAttributeClass = $this->parameterBag->get('sylius.model.product_attribute.class');
if (!class_exists($productAttributeClass)) {
throw new \LogicException('ProductAttribute class does not exist.');
Expand Down
1 change: 1 addition & 0 deletions src/Task/Option/DeleteTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ private function process(): void
$attributeCodes = $this->productAttributeRepository->getAllAttributeCodes();
$removedOptionIds = $this->productOptionRepository->getRemovedOptionIds($attributeCodes);

/** @var class-string $productOptionClass */
$productOptionClass = $this->parameterBag->get('sylius.model.product_option.class');
if (!class_exists($productOptionClass)) {
throw new \LogicException('ProductOption class does not exist.');
Expand Down
6 changes: 4 additions & 2 deletions src/Task/Product/AddProductToCategoriesTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ public function __invoke(PipelinePayloadInterface $payload): PipelinePayloadInte
return $payload;
}

foreach ($payload->getCategories() as $category) {
$taxon = $this->taxonRepository->findOneBy(['code' => $category]);
$taxonCodes = \array_unique($payload->getCategories());

foreach ($taxonCodes as $taxonCode) {
$taxon = $this->taxonRepository->findOneBy(['code' => $taxonCode]);
if (!$taxon instanceof TaxonInterface) {
continue;
}
Expand Down

0 comments on commit e4e873d

Please sign in to comment.