Skip to content

Commit 3296e11

Browse files
committed
[WIP]OP-289: Implement handling unpacked bundles in API
1 parent 3dff2a8 commit 3296e11

14 files changed

+170
-9
lines changed

src/Command/AddProductBundleToCartCommand.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace BitBag\SyliusProductBundlePlugin\Command;
1313

14+
use Doctrine\Common\Collections\ArrayCollection;
1415
use Doctrine\Common\Collections\Collection;
1516

1617
final class AddProductBundleToCartCommand implements OrderIdentityAwareInterface, ProductCodeAwareInterface
@@ -19,10 +20,11 @@ final class AddProductBundleToCartCommand implements OrderIdentityAwareInterface
1920
private Collection $productBundleItems;
2021

2122
public function __construct(
22-
private int $orderId,
23-
private string $productCode,
24-
private int $quantity = 1,
23+
private readonly int $orderId,
24+
private readonly string $productCode,
25+
private readonly int $quantity = 1,
2526
) {
27+
$this->productBundleItems = new ArrayCollection();
2628
}
2729

2830
public function getOrderId(): int

src/DataTransformer/AddProductBundleToCartDtoDataTransformer.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@
1414
use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
1515
use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleToCartCommand;
1616
use BitBag\SyliusProductBundlePlugin\Dto\Api\AddProductBundleToCartDto;
17+
use BitBag\SyliusProductBundlePlugin\Provider\AddProductBundleItemToCartCommandProviderInterface;
1718
use Sylius\Component\Order\Model\OrderInterface;
1819
use Webmozart\Assert\Assert;
1920

