refactor the user repository to use the new MySQL client.
This commit is contained in:
parent
e904b27017
commit
9ab1482acf
@ -15,7 +15,7 @@
|
||||
"dotenv-org/phpdotenv-vault": "^0.2.4",
|
||||
"react/react": "^1.4",
|
||||
"robmorgan/phinx": "^0.16.1",
|
||||
"react/mysql": "^0.6.0"
|
||||
"react/mysql": "^0.7dev"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^11.1",
|
||||
|
21
app/composer.lock
generated
21
app/composer.lock
generated
@ -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": "82f7933269cf85de8e7e915a83d6d715",
|
||||
"content-hash": "525ed6c2ef7a52e8447108c1427f1b7d",
|
||||
"packages": [
|
||||
{
|
||||
"name": "cakephp/chronos",
|
||||
@ -2376,16 +2376,16 @@
|
||||
},
|
||||
{
|
||||
"name": "react/mysql",
|
||||
"version": "v0.6.0",
|
||||
"version": "0.7.x-dev",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/friends-of-reactphp/mysql.git",
|
||||
"reference": "914ff50ebc15934a3847a90f032cccaac215146f"
|
||||
"reference": "b01e1cec90217662f01e3c8a990aafc18c9c79a1"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/friends-of-reactphp/mysql/zipball/914ff50ebc15934a3847a90f032cccaac215146f",
|
||||
"reference": "914ff50ebc15934a3847a90f032cccaac215146f",
|
||||
"url": "https://api.github.com/repos/friends-of-reactphp/mysql/zipball/b01e1cec90217662f01e3c8a990aafc18c9c79a1",
|
||||
"reference": "b01e1cec90217662f01e3c8a990aafc18c9c79a1",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
@ -2401,10 +2401,11 @@
|
||||
"phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36",
|
||||
"react/async": "^4 || ^3 || ^2"
|
||||
},
|
||||
"default-branch": true,
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"React\\MySQL\\": "src/"
|
||||
"React\\Mysql\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
@ -2420,9 +2421,9 @@
|
||||
],
|
||||
"support": {
|
||||
"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",
|
||||
@ -6540,7 +6541,9 @@
|
||||
],
|
||||
"aliases": [],
|
||||
"minimum-stability": "stable",
|
||||
"stability-flags": [],
|
||||
"stability-flags": {
|
||||
"react/mysql": 20
|
||||
},
|
||||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
"platform": [],
|
||||
|
@ -19,16 +19,14 @@ use Slim\Views\TwigMiddleware;
|
||||
use Slim\Psr7\Factory\ResponseFactory;
|
||||
|
||||
use Slovocast\Routes;
|
||||
use Slovocast\Configuration\{
|
||||
SiteInformationSchema,
|
||||
DatabaseConnectionSchema,
|
||||
SessionSchema
|
||||
};
|
||||
use Slovocast\Configuration\SiteInformationSchema;
|
||||
use Slovocast\Configuration\DatabaseConnectionSchema;
|
||||
use Slovocast\Configuration\SessionSchema;
|
||||
|
||||
use Twig\Error\LoaderError;
|
||||
use Slovocast\Domain\Repository\{
|
||||
UserRepositoryInterface,
|
||||
UserRepository
|
||||
};
|
||||
use Slovocast\Domain\Repository\UserRepositoryInterface;
|
||||
use Slovocast\Domain\Repository\UserRepository;
|
||||
|
||||
use Slovocast\Infrastructure\User\UserAuthorizationInterface;
|
||||
use Slovocast\Infrastructure\User\BasicUserAuthorization;
|
||||
|
||||
@ -141,8 +139,8 @@ class Bootstrap
|
||||
$config = $container->get('config')->get('database');
|
||||
$connectionString = sprintf(
|
||||
"%s:%s@%s/%s",
|
||||
$config['username'],
|
||||
$config['password'],
|
||||
rawurlencode($config['username']),
|
||||
rawurlencode($config['password']),
|
||||
$config['host'],
|
||||
$config['database']
|
||||
);
|
||||
|
@ -2,12 +2,11 @@
|
||||
|
||||
namespace Slovocast\Domain\Repository;
|
||||
|
||||
use function React\Async\await;
|
||||
use React\Mysql\MysqlClient;
|
||||
use React\Mysql\MysqlResult;
|
||||
use Slovocast\Infrastructure\User\UserAuthorizationInterface;
|
||||
use Slovocast\Exception\EntityNotFoundException;
|
||||
use Slovocast\Domain\Entity\User;
|
||||
use PDO;
|
||||
|
||||
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.
|
||||
*
|
||||
@ -64,14 +40,9 @@ class UserRepository implements UserRepositoryInterface
|
||||
*/
|
||||
public function get(int $id): User
|
||||
{
|
||||
$query = "SELECT * FROM users WHERE id = :id LIMIT 1";
|
||||
$userData = $this->queryForUser(
|
||||
$query,
|
||||
[ 'id' => $id ],
|
||||
$this->database->getConnection()
|
||||
);
|
||||
|
||||
return $this->userFromQueryResults($userData);
|
||||
$query = "SELECT * FROM users WHERE id = ? LIMIT 1";
|
||||
$results = await($this->db->query($query, [ $id ]));
|
||||
return $this->userFromQueryResults($results->resultRows[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -82,14 +53,14 @@ class UserRepository implements UserRepositoryInterface
|
||||
*/
|
||||
public function getFromEmail(string $email): User
|
||||
{
|
||||
$query = "SELECT * FROM users WHERE email = :email LIMIT 1";
|
||||
$userData = $this->queryForUser(
|
||||
$query,
|
||||
[ 'email' => $email ],
|
||||
$this->database->getConnection()
|
||||
);
|
||||
$query = "SELECT * FROM users WHERE email = ? LIMIT 1";
|
||||
$results = await($this->db->query($query, [ $email ]));
|
||||
|
||||
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
|
||||
{
|
||||
$connection = $this->database->getConnection();
|
||||
$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([
|
||||
'email' => $user->getEmail(),
|
||||
'name'=> $user->getName(),
|
||||
'password' => $this->userAuth->hash($user->getPassword())
|
||||
]);
|
||||
return (bool) $results->insertId;
|
||||
}
|
||||
|
||||
public function update(User $user): bool
|
||||
{
|
||||
$connection = $this->database->getConnection();
|
||||
$query = "UPDATE users
|
||||
SET email = :email,
|
||||
name = :name,
|
||||
password = :password
|
||||
WHERE id = :id";
|
||||
SET email = ?,
|
||||
name = ?,
|
||||
password = ?
|
||||
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([
|
||||
'email' => $user->getEmail(),
|
||||
'name' => $user->getName(),
|
||||
'password' => $this->userAuth->hash($user->getPassword()),
|
||||
'id' => $user->getId()
|
||||
]);
|
||||
return (bool) $results->affectedRows;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user