Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Replace hooks with PSR-14 events #467

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 14 additions & 22 deletions Classes/Controller/Cart/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Extcode\Cart\Domain\Model\Order\Item;
use Extcode\Cart\Domain\Model\Order\ShippingAddress;
use Extcode\Cart\Event\CheckProductAvailabilityEvent;
use Extcode\Cart\Event\ShowCartEvent;
use http\Exception\InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
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/ShowCartEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Extcode\Cart\Event;

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;
}
}
33 changes: 33 additions & 0 deletions Classes/Event/ShowCartEventInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Extcode\Cart\Event;

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
Loading