42 lines
1.3 KiB
PHP
42 lines
1.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service;
|
|
|
|
use App\Model\PurchaseFlow;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
/**
|
|
* Tells the login page which purchase flow, if any, it is the entry to.
|
|
*
|
|
* Both a booking and a voucher purchase can be pending in the same session, e.g. when they were
|
|
* started in two tabs. The one started last wins, which is what the visitor just clicked.
|
|
*/
|
|
class PurchaseFlowResolver
|
|
{
|
|
public const string LAST_STARTED_KEY = 'purchase_flow_last_started';
|
|
|
|
public function markStarted(Request $request, PurchaseFlow $flow): void
|
|
{
|
|
$request->getSession()->set(self::LAST_STARTED_KEY, $flow->value);
|
|
}
|
|
|
|
public function resolvePending(Request $request): ?PurchaseFlow
|
|
{
|
|
$session = $request->getSession();
|
|
$hasBooking = $session->has(BookingSessionManager::BOOKING_CREATE_KEY);
|
|
$hasVoucher = $session->has(VoucherPurchaseSessionManager::SESSION_KEY);
|
|
|
|
if (true === $hasBooking && true === $hasVoucher) {
|
|
return PurchaseFlow::tryFrom((string) $session->get(self::LAST_STARTED_KEY)) ?? PurchaseFlow::Booking;
|
|
}
|
|
|
|
return match (true) {
|
|
$hasBooking => PurchaseFlow::Booking,
|
|
$hasVoucher => PurchaseFlow::Voucher,
|
|
default => null,
|
|
};
|
|
}
|
|
}
|