Skip to content

Commit

Permalink
[TASK] Switch hooks to PSR-14 events (#469)
Browse files Browse the repository at this point in the history
* [TASK] Replace showCartActionAfterCartWasLoaded Hook with BeforeShowCartEvent

Add BeforeShowCartEvent to CartPreviewController as well.

Add some more Events in SessionHandler:

- BeforeWriteAddressEvent,
- AfterRestoreAddressEvent,
- BeforeWriteCartEvent, and
- AfterRestoreCartEvent

* TASK: Replace MailAttachmentHook with AttachmentEvent

* TASK: Add functional test for AttachmentEvent listeners

* [TASK] Remove changeVariantDiscount from Domain\Model\Cart\BeVariant

* [CLEANUP] Use PHP InvalidArgumentException

* [TASK] Replace AddToCartFinisher hook with AddToCartFinisherEvent

* [BUGFIX] Fix typo in documentation

* [BUGFIX] Rename variable

* [BUGFIX] Remove assignments after dispatching event

* [CLEANUP] Rename settings variable

* [CLEANUP] Remove event from service configuration

According to the feature #94345 "Auto-detect event types" the configuration
for the event has been removed.

* [TASK] Add BillingAddress and ShippingAddress to BeforeShowCartEvent

* [BUGFIX] Check for attachDocuments in TypoScript is true

Closes: #452
  • Loading branch information
extcode authored May 24, 2024
1 parent accba7d commit 39c6455
Show file tree
Hide file tree
Showing 35 changed files with 865 additions and 365 deletions.
28 changes: 5 additions & 23 deletions Classes/Controller/Cart/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
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\BeforeShowCartEvent;
use Extcode\Cart\Event\CheckProductAvailabilityEvent;
use http\Exception\InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\ExtbaseRequestParameters;
Expand All @@ -42,7 +42,7 @@ protected function initializeView(ViewInterface $view): void
}

if ($currentStep > $steps) {
throw new InvalidArgumentException();
throw new \InvalidArgumentException();
}
$view->setStep($currentStep);

Expand Down Expand Up @@ -101,32 +101,14 @@ 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);
}
}
}

$this->view->assign('cart', $this->cart);
$beforeShowCartEvent = new BeforeShowCartEvent($this->cart, $orderItem, $billingAddress, $shippingAddress);
$this->eventDispatcher->dispatch($beforeShowCartEvent);

$this->parseServicesAndAssignToView();