2021
final class AddProductBundleToCartDtoDataTransformer implements DataTransformerInterface
2122
{
2223
public const OBJECT_TO_POPULATE = 'object_to_populate';
2324

25+
public function __construct(
26+
private readonly AddProductBundleItemToCartCommandProviderInterface $addProductBundleItemToCartCommandProvider,
27+
) {
28+
}
29+
2430
/**
2531
* @param AddProductBundleToCartDto|object $object
2632
*/
@@ -37,8 +43,13 @@ public function transform(
3743

3844
$productCode = $object->getProductCode();
3945
$quantity = $object->getQuantity();
46+
$variantCodes = $object->getVariantCodes();
47+
$addItemToCartCommands = $this->addProductBundleItemToCartCommandProvider->provide($productCode, $variantCodes);
48+
49+
$command = new AddProductBundleToCartCommand($cart->getId(), $productCode, $quantity);
50+
$command->setProductBundleItems($addItemToCartCommands);
4051

41-
return new AddProductBundleToCartCommand($cart->getId(), $productCode, $quantity);
52+
return $command;
4253
}
4354

4455
public function supportsTransformation(

src/DependencyInjection/Configuration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleOrderItemInterface;
2020
use BitBag\SyliusProductBundlePlugin\Form\Type\ProductBundleItemType;
2121
use BitBag\SyliusProductBundlePlugin\Form\Type\ProductBundleType;
22+
use BitBag\SyliusProductBundlePlugin\Repository\ProductBundleItemRepository;
2223
use BitBag\SyliusProductBundlePlugin\Repository\ProductBundleRepository;
2324
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
2425
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
@@ -80,7 +81,7 @@ private function addResourcesSection(ArrayNodeDefinition $node): void
8081
->scalarNode('interface')->defaultValue(ProductBundleItemInterface::class)->cannotBeEmpty()->end()
8182
->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end()
8283
->scalarNode('factory')->defaultValue(Factory::class)->cannotBeEmpty()->end()
83-
->scalarNode('repository')->cannotBeEmpty()->end()
84+
->scalarNode('repository')->defaultValue(ProductBundleItemRepository::class)->cannotBeEmpty()->end()
8485
->scalarNode('form')->defaultValue(ProductBundleItemType::class)->cannotBeEmpty()->end()
8586
->end()
8687
->end()

src/Dto/Api/AddProductBundleToCartDto.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function __construct(
1919
private string $productCode,
2020
private int $quantity = 1,
2121
private ?string $orderTokenValue = null,
22+
private array $variantCodes = [],
2223
) {
2324
}
2425

@@ -41,4 +42,14 @@ public function getQuantity(): int
4142
{
4243
return $this->quantity;
4344
}
45+
46+
public function getVariantCodes(): array
47+
{
48+
return $this->variantCodes;
49+
}
50+
51+
public function setVariantCodes(array $variantCodes): void
52+
{
53+
$this->variantCodes = $variantCodes;
54+
}
4455
}

src/Factory/AddProductBundleToCartCommandFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
final class AddProductBundleToCartCommandFactory implements AddProductBundleToCartCommandFactoryInterface
2020
{
21-
/** @param Collection<int, AddProductBundleItemToCartCommand> */
21+
/** @param Collection<int, AddProductBundleItemToCartCommand> $productBundleItems */
2222
public function createNew(
2323
int $orderId,
2424
string $productCode,

src/Handler/AddProductBundleToCartHandler.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
final class AddProductBundleToCartHandler implements MessageHandlerInterface
2424
{
2525
public function __construct(
26-
private OrderRepositoryInterface $orderRepository,
27-
private ProductRepositoryInterface $productRepository,
28-
private CartProcessorInterface $cartProcessor,
26+
private readonly OrderRepositoryInterface $orderRepository,
27+
private readonly ProductRepositoryInterface $productRepository,
28+
private readonly CartProcessorInterface $cartProcessor,
2929
) {
3030
}
3131

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusProductBundlePlugin\Provider;
13+
14+
use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommand;
15+
use BitBag\SyliusProductBundlePlugin\Factory\AddProductBundleItemToCartCommandFactoryInterface;
16+
use BitBag\SyliusProductBundlePlugin\Repository\ProductBundleItemRepositoryInterface;
17+
use Doctrine\Common\Collections\ArrayCollection;
18+
use Doctrine\Common\Collections\Collection;
19+
20+
final class AddProductBundleItemToCartCommandProvider implements AddProductBundleItemToCartCommandProviderInterface
21+
{
22+
public function __construct(
23+
private readonly AddProductBundleItemToCartCommandFactoryInterface $addProductBundleItemToCartCommandFactory,
24+
private readonly ProductBundleItemRepositoryInterface $productBundleItemRepository,
25+
) {
26+
}
27+
28+
/** @return Collection<int, AddProductBundleItemToCartCommand> */
29+
public function provide(string $bundleCode, array $variantCodes): Collection
30+
{
31+
$bundleItems = $this->productBundleItemRepository->findByBundleCode($bundleCode);
32+
33+
$items = '';
34+
foreach ($bundleItems as $bundleItem) {
35+
$items .= $bundleItem->getProductVariant()->getCode() . ', ';
36+
}
37+
38+
$commands = [];
39+
foreach ($bundleItems as $bundleItem) {
40+
$command = $this->addProductBundleItemToCartCommandFactory->createNew($bundleItem);
41+
//TODO implement overwritting bundle's variants with variants provided in API call
42+
}
43+
}
44+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusProductBundlePlugin\Provider;
13+
14+
use BitBag\SyliusProductBundlePlugin\Command\AddProductBundleItemToCartCommand;
15+
use Doctrine\Common\Collections\Collection;
16+
17+
interface AddProductBundleItemToCartCommandProviderInterface
18+
{
19+
/** @return Collection<int, AddProductBundleItemToCartCommand> */
20+
public function provide(string $bundleCode, array $variantCodes): Collection;
21+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusProductBundlePlugin\Repository;
13+
14+
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItemInterface;
15+
use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
16+
17+
class ProductBundleItemRepository extends EntityRepository implements ProductBundleItemRepositoryInterface
18+
{
19+
/** @return ProductBundleItemInterface[] */
20+
public function findByBundleCode(string $bundleCode): array
21+
{
22+
return $this
23+
->createQueryBuilder('pbi')
24+
->leftJoin('pbi.productBundle', 'pb')
25+
->leftJoin('pb.product', 'p')
26+
->where('p.code = :code')
27+
->setParameter('code', $bundleCode)
28+
->getQuery()
29+
->getResult();
30+
}
31+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusProductBundlePlugin\Repository;
13+
14+
use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleItemInterface;
15+
use Sylius\Component\Resource\Repository\RepositoryInterface;
16+
17+
interface ProductBundleItemRepositoryInterface extends RepositoryInterface
18+
{
19+
/** @return ProductBundleItemInterface[] */
20+
public function findByBundleCode(string $bundleCode): array;
21+
}

0 commit comments

Comments
 (0)