From 7656012fa7ee20123b796983ea0819cbde9df45f Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Wed, 6 Nov 2024 21:34:59 -0500 Subject: [PATCH] Move to DB handler, test the local env to get a connection to database. Should add phinx migrations at some point. --- app/composer.lock | 52 +++++++++---------- app/src/Bootstrap.php | 13 +++-- app/src/Controller/HealthCheck.php | 10 ++-- app/src/Controller/User/LoginUserAction.php | 2 +- .../Repository/Channel/ChannelRepository.php | 4 +- .../Domain/Repository/User/UserRepository.php | 4 +- ...rface.php => DatabaseHandlerInterface.php} | 4 +- .../Database/DatabaseConnection.php | 31 +++++++---- docker-compose.yml | 12 +++-- 9 files changed, 72 insertions(+), 60 deletions(-) rename app/src/Infrastructure/Api/Database/{DatabaseConnectionInterface.php => DatabaseHandlerInterface.php} (53%) diff --git a/app/composer.lock b/app/composer.lock index c9249a7..2a694e3 100644 --- a/app/composer.lock +++ b/app/composer.lock @@ -2981,16 +2981,16 @@ }, { "name": "symfony/config", - "version": "v7.1.6", + "version": "v7.1.7", "source": { "type": "git", "url": "https://github.com/symfony/config.git", - "reference": "5c6152766251ff45a44b76affadd5287e253fb27" + "reference": "dc373a5cbd345354696f5dfd39c5c7a8ea23f4c8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/config/zipball/5c6152766251ff45a44b76affadd5287e253fb27", - "reference": "5c6152766251ff45a44b76affadd5287e253fb27", + "url": "https://api.github.com/repos/symfony/config/zipball/dc373a5cbd345354696f5dfd39c5c7a8ea23f4c8", + "reference": "dc373a5cbd345354696f5dfd39c5c7a8ea23f4c8", "shasum": "" }, "require": { @@ -3036,7 +3036,7 @@ "description": "Helps you find, load, combine, autofill and validate configuration values of any kind", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/config/tree/v7.1.6" + "source": "https://github.com/symfony/config/tree/v7.1.7" }, "funding": [ { @@ -3052,20 +3052,20 @@ "type": "tidelift" } ], - "time": "2024-10-25T15:11:02+00:00" + "time": "2024-11-04T11:34:07+00:00" }, { "name": "symfony/console", - "version": "v7.1.6", + "version": "v7.1.7", "source": { "type": "git", "url": "https://github.com/symfony/console.git", - "reference": "bb5192af6edc797cbab5c8e8ecfea2fe5f421e57" + "reference": "3284aafcac338b6e86fd955ee4d794cbe434151a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/console/zipball/bb5192af6edc797cbab5c8e8ecfea2fe5f421e57", - "reference": "bb5192af6edc797cbab5c8e8ecfea2fe5f421e57", + "url": "https://api.github.com/repos/symfony/console/zipball/3284aafcac338b6e86fd955ee4d794cbe434151a", + "reference": "3284aafcac338b6e86fd955ee4d794cbe434151a", "shasum": "" }, "require": { @@ -3129,7 +3129,7 @@ "terminal" ], "support": { - "source": "https://github.com/symfony/console/tree/v7.1.6" + "source": "https://github.com/symfony/console/tree/v7.1.7" }, "funding": [ { @@ -3145,7 +3145,7 @@ "type": "tidelift" } ], - "time": "2024-10-09T08:46:59+00:00" + "time": "2024-11-05T15:34:55+00:00" }, { "name": "symfony/deprecation-contracts", @@ -3926,16 +3926,16 @@ }, { "name": "twig/twig", - "version": "v3.14.0", + "version": "v3.14.1", "source": { "type": "git", "url": "https://github.com/twigphp/Twig.git", - "reference": "126b2c97818dbff0cdf3fbfc881aedb3d40aae72" + "reference": "f405356d20fb43603bcadc8b09bfb676cb04a379" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/twigphp/Twig/zipball/126b2c97818dbff0cdf3fbfc881aedb3d40aae72", - "reference": "126b2c97818dbff0cdf3fbfc881aedb3d40aae72", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/f405356d20fb43603bcadc8b09bfb676cb04a379", + "reference": "f405356d20fb43603bcadc8b09bfb676cb04a379", "shasum": "" }, "require": { @@ -3989,7 +3989,7 @@ ], "support": { "issues": "https://github.com/twigphp/Twig/issues", - "source": "https://github.com/twigphp/Twig/tree/v3.14.0" + "source": "https://github.com/twigphp/Twig/tree/v3.14.1" }, "funding": [ { @@ -4001,7 +4001,7 @@ "type": "tidelift" } ], - "time": "2024-09-09T17:55:12+00:00" + "time": "2024-11-06T18:17:38+00:00" }, { "name": "vlucas/phpdotenv", @@ -4497,16 +4497,16 @@ }, { "name": "phpdocumentor/reflection-docblock", - "version": "5.5.0", + "version": "5.5.1", "source": { "type": "git", "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", - "reference": "54e10d44fc1a84e2598d26f70d4f6f1f233e228a" + "reference": "0c70d2c566e899666f367ab7b80986beb3581e6f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/54e10d44fc1a84e2598d26f70d4f6f1f233e228a", - "reference": "54e10d44fc1a84e2598d26f70d4f6f1f233e228a", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/0c70d2c566e899666f367ab7b80986beb3581e6f", + "reference": "0c70d2c566e899666f367ab7b80986beb3581e6f", "shasum": "" }, "require": { @@ -4555,9 +4555,9 @@ "description": "With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded in a DocBlock.", "support": { "issues": "https://github.com/phpDocumentor/ReflectionDocBlock/issues", - "source": "https://github.com/phpDocumentor/ReflectionDocBlock/tree/5.5.0" + "source": "https://github.com/phpDocumentor/ReflectionDocBlock/tree/5.5.1" }, - "time": "2024-11-04T21:26:31+00:00" + "time": "2024-11-06T11:58:54+00:00" }, { "name": "phpdocumentor/type-resolver", @@ -6242,12 +6242,12 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": {}, + "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, "platform": { "ext-pdo": "*" }, - "platform-dev": {}, + "platform-dev": [], "plugin-api-version": "2.6.0" } diff --git a/app/src/Bootstrap.php b/app/src/Bootstrap.php index d010095..c29c6db 100644 --- a/app/src/Bootstrap.php +++ b/app/src/Bootstrap.php @@ -26,19 +26,18 @@ use Psr\Log\LoggerInterface; use Slim\App; use Slim\Factory\AppFactory; use Slim\Psr7\Factory\ResponseFactory; - -use Slovocast\Domain\Repository\Channel\ChannelRepository; -use Slovocast\Domain\Repository\Channel\ChannelRepositoryInterface; use Twig\Error\LoaderError; use Slovocast\Configuration\DatabaseConnectionSchema; use Slovocast\Configuration\SessionSchema; use Slovocast\Configuration\SiteInformationSchema; +use Slovocast\Domain\Repository\Channel\ChannelRepository; +use Slovocast\Domain\Repository\Channel\ChannelRepositoryInterface; use Slovocast\Domain\Repository\User\UserRepository; use Slovocast\Domain\Repository\User\UserRepositoryInterface; -use Slovocast\Infrastructure\Api\Database\DatabaseConnectionInterface; +use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface; use Slovocast\Infrastructure\Database\DatabaseConnection; use Slovocast\Infrastructure\Api\User\UserAuthorizationInterface; use Slovocast\Infrastructure\User\BasicUserAuthorization; @@ -142,7 +141,7 @@ class Bootstrap /** * Database Connections */ - DatabaseConnectionInterface::class => function (ContainerInterface $container) { + DatabaseHandlerInterface::class => function (ContainerInterface $container) { $config = $container->get('config'); return new DatabaseConnection( $config->get('database.host'), @@ -165,14 +164,14 @@ class Bootstrap */ UserRepositoryInterface::class => function (ContainerInterface $container) { return new UserRepository( - $container->get(DatabaseConnectionInterface::class), + $container->get(DatabaseHandlerInterface::class), $container->get(UserAuthorizationInterface::class) ); }, ChannelRepositoryInterface::class => function (ContainerInterface $container) { return new ChannelRepository( - $container->get(DatabaseConnectionInterface::class) + $container->get(DatabaseHandlerInterface::class) ); }, ]); diff --git a/app/src/Controller/HealthCheck.php b/app/src/Controller/HealthCheck.php index 87ac02c..5170d64 100644 --- a/app/src/Controller/HealthCheck.php +++ b/app/src/Controller/HealthCheck.php @@ -3,23 +3,23 @@ namespace Slovocast\Controller; use Slovocast\Controller\Controller; -use React\Mysql\MysqlClient; -use function React\Async\await; +use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface; use Psr\Http\Message\ResponseInterface as Response; class HealthCheck extends Controller { public function __construct( - protected MysqlClient $db + protected DatabaseHandlerInterface $database ) { } public function handle(): Response { - $results = await($this->db->query("SELECT 1")); + $statement = $this->database->getConnection()->query('SELECT 1'); + $results = $statement->fetchAll(); return $this->json([ 'http' => true, - 'database' => (bool) count($results->resultRows), + 'database' => (bool) count($results), ]); } } diff --git a/app/src/Controller/User/LoginUserAction.php b/app/src/Controller/User/LoginUserAction.php index dc9f226..b721200 100644 --- a/app/src/Controller/User/LoginUserAction.php +++ b/app/src/Controller/User/LoginUserAction.php @@ -29,7 +29,7 @@ class LoginUserAction extends Controller } if (!$this->auth->verify($credentials['password'], $user->getPassword())) { - $this->session->getFlash()->add('error', 'Unable to login.'); + $this->session->getFlash()->add('error', "Unable to login."); return $this->response->withStatus(400); } diff --git a/app/src/Domain/Repository/Channel/ChannelRepository.php b/app/src/Domain/Repository/Channel/ChannelRepository.php index 104515a..cb719be 100644 --- a/app/src/Domain/Repository/Channel/ChannelRepository.php +++ b/app/src/Domain/Repository/Channel/ChannelRepository.php @@ -10,12 +10,12 @@ use Slovocast\Domain\Repository\Channel\ChannelRepositoryInterface; use Slovocast\Domain\Entity\Channel; use Slovocast\Domain\Entity\User; use Slovocast\Exception\EntityNotFoundException; -use Slovocast\Infrastructure\Api\Database\DatabaseConnectionInterface; +use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface; class ChannelRepository implements ChannelRepositoryInterface { public function __construct( - protected DatabaseConnectionInterface $db + protected DatabaseHandlerInterface $db ) { } /** diff --git a/app/src/Domain/Repository/User/UserRepository.php b/app/src/Domain/Repository/User/UserRepository.php index e93736f..c2aff85 100644 --- a/app/src/Domain/Repository/User/UserRepository.php +++ b/app/src/Domain/Repository/User/UserRepository.php @@ -4,13 +4,13 @@ namespace Slovocast\Domain\Repository\User; use Slovocast\Domain\Entity\User; use Slovocast\Exception\EntityNotFoundException; -use Slovocast\Infrastructure\Api\Database\DatabaseConnectionInterface; use Slovocast\Infrastructure\Api\User\UserAuthorizationInterface; +use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface; class UserRepository implements UserRepositoryInterface { public function __construct( - private DatabaseConnectionInterface $db, + private DatabaseHandlerInterface $db, private UserAuthorizationInterface $userAuth ) {} diff --git a/app/src/Infrastructure/Api/Database/DatabaseConnectionInterface.php b/app/src/Infrastructure/Api/Database/DatabaseHandlerInterface.php similarity index 53% rename from app/src/Infrastructure/Api/Database/DatabaseConnectionInterface.php rename to app/src/Infrastructure/Api/Database/DatabaseHandlerInterface.php index c8112a4..494eece 100644 --- a/app/src/Infrastructure/Api/Database/DatabaseConnectionInterface.php +++ b/app/src/Infrastructure/Api/Database/DatabaseHandlerInterface.php @@ -4,8 +4,8 @@ namespace Slovocast\Infrastructure\Api\Database; use PDO; -interface DatabaseConnectionInterface +interface DatabaseHandlerInterface { public function getConnection(): PDO; - public function setNewConnection(PDO $connection): void; + public function getDsn(): string; } diff --git a/app/src/Infrastructure/Database/DatabaseConnection.php b/app/src/Infrastructure/Database/DatabaseConnection.php index 9be9081..a2203e5 100644 --- a/app/src/Infrastructure/Database/DatabaseConnection.php +++ b/app/src/Infrastructure/Database/DatabaseConnection.php @@ -2,22 +2,21 @@ namespace Slovocast\Infrastructure\Database; -use Slovocast\Infrastructure\Api\Database\DatabaseConnectionInterface; +use Slovocast\Infrastructure\Api\Database\DatabaseHandlerInterface; use PDO; -class DatabaseConnection implements DatabaseConnectionInterface +class DatabaseConnection implements DatabaseHandlerInterface { private PDO $pdo; public function __construct( - string $host, - string $username, - string $password, - string $database, - int $port = 3306 + private string $host, + private string $username, + private string $password, + private string $database, + private int $port = 3306 ) { - $dsn = "mysql:dbname={$database};host={$host};port={$port}"; - $this->pdo = new PDO($dsn. $username, $password); + $this->pdo = new PDO($this->getDsn(), $username, $password); } public function getConnection(): PDO @@ -25,8 +24,18 @@ class DatabaseConnection implements DatabaseConnectionInterface return $this->pdo; } - public function setNewConnection(PDO $pdo): void + public function getDsn(): string { - $this->pdo = $pdo; + return "mysql:dbname={$this->database};host={$this->host};port={$this->port}"; + } + + public function getHost(): string + { + return $this->host; + } + + public function getDatabase(): string + { + return $this->database; } } diff --git a/docker-compose.yml b/docker-compose.yml index 12b0ddf..31868bf 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,6 +7,8 @@ services: - './app:/var/www/slovocast' depends_on: - mariadb + links: + - mariadb nginx: image: nginx:latest volumes: @@ -19,11 +21,13 @@ services: mariadb: image: mariadb:10.9 environment: - MYSQL_ROOT_PASSWORD: "P4ssword!" - MYSQL_DATABASE: "slovocast" - MYSQL_USER: "slovocast" - MYSQL_PASSWORD: "slovocast" + MARIADB_ROOT_PASSWORD: password + MARIADB_DATABASE: slovocast + MARIADB_USER: slovocast + MARIADB_PASSWORD: password volumes: - "slovocast_data:/var/lib/mysql" + ports: + - "3306:3306" volumes: slovocast_data: