Skip to content

Commit

Permalink
Merge pull request #31 from Glancu/feature/product-bundle-edit-product
Browse files Browse the repository at this point in the history
Feature: edit product bundle product
  • Loading branch information
senghe authored Nov 12, 2023
2 parents 093c6d1 + 4af02a9 commit 0f379f0
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 17 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
"Tests\\BitBag\\SyliusProductBundlePlugin\\": [
"tests/",
"tests/Application/src"
]
],
"spec\\BitBag\\SyliusProductBundlePlugin\\": "spec/"
},
"classmap": ["tests/Application/Kernel.php"]
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace spec\BitBag\SyliusProductBundlePlugin\EventListener;

use BitBag\SyliusProductBundlePlugin\Entity\ProductBundleInterface;
use BitBag\SyliusProductBundlePlugin\Entity\ProductInterface;
use BitBag\SyliusProductBundlePlugin\EventListener\AddProductToProductBundleWhenEditNormalProductEventListener;
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;

final class AddProductToProductBundleWhenEditNormalProductEventListenerSpec extends ObjectBehavior
{
function it_is_initializable(): void
{
$this->shouldHaveType(AddProductToProductBundleWhenEditNormalProductEventListener::class);
}

public function it_should_add_product_to_product_bundle_if_not_exist_on_pre_create_and_update_event(
ResourceControllerEvent $resourceControllerEvent,
ProductInterface $product,
ProductBundleInterface $productBundle
): void {
$resourceControllerEvent->getSubject()->willReturn($product);

$product->getProductBundle()->shouldBeCalled();

$product->getProductBundle()->willReturn($productBundle);

$this->addProductToProductBundle($resourceControllerEvent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace BitBag\SyliusProductBundlePlugin\EventListener;

use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
use Tests\BitBag\SyliusProductBundlePlugin\Entity\Product;

final class AddProductToProductBundleWhenEditNormalProductEventListener
{
public function addProductToProductBundle(ResourceControllerEvent $resourceControllerEvent): void
{
/** @var Product $product */
$product = $resourceControllerEvent->getSubject();
if (null !== $product->getProductBundle() && null === $product->getProductBundle()->getProduct()) {
$product->getProductBundle()->setProduct($product);
}
}
}
8 changes: 0 additions & 8 deletions src/Form/Extension/ProductTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace BitBag\SyliusProductBundlePlugin\Form\Extension;

use BitBag\SyliusProductBundlePlugin\Entity\ProductInterface;
use BitBag\SyliusProductBundlePlugin\Form\Type\ProductBundleType;
use Sylius\Bundle\ProductBundle\Form\Type\ProductType;
use Symfony\Component\Form\AbstractTypeExtension;
Expand All @@ -21,13 +20,6 @@ final class ProductTypeExtension extends AbstractTypeExtension
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
/** @var ProductInterface $product */
$product = $builder->getData();

if (!$product->isBundle()) {
return;
}

$builder
->add('productBundle', ProductBundleType::class, [
'label' => false,
Expand Down
8 changes: 0 additions & 8 deletions src/Menu/AdminProductFormMenuListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,12 @@

namespace BitBag\SyliusProductBundlePlugin\Menu;

use BitBag\SyliusProductBundlePlugin\Entity\ProductInterface;
use Sylius\Bundle\AdminBundle\Event\ProductMenuBuilderEvent;

final class AdminProductFormMenuListener
{
public function addItems(ProductMenuBuilderEvent $event): void
{
/** @var ProductInterface $product */
$product = $event->getProduct();

if (!$product->isBundle()) {
return;
}

$menu = $event->getMenu();

$menu
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
<import resource="services/transformer.xml"/>
<import resource="services/twig.xml"/>
<import resource="services/validator.xml"/>
<import resource="services/event_listener.xml"/>
</imports>
</container>
10 changes: 10 additions & 0 deletions src/Resources/config/services/event_listener.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?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_plugin.request_listener.add_product_to_product_bundle_when_edit_normal_product" class="BitBag\SyliusProductBundlePlugin\EventListener\AddProductToProductBundleWhenEditNormalProductEventListener">
<tag name="kernel.event_listener" event="sylius.product.pre_update" method="addProductToProductBundle" />
<tag name="kernel.event_listener" event="sylius.product.pre_create" method="addProductToProductBundle" />
</service>
</services>
</container>

0 comments on commit 0f379f0

Please sign in to comment.