Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

Commit

Permalink
Fix dispatch event to work with contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
wbloszyk committed Jun 16, 2020
1 parent f859369 commit dfe345b
Show file tree
Hide file tree
Showing 17 changed files with 40 additions and 39 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"symfony/dependency-injection": "^4.3",
"symfony/doctrine-bridge": "^4.3",
"symfony/event-dispatcher": "^4.3",
"symfony/event-dispatcher-contracts": "^1.1 || ^2.0",
"symfony/filesystem": "^4.3",
"symfony/form": "^4.3",
"symfony/framework-bundle": "^4.3",
Expand Down
16 changes: 8 additions & 8 deletions src/Component/Payment/PaymentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
use Sonata\Component\Order\OrderInterface;
use Sonata\Component\Order\OrderManagerInterface;
use Sonata\NotificationBundle\Backend\BackendInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* Responsible for interactions between PaymentController & Model.
Expand Down Expand Up @@ -78,7 +78,7 @@ public function handleError(Request $request, BasketInterface $basket)
$order = $this->getValidOrder($transaction);

$event = new PaymentEvent($order, $transaction);
$this->getEventDispatcher()->dispatch(PaymentEvents::PRE_ERROR, $event);
$this->getEventDispatcher()->dispatch($event, PaymentEvents::PRE_ERROR);

