<?php declare(strict_types=1);
namespace GlobusSW6\Service\App;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\DependencyInjection\Container;
class AppIdentifier
{
const IS_APP_SESSION_KEY = 'ianeo_hitseller_is_app';
const DEVICE_ID_SESSION_KEY = 'ianeo_hitseller_app_device_id';
const AUTH_ID_SESSION_KEY = 'ianeo_hitseller_app_auth_id';
/** @var AppFromRequestIdentifier */
private $appFromRequestIdentifier;
/** @var LoggerInterface */
private $logger;
/** @var Session */
private $session;
/**
* @param AppFromRequestIdentifier $appFromRequestIdentifier
* @param LoggerInterface $logger
* @param Session $session
* @param Container $container
*/
public function __construct(AppFromRequestIdentifier $appFromRequestIdentifier, LoggerInterface $logger, Session $session, Container $container)
{
$this->appFromRequestIdentifier = $appFromRequestIdentifier;
$this->logger = $logger;
$this->session = $session;
$this->container = $container;
}
public function isApp(Request $request)
{
$isApp = $this->appFromRequestIdentifier->isApp($request);
if ($isApp) {
$deviceId = $this->appFromRequestIdentifier->getDeviceId($request);
$authToken = $this->appFromRequestIdentifier->getAuthToken($request);
$this->saveIsAppInSession();
$this->saveDeviceIdInSession($deviceId);
if (!empty($authToken)) {
$this->saveAuthTokenInSession($authToken);
}
}
return $this->checkIsAppInSession();
}
private function checkIsAppInSession() : bool
{
if (!$this->loadSession()) {
return false;
}
if (!$this->session->get($this::IS_APP_SESSION_KEY)) {
$this->logger->debug('AppIdentifier: CheckIsAppInSession: IS_APP_SESSION_KEY not exists in session');
return false;
} else {
$this->logger->debug('AppIdentifier: CheckIsAppInSession: IS_APP_SESSION_KEY exists in session - SessionId: ' . $this->session->getId());
return true;
}
return false;
}
private function saveIsAppInSession()
{
if (!$this->loadSession()) {
return;
}
$this->session->set($this::IS_APP_SESSION_KEY, true);
}
private function saveDeviceIdInSession($deviceId)
{
if (!$this->loadSession()) {
return;
}
if ($deviceId) {
$this->logger->debug('AppIdentifier: saveDeviceIdInSession - ' . $deviceId);
$this->session->set($this::DEVICE_ID_SESSION_KEY, $deviceId);
}
}
private function loadSession(): bool
{
// TODO-NGS: case !$this->container->initialized('session')
// TODO-NGS: case !$this->container->initialized('shop')
if (empty($this->session)) {
$this->logger->debug('AppIdentifier: Load Session: load session');
// TODO-NGS: Would be better to load the session directly instead of loading the whole container.
// Change this accordingly as soon as we know how to get the session directly.
$this->session = $this->container->get('session');
}
return true;
}
public function saveAuthTokenInSession($authToken)
{
if (!$this->loadSession()) {
return;
}
if ($authToken) {
$this->logger->debug('AppIdentifier: saveAuthTokenInSession - ' . $authToken);
$this->session->set($this::AUTH_ID_SESSION_KEY, $authToken);
}
}
}