refactor the user repository to use the new MySQL client.

This commit is contained in:
Dave Smith-Hayes 2024-06-26 22:09:43 -04:00
parent e904b27017
commit 9ab1482acf
4 changed files with 51 additions and 81 deletions

View File

@ -15,7 +15,7 @@
"dotenv-org/phpdotenv-vault": "^0.2.4", "dotenv-org/phpdotenv-vault": "^0.2.4",
"react/react": "^1.4", "react/react": "^1.4",
"robmorgan/phinx": "^0.16.1", "robmorgan/phinx": "^0.16.1",
"react/mysql": "^0.6.0" "react/mysql": "^0.7dev"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^11.1", "phpunit/phpunit": "^11.1",

21
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", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "82f7933269cf85de8e7e915a83d6d715", "content-hash": "525ed6c2ef7a52e8447108c1427f1b7d",
"packages": [ "packages": [
{ {
"name": "cakephp/chronos", "name": "cakephp/chronos",
@ -2376,16 +2376,16 @@
}, },
{ {
"name": "react/mysql", "name": "react/mysql",
"version": "v0.6.0", "version": "0.7.x-dev",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/friends-of-reactphp/mysql.git", "url": "https://github.com/friends-of-reactphp/mysql.git",
"reference": "914ff50ebc15934a3847a90f032cccaac215146f" "reference": "b01e1cec90217662f01e3c8a990aafc18c9c79a1"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/friends-of-reactphp/mysql/zipball/914ff50ebc15934a3847a90f032cccaac215146f", "url": "https://api.github.com/repos/friends-of-reactphp/mysql/zipball/b01e1cec90217662f01e3c8a990aafc18c9c79a1",
"reference": "914ff50ebc15934a3847a90f032cccaac215146f", "reference": "b01e1cec90217662f01e3c8a990aafc18c9c79a1",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -2401,10 +2401,11 @@
"phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36", "phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36",
"react/async": "^4 || ^3 || ^2" "react/async": "^4 || ^3 || ^2"
}, },
"default-branch": true,
"type": "library", "type": "library",
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"React\\MySQL\\": "src/" "React\\Mysql\\": "src/"
} }
}, },
"notification-url": "https://packagist.org/downloads/", "notification-url": "https://packagist.org/downloads/",
@ -2420,9 +2421,9 @@
], ],
"support": { "support": {
"issues": "https://github.com/friends-of-reactphp/mysql/issues", "issues": "https://github.com/friends-of-reactphp/mysql/issues",
"source": "https://github.com/friends-of-reactphp/mysql/tree/v0.6.0" "source": "https://github.com/friends-of-reactphp/mysql/tree/0.7.x"
}, },
"time": "2023-11-10T12:08:50+00:00" "time": "2024-04-28T21:27:01+00:00"
}, },
{ {
"name": "react/promise", "name": "react/promise",
@ -6540,7 +6541,9 @@
], ],
"aliases": [], "aliases": [],
"minimum-stability": "stable", "minimum-stability": "stable",
"stability-flags": [], "stability-flags": {
"react/mysql": 20
},
"prefer-stable": false, "prefer-stable": false,
"prefer-lowest": false, "prefer-lowest": false,
"platform": [], "platform": [],

View File

@ -19,16 +19,14 @@ use Slim\Views\TwigMiddleware;
use Slim\Psr7\Factory\ResponseFactory; use Slim\Psr7\Factory\ResponseFactory;
use Slovocast\Routes; use Slovocast\Routes;
use Slovocast\Configuration\{ use Slovocast\Configuration\SiteInformationSchema;
SiteInformationSchema, use Slovocast\Configuration\DatabaseConnectionSchema;
DatabaseConnectionSchema, use Slovocast\Configuration\SessionSchema;
SessionSchema
};
use Twig\Error\LoaderError; use Twig\Error\LoaderError;
use Slovocast\Domain\Repository\{ use Slovocast\Domain\Repository\UserRepositoryInterface;
UserRepositoryInterface, use Slovocast\Domain\Repository\UserRepository;
UserRepository
};
use Slovocast\Infrastructure\User\UserAuthorizationInterface; use Slovocast\Infrastructure\User\UserAuthorizationInterface;
use Slovocast\Infrastructure\User\BasicUserAuthorization; use Slovocast\Infrastructure\User\BasicUserAuthorization;
@ -141,8 +139,8 @@ class Bootstrap
$config = $container->get('config')->get('database'); $config = $container->get('config')->get('database');
$connectionString = sprintf( $connectionString = sprintf(
"%s:%s@%s/%s", "%s:%s@%s/%s",
$config['username'], rawurlencode($config['username']),
$config['password'], rawurlencode($config['password']),
$config['host'], $config['host'],
$config['database'] $config['database']
); );