if ($order->isCancellable()) {
$order->setStatus(OrderInterface::STATUS_STOPPED);
Expand All @@ -95,7 +95,7 @@ public function handleError(Request $request, BasketInterface $basket)
$this->getPayment($transaction->getPaymentCode())->getTransformer('order')->transformIntoBasket($order, $basket);

$event = new PaymentEvent($order, $transaction);
$this->getEventDispatcher()->dispatch(PaymentEvents::POST_ERROR, $event);
$this->getEventDispatcher()->dispatch($event, PaymentEvents::POST_ERROR);

$this->notificationBackend->createAndPublish('sonata_payment_order_process', [
'order_id' => $order->getId(),
Expand All @@ -111,7 +111,7 @@ public function handleConfirmation(Request $request)
$order = $this->getValidOrder($transaction);

$event = new PaymentEvent($order, $transaction);
$this->getEventDispatcher()->dispatch(PaymentEvents::CONFIRMATION, $event);
$this->getEventDispatcher()->dispatch($event, PaymentEvents::CONFIRMATION);

return $order;
}
Expand All @@ -121,7 +121,7 @@ public function getSendbankOrder(BasketInterface $basket)
$order = $basket->getPaymentMethod()->getTransformer('basket')->transformIntoOrder($basket);

$event = new PaymentEvent($order);
$this->getEventDispatcher()->dispatch(PaymentEvents::PRE_SENDBANK, $event);
$this->getEventDispatcher()->dispatch($event, PaymentEvents::PRE_SENDBANK);

// save the order
$this->orderManager->save($order);
Expand All @@ -130,7 +130,7 @@ public function getSendbankOrder(BasketInterface $basket)
$this->referenceGenerator->order($order);

$event = new PaymentEvent($order);
$this->getEventDispatcher()->dispatch(PaymentEvents::POST_SENDBANK, $event);
$this->getEventDispatcher()->dispatch($event, PaymentEvents::POST_SENDBANK);

return $order;
}
Expand All @@ -142,7 +142,7 @@ public function getPaymentCallbackResponse(Request $request)
$order = $this->getValidOrder($transaction);

$event = new PaymentEvent($order, $transaction);
$this->getEventDispatcher()->dispatch(PaymentEvents::PRE_CALLBACK, $event);
$this->getEventDispatcher()->dispatch($event, PaymentEvents::PRE_CALLBACK);

// start the payment callback
$response = $this->getPayment($transaction->getPaymentCode())->callback($transaction);
Expand All @@ -151,7 +151,7 @@ public function getPaymentCallbackResponse(Request $request)
$this->orderManager->save($order);

$event = new PaymentEvent($order, $transaction, $response);
$this->getEventDispatcher()->dispatch(PaymentEvents::POST_CALLBACK, $event);
$this->getEventDispatcher()->dispatch($event, PaymentEvents::POST_CALLBACK);

$this->notificationBackend->createAndPublish('sonata_payment_order_process', [
'order_id' => $order->getId(),
Expand Down
8 changes: 4 additions & 4 deletions src/Component/Transformer/BasketTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use Sonata\Component\Payment\PaymentInterface;
use Sonata\Component\Payment\TransactionInterface;
use Sonata\Component\Product\Pool as ProductPool;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class BasketTransformer extends BaseTransformer
{
Expand All @@ -47,7 +47,7 @@ class BasketTransformer extends BaseTransformer
protected $productPool;

/**
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
* @var EventDispatcherInterface
*/
protected $eventDispatcher;

Expand All @@ -74,7 +74,7 @@ public function __construct(OrderManagerInterface $orderManager, ProductPool $pr
public function transformIntoOrder(BasketInterface $basket)
{
$event = new BasketTransformEvent($basket);
$this->eventDispatcher->dispatch(TransformerEvents::PRE_BASKET_TO_ORDER_TRANSFORM, $event);
$this->eventDispatcher->dispatch($event, TransformerEvents::PRE_BASKET_TO_ORDER_TRANSFORM);

// Customer
$customer = $basket->getCustomer();
Expand Down Expand Up @@ -181,7 +181,7 @@ public function transformIntoOrder(BasketInterface $basket)
}

$event = new OrderTransformEvent($order);
$this->eventDispatcher->dispatch(TransformerEvents::POST_BASKET_TO_ORDER_TRANSFORM, $event);
$this->eventDispatcher->dispatch($event, TransformerEvents::POST_BASKET_TO_ORDER_TRANSFORM);

return $order;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Component/Transformer/InvoiceTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use Sonata\Component\Invoice\InvoiceInterface;
use Sonata\Component\Order\OrderElementInterface;
use Sonata\Component\Order\OrderInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* @author Hugo Briand <briand@ekino.com>
Expand All @@ -40,7 +40,7 @@ class InvoiceTransformer extends BaseTransformer
protected $deliveryPool;

/**
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
* @var EventDispatcherInterface
*/
protected $eventDispatcher;

Expand All @@ -61,7 +61,7 @@ public function __construct(InvoiceElementManagerInterface $invoiceElementManage
public function transformFromOrder(OrderInterface $order, InvoiceInterface $invoice): void
{
$event = new OrderTransformEvent($order);
$this->eventDispatcher->dispatch(TransformerEvents::PRE_ORDER_TO_INVOICE_TRANSFORM, $event);
$this->eventDispatcher->dispatch($event, TransformerEvents::PRE_ORDER_TO_INVOICE_TRANSFORM);

$invoice->setName($order->getBillingName());
$invoice->setAddress1($order->getBillingAddress1());
Expand Down Expand Up @@ -99,7 +99,7 @@ public function transformFromOrder(OrderInterface $order, InvoiceInterface $invo
$invoice->setStatus(InvoiceInterface::STATUS_OPEN);

$event = new InvoiceTransformEvent($invoice);
$this->eventDispatcher->dispatch(TransformerEvents::POST_ORDER_TO_INVOICE_TRANSFORM, $event);
$this->eventDispatcher->dispatch($event, TransformerEvents::POST_ORDER_TO_INVOICE_TRANSFORM);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Component/Transformer/OrderTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Sonata\Component\Order\OrderElementInterface;
use Sonata\Component\Order\OrderInterface;
use Sonata\Component\Product\Pool as ProductPool;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class OrderTransformer extends BaseTransformer
{
Expand Down Expand Up @@ -51,7 +51,7 @@ public function __construct(ProductPool $productPool, EventDispatcherInterface $
public function transformIntoBasket(OrderInterface $order, BasketInterface $basket)
{
$event = new OrderTransformEvent($order);
$this->eventDispatcher->dispatch(TransformerEvents::PRE_ORDER_TO_BASKET_TRANSFORM, $event);
$this->eventDispatcher->dispatch($event, TransformerEvents::PRE_ORDER_TO_BASKET_TRANSFORM);

// we reset the current basket
$basket->reset(true);
Expand Down Expand Up @@ -84,7 +84,7 @@ public function transformIntoBasket(OrderInterface $order, BasketInterface $bask
$basket->buildPrices();

$event = new BasketTransformEvent($basket);
$this->eventDispatcher->dispatch(TransformerEvents::POST_ORDER_TO_BASKET_TRANSFORM, $event);
$this->eventDispatcher->dispatch($event, TransformerEvents::POST_ORDER_TO_BASKET_TRANSFORM);

return $basket;
}
Expand Down
4 changes: 2 additions & 2 deletions src/CustomerBundle/Menu/ProfileMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* @author Hugo Briand <briand@ekino.com>
Expand Down Expand Up @@ -79,6 +79,6 @@ public function buildProfileMenu(ItemInterface $menu, array $itemOptions = []):
}

$event = new ProfileMenuEvent($menu);
$this->eventDispatcher->dispatch('sonata.customer.profile.configure_menu', $event);
$this->eventDispatcher->dispatch($event, 'sonata.customer.profile.configure_menu');
}
}
16 changes: 8 additions & 8 deletions src/ProductBundle/Model/BaseProductProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@
use Sonata\Component\Product\ProductProviderInterface;
use Sonata\Form\Validator\ErrorElement;
use Sonata\FormatterBundle\Form\Type\FormatterType;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

abstract class BaseProductProvider implements ProductProviderInterface
{
Expand Down Expand Up @@ -111,7 +111,7 @@ public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): v
}

/**
* @return \Symfony\Component\EventDispatcher\EventDispatcherInterface
* @return \Symfony\Contracts\EventDispatcher\EventDispatcherInterface
*/
public function getEventDispatcher()
{
Expand Down Expand Up @@ -807,7 +807,7 @@ public function validateFormBasketElement(ErrorElement $errorElement, BasketElem
public function basketAddProduct(BasketInterface $basket, ProductInterface $product, BasketElementInterface $basketElement)
{
$event = new AddBasketElementEvent($basket, $basketElement, $product, $this);
$this->getEventDispatcher()->dispatch(BasketEvents::PRE_ADD_PRODUCT, $event);
$this->getEventDispatcher()->dispatch($event, BasketEvents::PRE_ADD_PRODUCT);

if ($basket->hasProduct($product)) {
return false;
Expand All @@ -830,7 +830,7 @@ public function basketAddProduct(BasketInterface $basket, ProductInterface $prod
$basket->addBasketElement($basketElement);

$event = new AddBasketElementEvent($basket, $basketElement, $product, $this);
$this->getEventDispatcher()->dispatch(BasketEvents::POST_ADD_PRODUCT, $event);
$this->getEventDispatcher()->dispatch($event, BasketEvents::POST_ADD_PRODUCT);

return $event->getBasketElement();
}
Expand All @@ -845,7 +845,7 @@ public function basketAddProduct(BasketInterface $basket, ProductInterface $prod
public function basketMergeProduct(BasketInterface $basket, ProductInterface $product, BasketElementInterface $newBasketElement)
{
$event = new AddBasketElementEvent($basket, $newBasketElement, $product, $this);
$this->getEventDispatcher()->dispatch(BasketEvents::PRE_MERGE_PRODUCT, $event);
$this->getEventDispatcher()->dispatch($event, BasketEvents::PRE_MERGE_PRODUCT);

if (!$basket->hasProduct($product)) {
return false;
Expand All @@ -861,7 +861,7 @@ public function basketMergeProduct(BasketInterface $basket, ProductInterface $pr
$this->updateComputationPricesFields($basket, $basketElement, $product);

$event = new AddBasketElementEvent($basket, $basketElement, $product, $this);
$this->getEventDispatcher()->dispatch(BasketEvents::POST_MERGE_PRODUCT, $event);
$this->getEventDispatcher()->dispatch($event, BasketEvents::POST_MERGE_PRODUCT);

return $event->getBasketElement();
}
Expand Down Expand Up @@ -894,7 +894,7 @@ public function updateComputationPricesFields(BasketInterface $basket, BasketEle
public function calculatePrice(ProductInterface $product, CurrencyInterface $currency, $vat = false, $quantity = 1)
{
$event = new BeforeCalculatePriceEvent($product, $currency, $vat, $quantity);
$this->getEventDispatcher()->dispatch(BasketEvents::PRE_CALCULATE_PRICE, $event);
$this->getEventDispatcher()->dispatch($event, BasketEvents::PRE_CALCULATE_PRICE);

$vat = $event->getVat();
$quantity = $event->getQuantity();
Expand All @@ -906,7 +906,7 @@ public function calculatePrice(ProductInterface $product, CurrencyInterface $cur
$price = (float) (bcmul((string) $this->currencyPriceCalculator->getPrice($product, $currency, $vat), (string) $quantity));

$afterEvent = new AfterCalculatePriceEvent($product, $currency, $vat, $quantity, $price);
$this->getEventDispatcher()->dispatch(BasketEvents::POST_CALCULATE_PRICE, $afterEvent);
$this->getEventDispatcher()->dispatch($afterEvent, BasketEvents::POST_CALCULATE_PRICE);

return $afterEvent->getPrice();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Component/Basket/BasketElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use Sonata\Component\Product\ProductInterface;
use Sonata\Component\Product\ProductManagerInterface;
use Sonata\Component\Product\ProductProviderInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class BasketElementTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Component/Basket/BasketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
use Sonata\Component\Product\ProductManagerInterface;
use Sonata\Component\Product\ProductProviderInterface;
use Sonata\Component\Tests\Product\Product;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class BasketTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Component/Payment/PaymentHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
use Sonata\NotificationBundle\Backend\BackendInterface;
use Sonata\NotificationBundle\Backend\RuntimeBackend;
use Sonata\PaymentBundle\Tests\Entity\Transaction;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* @author Hugo Briand <briand@ekino.com>
Expand Down
2 changes: 1 addition & 1 deletion tests/Component/Transformer/BasketTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use Sonata\Component\Product\Pool;
use Sonata\Component\Transformer\BasketTransformer;
use Sonata\OrderBundle\Entity\BaseOrder;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class BasketTransformerTest_Order extends BaseOrder
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Component/Transformer/InvoiceTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use Sonata\Component\Order\OrderElementInterface;
use Sonata\Component\Order\OrderInterface;
use Sonata\Component\Transformer\InvoiceTransformer;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class InvoiceTransformerTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Component/Transformer/OrderTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
use Sonata\Component\Product\ProductManagerInterface;
use Sonata\Component\Product\ProductProviderInterface;
use Sonata\Component\Transformer\OrderTransformer;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class OrderTransformerTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Component/Transformer/PoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Sonata\Component\Transformer\BasketTransformer;
use Sonata\Component\Transformer\OrderTransformer;
use Sonata\Component\Transformer\Pool as TransformerPool;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class PoolTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/CustomerBundle/Block/ProfileMenuBlockServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
use Sonata\CustomerBundle\Block\ProfileMenuBlockService;
use Sonata\CustomerBundle\Menu\ProfileMenuBuilder;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* @author Wojciech Błoszyk <wbloszyk@gmail.com>
Expand Down
2 changes: 1 addition & 1 deletion tests/CustomerBundle/Menu/ProfileMenuBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
use Knp\Menu\ItemInterface;
use PHPUnit\Framework\TestCase;
use Sonata\CustomerBundle\Menu\ProfileMenuBuilder;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* @author Hugo Briand <briand@ekino.com>
Expand Down
2 changes: 1 addition & 1 deletion tests/ProductBundle/Model/BaseProductProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use Sonata\Form\Validator\ErrorElement;
use Sonata\ProductBundle\Entity\BaseProduct;
use Sonata\ProductBundle\Model\BaseProductProvider;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class ProductProviderTest extends BaseProductProvider
{
Expand Down

0 comments on commit dfe345b

Please sign in to comment.