More over engineering.

TBH at this point I think I need a DI container.
This commit is contained in:
2019-03-27 15:29:06 +01:00
parent 882214d6ea
commit 650b7ee48c

View File

@@ -1,10 +1,13 @@
<?php <?php
use FooBar\Configuration; use FooBar\Configuration;
use FooBar\ReadableState;
use FooBar\State;
require_once '../vendor/autoload.php'; require_once '../vendor/autoload.php';
$config = Configuration::loadConfig(); $config = Configuration::loadConfig();
$state = new State($config);
function auth_check(Configuration $config) function auth_check(Configuration $config)
{ {
@@ -12,26 +15,22 @@ function auth_check(Configuration $config)
http_response_code(403); http_response_code(403);
} }
echo 'Some text otherwise PHP crashes. I wish this was a joke.'; echo 'Some text otherwise PHP crashes. I wish this was a joke.';
} }
function toggle(Configuration $config) function toggle(Configuration $config, State $state)
{ {
if ($config->isAuthorized()) { if ($config->isAuthorized()) {
$file = $config->stateFile(); $state->toggle();
if ($config->isOpen()) {
unlink($file);
} else {
touch($file);
}
} }
header('location: ' . $_SERVER['REQUEST_URI']); header('location: ' . $_SERVER['REQUEST_URI']);
} }
function display_state(bool $open) function display_state(ReadableState $state)
{ {
$open = $state->isOpen();
header('X-Foobar-Open: ' . var_export($open, true)); header('X-Foobar-Open: ' . var_export($open, true));
if ($open) { if ($open) {
require '../templates/open.html'; require '../templates/open.html';
@@ -43,7 +42,7 @@ function display_state(bool $open)
if (isset($_POST['auth_check'])) { if (isset($_POST['auth_check'])) {
auth_check($config); auth_check($config);
} elseif (isset($_POST['toggle'])) { } elseif (isset($_POST['toggle'])) {
toggle($config); toggle($config, $state);
} else { } else {
display_state($config->isOpen()); display_state($state);
} }