<?php
declare(strict_types=1);
namespace Lnb\Shopware6\FraudPrevention\Subscriber;
use Lnb\Shopware6\FraudPrevention\Installer\StateInstaller;
use Shopware\Core\Checkout\Order\Event\OrderStateMachineStateChangeEvent;
use Shopware\Core\Content\Flow\Dispatching\Action\FlowAction;
use Shopware\Core\Content\Flow\Dispatching\Action\SetOrderStateAction;
use Shopware\Core\Content\Flow\Dispatching\DelayableAction;
use Shopware\Core\Content\Flow\Dispatching\StorableFlow;
use Shopware\Core\Framework\Event\FlowEvent;
use Shopware\Core\Framework\Event\OrderAware;
use Shopware\Core\Framework\Feature;
class SetOrderStateActionDecorator extends FlowAction implements DelayableAction
{
public function __construct(
private readonly SetOrderStateAction $decorated
) {
}
public static function getName(): string
{
return SetOrderStateAction::getName();
}
public function getDecorated(): SetOrderStateAction
{
return $this->decorated;
}
/**
* @deprecated tag:v6.5.0 Will be removed, implement handleFlow instead
*/
public function handle(FlowEvent $event): void
{
$flowEvent = $event->getFlowState()->event;
if (! $flowEvent instanceof OrderStateMachineStateChangeEvent) {
$this->getDecorated()->handle($event);
}
$event = $this->parseFraudAwareness($event);
$this->getDecorated()->handle($event);
}
public function requirements(): array
{
return [OrderAware::class];
}
public static function getSubscribedEvents(): array
{
if (Feature::isActive('v6.5.0.0')) {
return [];
}
return [
self::getName() => 'handle',
];
}
public function handleFlow(StorableFlow $flow): void
{
Feature::triggerDeprecationOrThrow(
'v6.5.0.0',
Feature::deprecatedMethodMessage(__CLASS__, __METHOD__, 'v6.5.0.0')
);
$this->getDecorated()->handleFlow($flow);
}
/**
* @deprecated tag:v6.5.0 Will be invalid because StorableFlow will be used instead of FlowEvent
*/
private function parseFraudAwareness(FlowEvent $event): FlowEvent
{
$flowEvent = $event->getFlowState()->event;
if (! $flowEvent instanceof OrderStateMachineStateChangeEvent) {
return $event;
}
$config = $event->getConfig();
if (! isset($config['fraud_prevention_aware']) || $config['fraud_prevention_aware'] !== true) {
return $event;
}
$order = $flowEvent->getOrder();
if ($order->getStateMachineState()?->getTechnicalName() !== StateInstaller::FRAUD_PREVENTED_STATE_TECHNICAL_NAME) {
return $event;
}
//order is set to fraud -> so prevent new order state
$newConfig = $event->getConfig();
$newConfig['order'] = '';
return new FlowEvent(
$event->getActionName(),
$event->getFlowState(),
$newConfig
);
}
}