Convert SessionManager to a factory class

This commit is contained in:
Pierre Rudloff 2020-10-21 22:47:15 +02:00
parent 5b0ee7651b
commit de8c5e5dc7
10 changed files with 64 additions and 52 deletions

View file

@ -10,7 +10,7 @@ use Alltube\Config;
use Alltube\Library\Downloader;
use Alltube\Library\Video;
use Alltube\LocaleManager;
use Alltube\SessionManager;
use Alltube\SessionFactory;
use Aura\Session\Segment;
use Consolidation\Log\Logger;
use Psr\Container\ContainerInterface;
@ -85,7 +85,7 @@ abstract class BaseController
{
$this->config = $container->get('config');
$this->container = $container;
$session = SessionManager::getSession();
$session = $container->get('session');
$this->sessionSegment = $session->getSegment(self::class);
$this->localeManager = $this->container->get('locale');
$this->downloader = $this->config->getDownloader();

View file

@ -4,6 +4,7 @@ namespace Alltube\Factory;
use Alltube\Exception\DependencyException;
use Alltube\LocaleManager;
use Slim\Container;
/**
* Class LocaleManagerFactory
@ -13,15 +14,16 @@ class LocaleManagerFactory
{
/**
* @param Container $container
* @return LocaleManager|null
* @throws DependencyException
*/
public static function create()
public static function create(Container $container)
{
if (!class_exists('Locale')) {
throw new DependencyException('You need to install the intl extension for PHP.');
}
return new LocaleManager();
return new LocaleManager($container->get('session'));
}
}

View file

@ -0,0 +1,27 @@
<?php
/**
* SessionFactory class.
*/
namespace Alltube\Factory;
use Aura\Session\Session;
/**
* Manage sessions.
*/
class SessionFactory
{
/**
* Get the current session.
*
* @return Session
*/
public static function create()
{
$session_factory = new \Aura\Session\SessionFactory();
return $session_factory->newInstance($_COOKIE);
}
}

View file

@ -7,6 +7,7 @@
namespace Alltube;
use Aura\Session\Segment;
use Aura\Session\Session;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\Loader\PoFileLoader;
@ -52,10 +53,10 @@ class LocaleManager
/**
* LocaleManager constructor.
* @param Session $session
*/
public function __construct()
public function __construct(Session $session)
{
$session = SessionManager::getSession();
$this->sessionSegment = $session->getSegment(self::class);
$cookieLocale = $this->sessionSegment->get('locale');

View file

@ -1,38 +0,0 @@
<?php
/**
* SessionManager class.
*/
namespace Alltube;
use Aura\Session\Session;
use Aura\Session\SessionFactory;
/**
* Manage sessions.
*/
class SessionManager
{
/**
* Current session.
*
* @var Session
*/
private static $session;
/**
* Get the current session.
*
* @return Session
*/
public static function getSession()
{
if (!isset(self::$session)) {
$session_factory = new SessionFactory();
self::$session = $session_factory->newInstance($_COOKIE);
}
return self::$session;
}
}