From de8c5e5dc79dca95a477e9a3b8899e69a7dc7116 Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Wed, 21 Oct 2020 22:47:15 +0200 Subject: [PATCH] Convert SessionManager to a factory class --- classes/Controller/BaseController.php | 4 +-- classes/Factory/LocaleManagerFactory.php | 6 ++-- classes/Factory/SessionFactory.php | 27 +++++++++++++++++ classes/LocaleManager.php | 5 ++-- classes/SessionManager.php | 38 ------------------------ index.php | 6 +++- tests/ControllerTest.php | 7 ++++- tests/LocaleManagerTest.php | 3 +- tests/LocaleMiddlewareTest.php | 8 +++-- tests/ViewFactoryTest.php | 12 ++++++-- 10 files changed, 64 insertions(+), 52 deletions(-) create mode 100644 classes/Factory/SessionFactory.php delete mode 100644 classes/SessionManager.php diff --git a/classes/Controller/BaseController.php b/classes/Controller/BaseController.php index 9f0ee94..1922aff 100644 --- a/classes/Controller/BaseController.php +++ b/classes/Controller/BaseController.php @@ -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(); diff --git a/classes/Factory/LocaleManagerFactory.php b/classes/Factory/LocaleManagerFactory.php index 250f50f..22fa261 100644 --- a/classes/Factory/LocaleManagerFactory.php +++ b/classes/Factory/LocaleManagerFactory.php @@ -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')); } } diff --git a/classes/Factory/SessionFactory.php b/classes/Factory/SessionFactory.php new file mode 100644 index 0000000..3d291b7 --- /dev/null +++ b/classes/Factory/SessionFactory.php @@ -0,0 +1,27 @@ +newInstance($_COOKIE); + } +} diff --git a/classes/LocaleManager.php b/classes/LocaleManager.php index 01a92a8..f3f7919 100644 --- a/classes/LocaleManager.php +++ b/classes/LocaleManager.php @@ -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'); diff --git a/classes/SessionManager.php b/classes/SessionManager.php deleted file mode 100644 index 9d8dace..0000000 --- a/classes/SessionManager.php +++ /dev/null @@ -1,38 +0,0 @@ -newInstance($_COOKIE); - } - - return self::$session; - } -} diff --git a/index.php b/index.php index c22e010..d6cdd7c 100644 --- a/index.php +++ b/index.php @@ -9,6 +9,7 @@ use Alltube\ErrorHandler; use Alltube\Factory\ConfigFactory; use Alltube\Factory\LocaleManagerFactory; use Alltube\Factory\LoggerFactory; +use Alltube\Factory\SessionFactory; use Alltube\Factory\ViewFactory; use Alltube\Middleware\CspMiddleware; use Alltube\Middleware\LinkHeaderMiddleware; @@ -32,8 +33,11 @@ try { // Config. $container['config'] = ConfigFactory::create($container); + // Session. + $container['session'] = SessionFactory::create(); + // Locales. - $container['locale'] = LocaleManagerFactory::create(); + $container['locale'] = LocaleManagerFactory::create($container); // Smarty. $container['view'] = ViewFactory::create($container); diff --git a/tests/ControllerTest.php b/tests/ControllerTest.php index ab5a7df..535df78 100644 --- a/tests/ControllerTest.php +++ b/tests/ControllerTest.php @@ -11,6 +11,9 @@ use Alltube\Controller\BaseController; use Alltube\Controller\DownloadController; use Alltube\Controller\FrontController; use Alltube\Exception\ConfigException; +use Alltube\Exception\DependencyException; +use Alltube\Factory\LocaleManagerFactory; +use Alltube\Factory\SessionFactory; use Alltube\Factory\ViewFactory; use Alltube\LocaleManager; use Psr\Log\NullLogger; @@ -55,6 +58,7 @@ abstract class ControllerTest extends BaseTest /** * Prepare tests. * @throws ConfigException|SmartyException + * @throws DependencyException */ protected function setUp(): void { @@ -64,7 +68,8 @@ abstract class ControllerTest extends BaseTest $this->request = Request::createFromEnvironment(Environment::mock()); $this->response = new Response(); $this->container['config'] = Config::fromFile($this->getConfigFile()); - $this->container['locale'] = new LocaleManager(); + $this->container['session'] = SessionFactory::create(); + $this->container['locale'] = LocaleManagerFactory::create($this->container); $this->container['view'] = ViewFactory::create($this->container, $this->request); $this->container['logger'] = new NullLogger(); diff --git a/tests/LocaleManagerTest.php b/tests/LocaleManagerTest.php index 15b1fcf..b2010a4 100644 --- a/tests/LocaleManagerTest.php +++ b/tests/LocaleManagerTest.php @@ -6,6 +6,7 @@ namespace Alltube\Test; +use Alltube\Factory\SessionFactory; use Alltube\Locale; use Alltube\LocaleManager; @@ -27,7 +28,7 @@ class LocaleManagerTest extends BaseTest protected function setUp(): void { $_SESSION[LocaleManager::class]['locale'] = 'foo_BAR'; - $this->localeManager = new LocaleManager(); + $this->localeManager = new LocaleManager(SessionFactory::create()); } /** diff --git a/tests/LocaleMiddlewareTest.php b/tests/LocaleMiddlewareTest.php index fecfc16..2ea66da 100644 --- a/tests/LocaleMiddlewareTest.php +++ b/tests/LocaleMiddlewareTest.php @@ -6,7 +6,9 @@ namespace Alltube\Test; -use Alltube\LocaleManager; +use Alltube\Exception\DependencyException; +use Alltube\Factory\LocaleManagerFactory; +use Alltube\Factory\SessionFactory; use Alltube\Middleware\LocaleMiddleware; use Slim\Container; use Slim\Http\Environment; @@ -34,11 +36,13 @@ class LocaleMiddlewareTest extends BaseTest /** * Prepare tests. + * @throws DependencyException */ protected function setUp(): void { $this->container = new Container(); - $this->container['locale'] = new LocaleManager(); + $this->container['session'] = SessionFactory::create(); + $this->container['locale'] = LocaleManagerFactory::create($this->container); $this->middleware = new LocaleMiddleware($this->container); } diff --git a/tests/ViewFactoryTest.php b/tests/ViewFactoryTest.php index 9237bb3..62990ef 100644 --- a/tests/ViewFactoryTest.php +++ b/tests/ViewFactoryTest.php @@ -6,8 +6,10 @@ namespace Alltube\Test; +use Alltube\Exception\DependencyException; +use Alltube\Factory\LocaleManagerFactory; +use Alltube\Factory\SessionFactory; use Alltube\Factory\ViewFactory; -use Alltube\LocaleManager; use Slim\Container; use Slim\Http\Environment; use Slim\Http\Request; @@ -24,11 +26,13 @@ class ViewFactoryTest extends BaseTest * * @return void * @throws SmartyException + * @throws DependencyException */ public function testCreate() { $container = new Container(); - $container['locale'] = new LocaleManager(); + $container['session'] = SessionFactory::create(); + $container['locale'] = LocaleManagerFactory::create($container); $view = ViewFactory::create($container); $this->assertInstanceOf(Smarty::class, $view); } @@ -38,11 +42,13 @@ class ViewFactoryTest extends BaseTest * * @return void * @throws SmartyException + * @throws DependencyException */ public function testCreateWithXForwardedProto() { $container = new Container(); - $container['locale'] = new LocaleManager(); + $container['session'] = SessionFactory::create(); + $container['locale'] = LocaleManagerFactory::create($container); $request = Request::createFromEnvironment(Environment::mock()); $view = ViewFactory::create($container, $request->withHeader('X-Forwarded-Proto', 'https')); $this->assertInstanceOf(Smarty::class, $view);