$this->view->assignMultiple(
[
'cart' => $this->cart,
'orderItem' => $orderItem,
'billingAddress' => $billingAddress,
'shippingAddress' => $shippingAddress,
Expand Down
6 changes: 6 additions & 0 deletions Classes/Controller/Cart/CartPreviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

use Extcode\Cart\Event\Cart\BeforeShowCartEvent;
use Psr\Http\Message\ResponseInterface;

class CartPreviewController extends ActionController
{
public function showAction(): ResponseInterface
{
$this->restoreSession();

$beforeShowCartEvent = new BeforeShowCartEvent($this->cart);
$this->eventDispatcher->dispatch($beforeShowCartEvent);

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

return $this->htmlResponse();
Expand Down
32 changes: 19 additions & 13 deletions Classes/Domain/Finisher/Form/AddToCartFinisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,26 @@
*/
use Extcode\Cart\Domain\Model\Cart\Cart;
use Extcode\Cart\Domain\Model\Cart\Product;
use Extcode\Cart\Event\Form\AddToCartFinisherEvent;
use Extcode\Cart\Service\SessionHandler;
use Extcode\Cart\Utility\CartUtility;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Core\Http\PropagateResponseException;
use TYPO3\CMS\Core\Http\StreamFactory;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Messaging\FlashMessageService;
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Extbase\Service\ExtensionService;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher;

class AddToCartFinisher extends AbstractFinisher
{
protected ConfigurationManager $configurationManager;
protected ConfigurationManagerInterface $configurationManager;

protected EventDispatcherInterface $eventDispatcher;

protected SessionHandler $sessionHandler;

Expand All @@ -37,11 +41,11 @@ class AddToCartFinisher extends AbstractFinisher

protected array $configurations;

public function injectConfigurationManager(ConfigurationManager $configurationManager): void
public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager): void
{
$this->configurationManager = $configurationManager;
$this->configurations = $this->configurationManager->getConfiguration(
ConfigurationManager::CONFIGURATION_TYPE_FRAMEWORK,
ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK,
'Cart'
);
}
Expand All @@ -56,6 +60,11 @@ public function injectCartUtility(CartUtility $cartUtility): void
$this->cartUtility = $cartUtility;
}

public function injectEventDispatcher(EventDispatcherInterface $eventDispatcher): void
{
$this->eventDispatcher = $eventDispatcher;
}

protected function executeInternal(): ?string
{
$cart = $this->sessionHandler->restoreCart($this->configurations['settings']['cart']['pid']);
Expand All @@ -67,18 +76,15 @@ protected function executeInternal(): ?string

$formValues = $this->getFormValues();

$className = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['cart'][$formValues['productType']]['Form']['AddToCartFinisher'];
$hookObject = GeneralUtility::makeInstance($className);
if (!$hookObject instanceof AddToCartFinisherInterface) {
throw new \UnexpectedValueException($className . ' must implement interface ' . AddToCartFinisherInterface::class, 123);
}

unset($formValues[$this->getHoneypotIdentifier()]);

[$errors, $cartProducts] = $hookObject->getProductFromForm(
$addToCartFinisherEvent = new AddToCartFinisherEvent(
$formValues,
$this->cart
);
$this->eventDispatcher->dispatch($addToCartFinisherEvent);
$errors = $addToCartFinisherEvent->getErrors();
$cartProducts = $addToCartFinisherEvent->getCartProducts();

unset($formValues[$this->getHoneypotIdentifier()]);

if (empty($errors)) {
$quantity = $this->addProductsToCart($cartProducts);
Expand Down
135 changes: 28 additions & 107 deletions Classes/Domain/Model/Cart/BeVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
* LICENSE file that was distributed with this source code.
*/

use TYPO3\CMS\Core\Utility\GeneralUtility;
use Psr\EventDispatcher\EventDispatcherInterface;

class BeVariant
{
private ?EventDispatcherInterface $eventDispatcher = null;

protected string $id = '';

protected ?Product $product = null;
Expand Down Expand Up @@ -147,15 +149,14 @@ public function setParentBeVariant(self $parentBeVariant): void

public function isNetPrice(): bool
{
$isNetPrice = false;

if ($this->getParentBeVariant()) {
$isNetPrice = $this->getParentBeVariant()->isNetPrice();
} elseif ($this->getProduct()) {
$isNetPrice = $this->getProduct()->isNetPrice();
return $this->getParentBeVariant()->isNetPrice();
}
if ($this->getProduct()) {
return $this->getProduct()->isNetPrice();
}

return $isNetPrice;
return false;
}

public function getId(): string
Expand Down Expand Up @@ -252,9 +253,7 @@ public function getBestPrice(): float

public function getDiscount(): float
{
$discount = $this->getPriceCalculated() - $this->getBestPriceCalculated();

return $discount;
return $this->getPriceCalculated() - $this->getBestPriceCalculated();
}

public function getSpecialPriceDiscount(): float
Expand All @@ -278,106 +277,31 @@ public function getPriceCalculated(): float
$parentPrice = 0;
}

switch ($this->priceCalcMethod) {
case 3:
$calc_price = -1 * (($price / 100) * ($parentPrice));
break;
case 5:
$calc_price = ($price / 100) * ($parentPrice);
break;
default:
$calc_price = 0;
if ($this->priceCalcMethod === 0) {
return $parentPrice;
}

if (
isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['cart']['changeVariantDiscount']) &&
is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['cart']['changeVariantDiscount'])
) {
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['cart']['changeVariantDiscount'] as $funcRef) {
if ($funcRef) {
$params = [
'price_calc_method' => $this->priceCalcMethod,
'price' => &$price,
'parent_price' => &$parentPrice,
'calc_price' => &$calc_price,
];

GeneralUtility::callUserFunction($funcRef, $params, $this);
}
}
if ($this->priceCalcMethod === 1) {
return $price;
}

switch ($this->priceCalcMethod) {
case 1:
$parentPrice = 0.0;
break;
case 2:
$price = -1 * $price;
break;
case 4:
break;
default:
$price = 0.0;
if ($this->priceCalcMethod === 2) {
return $parentPrice - $price;
}

return $parentPrice + $price + $calc_price;
}

public function getBestPriceCalculated(): float
{
$price = $this->getBestPrice();

if ($this->getParentBeVariant()) {
$parentPrice = $this->getParentBeVariant()->getBestPrice();
} elseif ($this->getProduct()) {
$parentPrice = $this->getProduct()->getBestPrice($this->getQuantity());
} else {
$parentPrice = 0;
if ($this->priceCalcMethod === 3) {
return $parentPrice - (($price / 100) * $parentPrice);
}

switch ($this->priceCalcMethod) {
case 3:
$calc_price = -1 * (($price / 100) * ($parentPrice));
break;
case 5:
$calc_price = ($price / 100) * ($parentPrice);
break;
default:
$calc_price = 0;
if ($this->priceCalcMethod === 4) {
return $parentPrice + $price;
}

if (
isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['cart']['changeVariantDiscount']) &&
is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['cart']['changeVariantDiscount'])
) {
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['cart']['changeVariantDiscount'] as $funcRef) {
if ($funcRef) {
$params = [
'price_calc_method' => $this->priceCalcMethod,
'price' => &$price,
'parent_price' => &$parentPrice,
'calc_price' => &$calc_price,
];

GeneralUtility::callUserFunction($funcRef, $params, $this);
}
}
if ($this->priceCalcMethod === 5) {
return $parentPrice + (($price / 100) * $parentPrice);
}

switch ($this->priceCalcMethod) {
case 1:
$parentPrice = 0.0;
break;
case 2:
$price = -1 * $price;
break;
case 4:
break;
default:
$price = 0.0;
}
throw new \InvalidArgumentException('Unkonwn price calc method', 1711969492);
}

return $parentPrice + $price + $calc_price;
public function getBestPriceCalculated(): float
{
return $this->getPriceCalculated();
}

public function getParentPrice(): float
Expand Down Expand Up @@ -554,11 +478,10 @@ public function changeVariantsQuantity(array $variantQuantityArray): void

if (is_array($quantity)) {
$beVariant->changeVariantsQuantity($quantity);
$this->reCalc();
} else {
$beVariant->changeQuantity($quantity);
$this->reCalc();
}
$this->reCalc();
}
}

Expand Down Expand Up @@ -618,12 +541,10 @@ public function removeBeVariants(array $beVariantsArray)
unset($this->beVariants[$beVariantId]);
}

$this->reCalc();
} else {
unset($this->beVariants[$beVariantId]);

$this->reCalc();
}
$this->reCalc();
} else {
return -1;
}
Expand Down
Loading

0 comments on commit 39c6455

Please sign in to comment.