View File

@ -2,12 +2,11 @@
namespace Slovocast\Domain\Repository; namespace Slovocast\Domain\Repository;
use function React\Async\await;
use React\Mysql\MysqlClient; use React\Mysql\MysqlClient;
use React\Mysql\MysqlResult;
use Slovocast\Infrastructure\User\UserAuthorizationInterface; use Slovocast\Infrastructure\User\UserAuthorizationInterface;
use Slovocast\Exception\EntityNotFoundException; use Slovocast\Exception\EntityNotFoundException;
use Slovocast\Domain\Entity\User; use Slovocast\Domain\Entity\User;
use PDO;
class UserRepository implements UserRepositoryInterface class UserRepository implements UserRepositoryInterface
{ {
@ -33,29 +32,6 @@ class UserRepository implements UserRepositoryInterface
]); ]);
} }
/**
* @param string $query The Query for getting a User
* @param array $params The parameters in the query
* @param PDO $connection The PDO connection
* @return array The column data from the Database
* @throws EntityNotFoundException The User does no exist
*/
protected function queryForUser(
string $query,
array $params,
PDO $connection
): array {
$statement = $connection->prepare($query);
$statement->execute($params);
$results = $statement->fetchAll();
if (!$results) {
throw new EntityNotFoundException("Unable to find User");
}
return array_shift($results);
}
/** /**
* Get a single instance of the User Entity. * Get a single instance of the User Entity.
* *
@ -64,14 +40,9 @@ class UserRepository implements UserRepositoryInterface
*/ */
public function get(int $id): User public function get(int $id): User
{ {
$query = "SELECT * FROM users WHERE id = :id LIMIT 1"; $query = "SELECT * FROM users WHERE id = ? LIMIT 1";
$userData = $this->queryForUser( $results = await($this->db->query($query, [ $id ]));
$query, return $this->userFromQueryResults($results->resultRows[0]);
[ 'id' => $id ],
$this->database->getConnection()
);
return $this->userFromQueryResults($userData);
} }
/** /**
@ -82,14 +53,14 @@ class UserRepository implements UserRepositoryInterface
*/ */
public function getFromEmail(string $email): User public function getFromEmail(string $email): User
{ {
$query = "SELECT * FROM users WHERE email = :email LIMIT 1"; $query = "SELECT * FROM users WHERE email = ? LIMIT 1";
$userData = $this->queryForUser( $results = await($this->db->query($query, [ $email ]));
$query,
[ 'email' => $email ],
$this->database->getConnection()
);
return $this->userFromQueryResults($userData); if (count($results->resultRows)) {
throw new EntityNotFoundException("Entity does not exist.");
}
return $this->userFromQueryResults($results->resultRows[0]);
} }
/** /**
@ -98,36 +69,34 @@ class UserRepository implements UserRepositoryInterface
*/ */
public function save(User $user): bool public function save(User $user): bool
{ {
$connection = $this->database->getConnection();
$query = "INSERT INTO users (email, password, name) $query = "INSERT INTO users (email, password, name)
VALUES (:email, :password, :name)"; VALUES (?, ?, ?)";
$statement = $connection->prepare($query); $results = await($this->db->query($query, [
$user->getEmail(),
$this->userAuth->hash($user->getPassword()),
$user->getName(),
]));
return $statement->execute([ return (bool) $results->insertId;
'email' => $user->getEmail(),
'name'=> $user->getName(),
'password' => $this->userAuth->hash($user->getPassword())
]);
} }
public function update(User $user): bool public function update(User $user): bool
{ {
$connection = $this->database->getConnection();
$query = "UPDATE users $query = "UPDATE users
SET email = :email, SET email = ?,
name = :name, name = ?,
password = :password password = ?
WHERE id = :id"; WHERE id = ?";
$statement = $connection->prepare($query); $results = await($this->db->query($query, [
$user->getEmail(),
$user->getName(),
$this->userAuth->hash($user->getPassword()),
$user->getId()
]));
return $statement->execute([ return (bool) $results->affectedRows;
'email' => $user->getEmail(),
'name' => $user->getName(),
'password' => $this->userAuth->hash($user->getPassword()),
'id' => $user->getId()
]);
} }
/** /**