Fix the configuration organization and add more implementations for the repositories.

This commit is contained in:
Dave Smith-Hayes 2024-05-25 21:44:57 -04:00
parent 2b24a4263c
commit c478be5601
13 changed files with 225 additions and 41 deletions

View File

@ -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"

107
app/composer.lock generated
View File

@ -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",

View File

@ -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));
}
/**

View File

@ -0,0 +1,22 @@
<?php
namespace Slovocast\Configuration;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
class DatabaseConnectionSchema
{
public static function getSchema(): Schema
{
return 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()
]);
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace Slovocast\Configuration;
use Nette\Schema\Schema;
use Nette\Schema\Expect;
class SiteInformationSchema
{
public static function getSchema(): Schema
{
return Expect::structure([
'name' => Expect::string()->default("Slovocast"),
'description' => Expect::string()->required()
]);
}
}

View File

@ -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();
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);
}
return $this->render('user/success.twig');
}
}

View File

@ -9,6 +9,6 @@ class RegisterUserPage extends Controller
{
public function respond(): Response
{
return $this->render('users/register.twig');
return $this->render('user/register.twig');
}
}

View File

@ -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(),

View File

@ -1,25 +0,0 @@
<?php
namespace Slovocast\Infrastructure\Configuration;
use League\Config\ConfigurationInterface;
use League\Config\Configuration;
use Nette\Schema\Expect;
class DatabaseConfiguration
{
public static function getConfig(): ConfigurationInterface
{
return new Configuration([
'database' => 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()
])
]);
}
}

View File

@ -1,7 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>{% block page_title %}{% endblock %}</title>
<title>{% block page_title %}{% endblock %}{{ site_name }}</title>
<meta name="description" value="{{ site_description }}">
</head>
<body>

View File

@ -0,0 +1,27 @@
{% extends 'layouts/skeleton' %}
{% block "content" %}
<div>
<form action="/users/register" method="post">
<div>
<label for="name">Name<br>
<input name="name" type="text" required>
</div>
<div>
<label for="email">Email<br>
<input name="email" type="text" required>
</div>
<div>
<label for="password">Password<br>
<input name="password" type="password" required>
</div>
<div>
<label for="check_password">Confirm Password<br>
<input name="check_password" type="password" required>
</div>
<div>
<input type="submit" label="Submit">
</div>
</form>
</div>
{% endblock %}

View File

@ -1,10 +0,0 @@
{% extends 'layouts/skeleton' %}
{% block "content" %}
<div>
<form action="/users/register" method="post">
<input type="submit" label="Submit">
</form>
</div>
{% endblock %}