soko-web/src/Request/Request.php
Pőcze Bence 6a35344210
Some checks failed
soko-web/pipeline/pr-master There was a failure building this commit
pass Session object to Request
2023-05-02 11:47:08 +02:00

105 lines
2.1 KiB
PHP

<?php namespace SokoWeb\Request;
use SokoWeb\Interfaces\Repository\IUserRepository;
use SokoWeb\Interfaces\Authentication\IUser;
use SokoWeb\Interfaces\Request\IRequest;
use SokoWeb\Interfaces\Request\ISession;
class Request implements IRequest
{
private string $base;
private array $get;
private array $routeParams = [];
private array $post;
private array $headers;
private ISession $session;
private ?IUser $user = null;
public function __construct(
string $base,
array $get,
array $post,
array $headers,
ISession $session,
IUserRepository $userRepository)
{
$this->base = $base;
$this->get = $get;
$this->post = $post;
$this->headers = $headers;
$this->session = $session;
$userId = $this->session->get('userId');
if ($userId !== null) {
$this->user = $userRepository->getById($userId);
}
}
public function setParsedRouteParams(array $routeParams): void
{
$this->routeParams = $routeParams;
}
public function getBase(): string
{
return $this->base;
}
public function query($key)
{
if (isset($this->get[$key])) {
return $this->get[$key];
}
if (isset($this->routeParams[$key])) {
return $this->routeParams[$key];
}
return null;
}
public function post($key)
{
if (isset($this->post[$key])) {
return $this->post[$key];
}
return null;
}
public function header($key)
{
if (isset($this->headers[$key])) {
return $this->headers[$key];
}
return null;
}
public function session(): ISession
{
return $this->session;
}
public function setUser(?IUser $user): void
{
if ($user === null) {
$this->session->delete('userId');
return;
}
$this->session->set('userId', $user->getUniqueId());
}
public function user(): ?IUser
{
return $this->user;
}
}