-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OP-289: Implement handling unpacked bundles in API
- Loading branch information
Showing
17 changed files
with
230 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/Provider/AddProductBundleItemToCartCommandProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
/* | ||
* This file has been created by developers from BitBag. | ||
* Feel free to contact us once you face any issues or want to start | ||
* You can find more information about us on https://bitbag.io and write us | ||
* an email on [email protected]. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitBag\SyliusProductBundlePlugin\Provider; | ||
|
||
use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommand; | ||
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItemInterface; | ||
use BitBag\SyliusProductBundlePlugin\Factory\AddProductBundleItemToCartCommandFactoryInterface; | ||
use BitBag\SyliusProductBundlePlugin\Repository\ProductBundleItemRepositoryInterface; | ||
use BitBag\SyliusProductBundlePlugin\Repository\ProductBundleRepositoryInterface; | ||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Sylius\Component\Core\Model\ProductVariantInterface; | ||
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface; | ||
|
||
final class AddProductBundleItemToCartCommandProvider implements AddProductBundleItemToCartCommandProviderInterface | ||
{ | ||
public function __construct( | ||
private readonly AddProductBundleItemToCartCommandFactoryInterface $addProductBundleItemToCartCommandFactory, | ||
private readonly ProductBundleItemRepositoryInterface $productBundleItemRepository, | ||
private readonly ProductBundleRepositoryInterface $productBundleRepository, | ||
private readonly ProductVariantRepositoryInterface $productVariantRepository, | ||
) { | ||
} | ||
|
||
/** | ||
* @return Collection<int, AddProductBundleItemToCartCommand> | ||
* | ||
* @throws \Exception | ||
*/ | ||
public function provide(string $bundleCode, array $overwrittenVariants): Collection | ||
{ | ||
$bundle = $this->productBundleRepository->findOneByProductCode($bundleCode); | ||
if (null === $bundle) { | ||
throw new \Exception('Product bundle not found'); | ||
} | ||
|
||
$bundleItems = $this->productBundleItemRepository->findByBundleCode($bundleCode); | ||
|
||
$commands = []; | ||
foreach ($bundleItems as $bundleItem) { | ||
$command = $this->addProductBundleItemToCartCommandFactory->createNew($bundleItem); | ||
if (!$bundle->isPackedProduct() && [] !== $overwrittenVariants) { | ||
$this->overwriteVariant($command, $bundleItem, $overwrittenVariants); | ||
} | ||
$commands[] = $command; | ||
} | ||
|
||
return new ArrayCollection($commands); | ||
} | ||
|
||
private function overwriteVariant( | ||
AddProductBundleItemToCartCommand $command, | ||
ProductBundleItemInterface $bundleItem, | ||
array $overwrittenVariants, | ||
): void { | ||
foreach ($overwrittenVariants as $overwrittenVariant) { | ||
if (null !== $overwrittenVariant['from'] && null !== $overwrittenVariant['to'] && | ||
$bundleItem->getProductVariant()?->getCode() === $overwrittenVariant['from'] && | ||
$this->shouldOverwriteVariant($overwrittenVariant['from'], $overwrittenVariant['to']) | ||
) { | ||
/** @var ProductVariantInterface $newVariant */ | ||
$newVariant = $this->productVariantRepository->findOneBy(['code' => $overwrittenVariant['to']]); | ||
$command->setProductVariant($newVariant); | ||
} | ||
} | ||
} | ||
|
||
private function shouldOverwriteVariant(string $oldVariantCode, string $newVariantCode): bool | ||
{ | ||
$oldVariant = $this->productVariantRepository->findOneBy(['code' => $oldVariantCode]); | ||
$newVariant = $this->productVariantRepository->findOneBy(['code' => $newVariantCode]); | ||
|
||
return | ||
$oldVariant instanceof ProductVariantInterface && | ||
$newVariant instanceof ProductVariantInterface && | ||
$oldVariant->getProduct() === $newVariant->getProduct(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Provider/AddProductBundleItemToCartCommandProviderInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file has been created by developers from BitBag. | ||
* Feel free to contact us once you face any issues or want to start | ||
* You can find more information about us on https://bitbag.io and write us | ||
* an email on [email protected]. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitBag\SyliusProductBundlePlugin\Provider; | ||
|
||
use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommand; | ||
use Doctrine\Common\Collections\Collection; | ||
|
||
interface AddProductBundleItemToCartCommandProviderInterface | ||
{ | ||
/** @return Collection<int, AddProductBundleItemToCartCommand> */ | ||
public function provide(string $bundleCode, array $overwrittenVariants): Collection; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file has been created by developers from BitBag. | ||
* Feel free to contact us once you face any issues or want to start | ||
* You can find more information about us on https://bitbag.io and write us | ||
* an email on [email protected]. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitBag\SyliusProductBundlePlugin\Repository; | ||
|
||
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItemInterface; | ||
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository; | ||
|
||
class ProductBundleItemRepository extends EntityRepository implements ProductBundleItemRepositoryInterface | ||
{ | ||
/** @return ProductBundleItemInterface[] */ | ||
public function findByBundleCode(string $bundleCode): array | ||
{ | ||
return $this | ||
->createQueryBuilder('pbi') | ||
->leftJoin('pbi.productBundle', 'pb') | ||
->leftJoin('pb.product', 'p') | ||
->where('p.code = :code') | ||
->setParameter('code', $bundleCode) | ||
->getQuery() | ||
->getResult(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file has been created by developers from BitBag. | ||
* Feel free to contact us once you face any issues or want to start | ||
* You can find more information about us on https://bitbag.io and write us | ||
* an email on [email protected]. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitBag\SyliusProductBundlePlugin\Repository; | ||
|
||
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItemInterface; | ||
use Sylius\Component\Resource\Repository\RepositoryInterface; | ||
|
||
interface ProductBundleItemRepositoryInterface extends RepositoryInterface | ||
{ | ||
/** @return ProductBundleItemInterface[] */ | ||
public function findByBundleCode(string $bundleCode): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service | ||
id="bitbag_sylius_product_bundle.provider.add_product_bundle_item_to_cart_command" | ||
class="BitBag\SyliusProductBundlePlugin\Provider\AddProductBundleItemToCartCommandProvider" | ||
> | ||
<argument type="service" id="bitbag_sylius_product_bundle.factory.add_product_bundle_item_to_cart_command"/> | ||
<argument type="service" id="bitbag_sylius_product_bundle.repository.product_bundle_item"/> | ||
<argument type="service" id="bitbag_sylius_product_bundle.repository.product_bundle"/> | ||
<argument type="service" id="sylius.repository.product_variant"/> | ||
</service> | ||
</services> | ||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters