Skip to content

Commit

Permalink
WIP [TASK] Replace hooks with PSR-14 events
Browse files Browse the repository at this point in the history
Replace existing hooks with PSR-14 events

Todo:
* Replace missing hooks
  • Loading branch information
rintisch committed Mar 28, 2024
1 parent 1ed3556 commit 33ce150
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 22 deletions.
36 changes: 14 additions & 22 deletions Classes/Controller/Cart/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Extcode\Cart\Domain\Model\Order\BillingAddress;
use Extcode\Cart\Domain\Model\Order\Item;
use Extcode\Cart\Domain\Model\Order\ShippingAddress;
use Extcode\Cart\Event\Cart\ShowCartEvent;
use Extcode\Cart\Event\CheckProductAvailabilityEvent;
use http\Exception\InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -56,10 +57,11 @@ protected function initializeView(ViewInterface $view): void
}

public function showAction(
Item $orderItem = null,
BillingAddress $billingAddress = null,
Item $orderItem = null,
BillingAddress $billingAddress = null,
ShippingAddress $shippingAddress = null
): ResponseInterface {
): ResponseInterface
{
$this->restoreSession();

if (!is_null($billingAddress)) {
Expand Down Expand Up @@ -101,25 +103,15 @@ public function showAction(
$this->sessionHandler->writeCart($this->settings['cart']['pid'], $this->cart);
}

if (
isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['cart']['showCartActionAfterCartWasLoaded']) &&
!empty($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['cart']['showCartActionAfterCartWasLoaded'])
) {
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['cart']['showCartActionAfterCartWasLoaded'] as $funcRef) {
if ($funcRef) {
$params = [
'request' => $this->request,
'settings' => $this->settings,
'cart' => &$this->cart,
'orderItem' => &$orderItem,
'billingAddress' => &$billingAddress,
'shippingAddress' => &$shippingAddress,
];

GeneralUtility::callUserFunction($funcRef, $params, $this);
}
}
}
$showCartEvent = new ShowCartEvent(
$this->cart,
$this->request,
$this->settings,
$orderItem,
$billingAddress,
$shippingAddress
);
$this->eventDispatcher->dispatch($showCartEvent);

$this->view->assign('cart', $this->cart);

Expand Down
66 changes: 66 additions & 0 deletions Classes/Event/Cart/ShowCartEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Extcode\Cart\Event\Cart;

use Extcode\Cart\Domain\Model\Cart\Cart;
use Extcode\Cart\Domain\Model\Order\BillingAddress;
use Extcode\Cart\Domain\Model\Order\Item;
use Extcode\Cart\Domain\Model\Order\ShippingAddress;
use TYPO3\CMS\Extbase\Mvc\Request;

final class ShowCartEvent implements ShowCartEventInterface
{
public function __construct(
private Cart $cart,
private readonly Request $request,
private readonly array $settings,
private Item $orderItem,
private BillingAddress $billingAddress,
private ShippingAddress $shippingAddress
) {}

public function getCart(): Cart
{
return $this->cart;
}

public function setCart(Cart $cart): void
{
$this->cart = $cart;
}

public function getRequest(): Request
{
return $this->request;
}

public function getSettings(): array
{
return $this->settings;
}

public function getOrderItem(): Item
{
return $this->orderItem;
}
public function setOrderItem(Item $orderItem): void
{
$this->orderItem = $orderItem;
}
public function getBillingAddress(): BillingAddress
{
return $this->billingAddress;
}
public function setBillingAddress(BillingAddress $billingAddress): void
{
$this->billingAddress = $billingAddress;
}
public function getShippingAddress(): ShippingAddress
{
return $this->shippingAddress;
}
public function setShippingAddress(ShippingAddress $shippingAddress): void
{
$this->shippingAddress = $shippingAddress;
}
}
35 changes: 35 additions & 0 deletions Classes/Event/Cart/ShowCartEventInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Extcode\Cart\Event\Cart;

use Extcode\Cart\Domain\Model\Cart\Cart;
use Extcode\Cart\Domain\Model\Order\BillingAddress;
use Extcode\Cart\Domain\Model\Order\Item;
use Extcode\Cart\Domain\Model\Order\ShippingAddress;
use TYPO3\CMS\Extbase\Mvc\Request;

interface ShowCartEventInterface
{
public function __construct(
Cart $cart,
Request $request,
array $settings,
Item $orderItem,
BillingAddress $billingAddress,
ShippingAddress $shippingAddress
);

public function getCart(): Cart;

public function getRequest(): Request;

public function getSettings(): array;

public function getOrderItem(): Item;

public function getBillingAddress(): BillingAddress;

public function getShippingAddress(): ShippingAddress;


}
29 changes: 29 additions & 0 deletions Documentation/Changelog/9.0/Breaking-452-RemoveHooks.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.. include:: ../../Includes.txt

=============================
Breaking: #452 - Remove Hooks
=============================

See :issue:`452`

Description
===========

Existing Hooks have been removed. They were replaced by EventListeners.
The extension offered following hooks:

* `showCartActionAfterCartWasLoaded` in `\Extcode\Cart\Controller\Cart\CartController`

Affected Installations
======================

All installations that used the hooks to programmatically adjust the behavior
of the extension are affected.

Migration
=========

* `showCartActionAfterCartWasLoaded` in `\Extcode\Cart\Controller\Cart\CartController`
now needs to listen to the event `\Extcode\Cart\Event\Cart\ShowCartEvent`

.. index:: API
8 changes: 8 additions & 0 deletions Documentation/Developer/Events/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ integrate custom requirements in the ordering process.
You can register your own EventListener for the following
events:

.. confval:: \Extcode\Cart\Event\Cart\ShowCartEvent

Triggered before the cart is shown.

Allows adaptions of the cart itself. Furthermore it makes it possible to
fill the form fields for the billing address and the shipping address with
data from a logged-in frontend user.

.. confval:: \Extcode\Cart\Event\Cart\UpdateCountryEvent

This event is triggered when the user changes the country in the order
Expand Down

0 comments on commit 33ce150

Please sign in to comment.