<?php
declare(strict_types=1);
namespace Lnb\Shopware6\LnbRuleCondition\Context;
use Shopware\Core\Framework\Struct\Struct;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class RequestScope extends Struct implements \Serializable
{
protected ?Request $request = null;
public function __construct(RequestStack $requestStack)
{
$this->request = $requestStack->getCurrentRequest();
}
public function isStorefront(): bool
{
if (!$this->request) {
return false;
}
return str_starts_with($this->request->attributes->get('_route', ''), 'frontend.');
}
public function serialize(): string
{
if (!$this->request) {
return '';
}
return serialize(
[
'query' => $this->request->query->all(),
'request' => $this->request->request->all(),
'attributes' => $this->request->attributes->all(),
'cookies' => $this->request->cookies->all(),
'files' => $this->request->files->all(),
'server' => $this->request->server->all(),
'content' => $this->request->getContent(),
]
);
}
public function unserialize($data): void
{
if ($data === '') {
return;
}
$unserialized = unserialize($data);
if (!$unserialized ||
!array_key_exists('query', $unserialized) ||
!array_key_exists('request', $unserialized) ||
!array_key_exists('attributes', $unserialized) ||
!array_key_exists('cookies', $unserialized) ||
!array_key_exists('files', $unserialized) ||
!array_key_exists('server', $unserialized) ||
!array_key_exists('content', $unserialized)
) {
return;
}
$this->request = new Request(
$unserialized['query'] ?? [],
$unserialized['request'] ?? [],
$unserialized['attributes'] ?? [],
$unserialized['cookies'] ?? [],
$unserialized['files'] ?? [],
$unserialized['server'] ?? [],
$unserialized['content'] ?? null
);
}
public function getRequest(): ?Request
{
return $this->request;
}
}