<?php
declare(strict_types=1);
namespace NetInventors\NetiNextEasyCoupon\Service\ConditionService;
use NetInventors\NetiNextEasyCoupon\Service\VoucherRedemption\Validator\Error\ConditionsError;
use Symfony\Component\HttpFoundation\Session\Session;
class ConditionErrorService
{
private const SESSION_KEY = 'EasyCouponConditionsError';
private Session $session;
public function __construct(Session $session)
{
$this->session = $session;
}
public function set(?ConditionsError $error): void
{
if (!$error) {
return;
}
$this->session->set(self::SESSION_KEY, [
'code' => $error->getCode(),
'messages' => $error->getMessages(),
]);
}
public function get(): ?ConditionsError
{
/** @var array|null $error */
$error = $this->session->get(self::SESSION_KEY);
$this->session->remove(self::SESSION_KEY);
if (\is_array($error) && \is_string($error['code']) && \is_array($error['messages'])) {
return new ConditionsError($error['code'], $error['messages']);
}
return null;
}
}