From c478be56016c75afc70515948aeccad86dcc6a74 Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Sat, 25 May 2024 21:44:57 -0400 Subject: [PATCH] Fix the configuration organization and add more implementations for the repositories. --- app/composer.json | 4 +- app/composer.lock | 107 +++++++++++++++++- app/src/Bootstrap.php | 7 +- .../DatabaseConnectionSchema.php | 22 ++++ .../Configuration/SiteInformationSchema.php | 17 +++ .../Controller/User/RegisterUserAction.php | 27 ++++- app/src/Controller/User/RegisterUserPage.php | 2 +- app/src/Domain/Repository/UserRepository.php | 16 +++ .../Configuration/DatabaseConfiguration.php | 25 ---- app/templates/layouts/skeleton.twig | 2 +- app/templates/user/register.twig | 27 +++++ app/templates/{users => user}/success.twig | 0 app/templates/users/register.twig | 10 -- 13 files changed, 225 insertions(+), 41 deletions(-) create mode 100644 app/src/Configuration/DatabaseConnectionSchema.php create mode 100644 app/src/Configuration/SiteInformationSchema.php delete mode 100644 app/src/Infrastructure/Configuration/DatabaseConfiguration.php create mode 100644 app/templates/user/register.twig rename app/templates/{users => user}/success.twig (100%) delete mode 100644 app/templates/users/register.twig diff --git a/app/composer.json b/app/composer.json index 19f8142..7114e5d 100644 --- a/app/composer.json +++ b/app/composer.json @@ -9,7 +9,9 @@ "php-di/php-di": "^7.0", "league/flysystem": "^3.27", "league/config": "^1.2", - "monolog/monolog": "^3.6" + "monolog/monolog": "^3.6", + "middlewares/php-session": "^3.1", + "slim/flash": "^0.4.0" }, "require-dev": { "phpunit/phpunit": "^11.1" diff --git a/app/composer.lock b/app/composer.lock index 77addb7..1d44314 100644 --- a/app/composer.lock +++ b/app/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "5210478491eae9635949e00b3a77cfdb", + "content-hash": "f0850ac980c2cfb02478e69d41ea86eb", "packages": [ { "name": "dflydev/dot-access-data", @@ -467,6 +467,59 @@ ], "time": "2024-01-28T23:22:08+00:00" }, + { + "name": "middlewares/php-session", + "version": "v3.1.1", + "source": { + "type": "git", + "url": "https://github.com/middlewares/php-session.git", + "reference": "089ea6929435d6f6f26e61a4f8ab682544294c31" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/middlewares/php-session/zipball/089ea6929435d6f6f26e61a4f8ab682544294c31", + "reference": "089ea6929435d6f6f26e61a4f8ab682544294c31", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0", + "psr/http-server-middleware": "^1.0" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^2.0", + "laminas/laminas-diactoros": "^2.3", + "middlewares/utils": "^3.0", + "oscarotero/php-cs-fixer-config": "^1.0", + "phpstan/phpstan": "^0.12", + "phpunit/phpunit": "^8|^9", + "squizlabs/php_codesniffer": "^3.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Middlewares\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Middleware to start php sessions using the request data", + "homepage": "https://github.com/middlewares/php-session", + "keywords": [ + "http", + "middleware", + "psr-15", + "psr-7", + "server", + "session" + ], + "support": { + "issues": "https://github.com/middlewares/php-session/issues", + "source": "https://github.com/middlewares/php-session/tree/v3.1.1" + }, + "time": "2022-03-13T01:22:27+00:00" + }, { "name": "monolog/monolog", "version": "3.6.0", @@ -1262,6 +1315,58 @@ }, "time": "2019-03-08T08:55:37+00:00" }, + { + "name": "slim/flash", + "version": "0.4.0", + "source": { + "type": "git", + "url": "https://github.com/slimphp/Slim-Flash.git", + "reference": "9aaff5fded3b54f4e519ec3d4ac74d3d1f2cbbbc" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/slimphp/Slim-Flash/zipball/9aaff5fded3b54f4e519ec3d4ac74d3d1f2cbbbc", + "reference": "9aaff5fded3b54f4e519ec3d4ac74d3d1f2cbbbc", + "shasum": "" + }, + "require": { + "php": ">=5.5.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Slim\\Flash\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Josh Lockhart", + "email": "hello@joshlockhart.com", + "homepage": "http://joshlockhart.com" + } + ], + "description": "Slim Framework Flash message service provider", + "homepage": "http://slimframework.com", + "keywords": [ + "flash", + "framework", + "message", + "provider", + "slim" + ], + "support": { + "issues": "https://github.com/slimphp/Slim-Flash/issues", + "source": "https://github.com/slimphp/Slim-Flash/tree/master" + }, + "time": "2017-10-22T10:35:05+00:00" + }, { "name": "slim/psr7", "version": "1.6.1", diff --git a/app/src/Bootstrap.php b/app/src/Bootstrap.php index df085e4..1621f44 100644 --- a/app/src/Bootstrap.php +++ b/app/src/Bootstrap.php @@ -10,6 +10,8 @@ use DI\ContainerBuilder; use Slim\Views\Twig; use Slim\Views\TwigMiddleware; use Slovocast\Routes; +use Slovocast\Configuration\SiteInformationSchema; +use Slovocast\Configuration\DatabaseConnectionSchema; class Bootstrap { @@ -23,6 +25,8 @@ class Bootstrap $config = new Configuration(); // set all configuration details + $config->addSchema('site', SiteInformationSchema::getSchema()); + $config->addSchema('database', DatabaseConnectionSchema::getSchema()); $config->merge([ 'site' => [ @@ -77,7 +81,7 @@ class Bootstrap /** * @var Configuration */ - $config = $app->get('config'); + $config = $app->getContainer()->get('config'); $app->addErrorMiddleware(true, true, true); // Twig @@ -91,6 +95,7 @@ class Bootstrap $twig->getEnvironment() ->addGlobal('site_description', $config->get('site.description')); $app->add(TwigMiddleware::create($app, $twig)); + } /** diff --git a/app/src/Configuration/DatabaseConnectionSchema.php b/app/src/Configuration/DatabaseConnectionSchema.php new file mode 100644 index 0000000..d57d598 --- /dev/null +++ b/app/src/Configuration/DatabaseConnectionSchema.php @@ -0,0 +1,22 @@ + Expect::anyOf('mysql', 'sqlite')->required(), + 'host' => Expect::string()->default('localhost'), + 'post' => Expect::int()->min(1)->max(65535), + 'database' => Expect::string()->required(), + 'username' => Expect::string()->required(), + 'password' => Expect::string()->nullable() + ]); + } + +} diff --git a/app/src/Configuration/SiteInformationSchema.php b/app/src/Configuration/SiteInformationSchema.php new file mode 100644 index 0000000..8afeb33 --- /dev/null +++ b/app/src/Configuration/SiteInformationSchema.php @@ -0,0 +1,17 @@ + Expect::string()->default("Slovocast"), + 'description' => Expect::string()->required() + ]); + } +} diff --git a/app/src/Controller/User/RegisterUserAction.php b/app/src/Controller/User/RegisterUserAction.php index 2c72c24..324eaa1 100644 --- a/app/src/Controller/User/RegisterUserAction.php +++ b/app/src/Controller/User/RegisterUserAction.php @@ -4,13 +4,38 @@ namespace Slovocast\Controller\User; use Psr\Http\Message\ResponseInterface as Response; use Slovocast\Controller\Controller; +use Slovocast\Domain\Entity\User; +use Slovocast\Domain\Repository\UserRepositoryInterface; class RegisterUserAction extends Controller { + public function __construct( + protected UserRepositoryInterface $userRepository + ) { } + public function respond(): Response { // get user Data + $requestData = $this->request->getParsedBody(); - return $this->render('user/success.twig'); + if ($requestData['password'] !== $requestData['checked_password']) { + $response = $this->render('user/register.twig'); + return $response->withStatus(400); + } + + $user = User::fromArray([ + 'email' => $requestData['email'], + 'name' => $requestData['name'], + 'password' => $requestData['password'], + ]); + + try { + $this->userRepository->save($user); + return $this->render('user/success.twig'); + } catch (\Exception $e) { + $response = $this->render('user/register.twig'); + return $response->withStatus(400); + } + } } diff --git a/app/src/Controller/User/RegisterUserPage.php b/app/src/Controller/User/RegisterUserPage.php index 7e0ac48..d4c1ff4 100644 --- a/app/src/Controller/User/RegisterUserPage.php +++ b/app/src/Controller/User/RegisterUserPage.php @@ -9,6 +9,6 @@ class RegisterUserPage extends Controller { public function respond(): Response { - return $this->render('users/register.twig'); + return $this->render('user/register.twig'); } } diff --git a/app/src/Domain/Repository/UserRepository.php b/app/src/Domain/Repository/UserRepository.php index bb8d91e..99e4996 100644 --- a/app/src/Domain/Repository/UserRepository.php +++ b/app/src/Domain/Repository/UserRepository.php @@ -55,6 +55,12 @@ class UserRepository implements UserRepositoryInterface return array_shift($results); } + /** + * Get a single instance of the User Entity. + * + * @param int $id + * @return User + */ public function get(int $id): User { $query = "SELECT * FROM users WHERE id = :id LIMIT 1"; @@ -63,9 +69,16 @@ class UserRepository implements UserRepositoryInterface [ 'id' => $id ], $this->database->getConnection() ); + return $this->userFromQueryResults($userData); } + /** + * Get a single instance of the User Entity by their Email address. + * + * @param string $email + * @return User + */ public function getFromEmail(string $email): User { $query = "SELECT * FROM users WHERE email = :email LIMIT 1"; @@ -74,6 +87,7 @@ class UserRepository implements UserRepositoryInterface [ 'email' => $email ], $this->database->getConnection() ); + return $this->userFromQueryResults($userData); } @@ -84,6 +98,7 @@ class UserRepository implements UserRepositoryInterface VALUES (:email, :password, :name)"; $statement = $connection->prepare($query); + return $statement->execute([ 'email' => $user->getEmail(), 'name'=> $user->getName(), @@ -101,6 +116,7 @@ class UserRepository implements UserRepositoryInterface WHERE id = :id"; $statement = $connection->prepare($query); + return $statement->execute([ 'email' => $user->getEmail(), 'name' => $user->getName(), diff --git a/app/src/Infrastructure/Configuration/DatabaseConfiguration.php b/app/src/Infrastructure/Configuration/DatabaseConfiguration.php deleted file mode 100644 index 34754b1..0000000 --- a/app/src/Infrastructure/Configuration/DatabaseConfiguration.php +++ /dev/null @@ -1,25 +0,0 @@ - Expect::structure([ - 'driver' => Expect::anyOf('mysql', 'sqlite')->required(), - 'host' => Expect::string()->default('localhost'), - 'post' => Expect::int()->min(1)->max(65535), - 'database' => Expect::string()->required(), - 'username' => Expect::string()->required(), - 'password' => Expect::string()->nullable() - ]) - ]); - } - -} diff --git a/app/templates/layouts/skeleton.twig b/app/templates/layouts/skeleton.twig index 10961d2..0fcfb47 100644 --- a/app/templates/layouts/skeleton.twig +++ b/app/templates/layouts/skeleton.twig @@ -1,7 +1,7 @@ - {% block page_title %}{% endblock %} + {% block page_title %}{% endblock %}{{ site_name }} diff --git a/app/templates/user/register.twig b/app/templates/user/register.twig new file mode 100644 index 0000000..fa53884 --- /dev/null +++ b/app/templates/user/register.twig @@ -0,0 +1,27 @@ +{% extends 'layouts/skeleton' %} + +{% block "content" %} +
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+{% endblock %} diff --git a/app/templates/users/success.twig b/app/templates/user/success.twig similarity index 100% rename from app/templates/users/success.twig rename to app/templates/user/success.twig diff --git a/app/templates/users/register.twig b/app/templates/users/register.twig deleted file mode 100644 index 11aa607..0000000 --- a/app/templates/users/register.twig +++ /dev/null @@ -1,10 +0,0 @@ -{% extends 'layouts/skeleton' %} - -{% block "content" %} -
-
- - -
-
-{% endblock %}