From 20cfdbb61bac6d8c435a7285f553122e2a52e953 Mon Sep 17 00:00:00 2001 From: samuelgfeller Date: Mon, 1 Apr 2024 14:07:16 +0200 Subject: [PATCH] Removed querybuilder [SLE-197] --- .scrutinizer.yml | 3 +- public/frontend/home.html | 2 +- .../User/Repository/UserCreatorRepository.php | 13 +- .../User/Repository/UserFinderRepository.php | 8 +- src/Infrastructure/Factory/QueryFactory.php | 120 ------------------ .../NonFatalErrorHandlerMiddlewareTest.php | 42 ++++++ 6 files changed, 57 insertions(+), 131 deletions(-) delete mode 100644 src/Infrastructure/Factory/QueryFactory.php create mode 100644 tests/Integration/ErrorHandler/NonFatalErrorHandlerMiddlewareTest.php diff --git a/.scrutinizer.yml b/.scrutinizer.yml index e2ebdd6..a2f59fd 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -1,6 +1,7 @@ filter: paths: [ "src/*" ] - excluded_paths: [ "vendor/*", "tests/*", "resources/", "public/" ] + excluded_paths: [ "vendor/*", "tests/*", "resources/", "public/", "src/Infrastructure/Console/**", + "src/Application/ErrorHandler/**", ] checks: php: diff --git a/public/frontend/home.html b/public/frontend/home.html index 680b3d3..a9b6ffc 100644 --- a/public/frontend/home.html +++ b/public/frontend/home.html @@ -4,7 +4,7 @@ - Example Frontend for Slim API Starter + Slim API Starter - frontend

Frontend for Slim API Starter

diff --git a/src/Domain/User/Repository/UserCreatorRepository.php b/src/Domain/User/Repository/UserCreatorRepository.php index 5db24f5..3e36a84 100644 --- a/src/Domain/User/Repository/UserCreatorRepository.php +++ b/src/Domain/User/Repository/UserCreatorRepository.php @@ -2,13 +2,12 @@ namespace App\Domain\User\Repository; -use App\Infrastructure\Factory\QueryFactory; +use Cake\Database\Connection; final readonly class UserCreatorRepository { - public function __construct( - private QueryFactory $queryFactory, - ) { + public function __construct(private Connection $connection) + { } /** @@ -21,6 +20,10 @@ public function __construct( public function insertUser(array $userValues): int { // Insert user into database - return (int)$this->queryFactory->insertQueryWithData($userValues)->into('user')->execute()->lastInsertId(); + return (int)$this->connection->insertQuery() + ->insert(array_keys($userValues)) + ->values($userValues) + ->into('user') + ->execute()->lastInsertId(); } } diff --git a/src/Domain/User/Repository/UserFinderRepository.php b/src/Domain/User/Repository/UserFinderRepository.php index 05dd3e0..fa0c8d1 100644 --- a/src/Domain/User/Repository/UserFinderRepository.php +++ b/src/Domain/User/Repository/UserFinderRepository.php @@ -3,14 +3,14 @@ namespace App\Domain\User\Repository; use App\Domain\User\Data\UserData; -use App\Infrastructure\Factory\QueryFactory; use App\Infrastructure\Utility\Hydrator; +use Cake\Database\Connection; final readonly class UserFinderRepository { public function __construct( - private QueryFactory $queryFactory, - private Hydrator $hydrator + private Connection $connection, + private Hydrator $hydrator, ) { } @@ -21,7 +21,7 @@ public function __construct( */ public function findAllUsers(): array { - $query = $this->queryFactory->selectQuery()->select([ + $query = $this->connection->selectQuery()->select([ 'id', 'first_name', 'last_name', diff --git a/src/Infrastructure/Factory/QueryFactory.php b/src/Infrastructure/Factory/QueryFactory.php deleted file mode 100644 index 5fedca1..0000000 --- a/src/Infrastructure/Factory/QueryFactory.php +++ /dev/null @@ -1,120 +0,0 @@ -queryFactory->selectQuery()->select(['*'])->from('user')->where( - * ['deleted_at IS' => null, 'name LIKE' => '%John%']); - * return $query->execute()->fetchAll('assoc'); - * - * @return SelectQuery - */ - public function selectQuery(): SelectQuery - { - return $this->connection->selectQuery(); - } - - /** - * Returns an update query instance. - * - * UPDATE example: - * $query = $this->queryFactory->updateQuery()->update('user')->set($data)->where(['id' => 1]); - * return $query->execute()->rowCount() > 0; - * - * @return UpdateQuery - */ - public function updateQuery(): UpdateQuery - { - return $this->connection->updateQuery(); - } - - /** - * Returns an insert query instance. - * - * @return InsertQuery the insert query object - */ - public function insertQuery(): InsertQuery - { - return $this->connection->insertQuery(); - } - - /** - * Data is an assoc array of a row to insert where the key is the column name. - * - * Example usage: - * return (int)$this->queryFactory->insertQueryWithData($data)->into('user')->execute()->lastInsertId();. - * - * @param array $data ['col_name' => 'Value', 'other_col' => 'Other value'] - * - * @return InsertQuery - */ - public function insertQueryWithData(array $data): InsertQuery - { - return $this->connection->insertQuery()->insert(array_keys($data))->values($data); - } - - /** - * Soft deletes entry from given table name. - * - * Example usage: - * $query = $this->queryFactory->softDeleteQuery('user')->where(['id' => $id]); - * return $query->execute()->rowCount() > 0;. - * - * @param string $fromTable - * - * @return UpdateQuery - */ - public function softDeleteQuery(string $fromTable): UpdateQuery - { - return $this->connection->updateQuery()->update($fromTable)->set(['deleted_at' => date('Y-m-d H:i:s')]); - } - - /** - * Returns a delete query instance for hard deletion. - * - * @return Query\DeleteQuery the delete query object - */ - public function hardDeleteQuery(): Query\DeleteQuery - { - // Return the delete query object created by the connection. - return $this->connection->deleteQuery(); - } - - /** - * Data is an assoc array of rows to insert where the key is the column name - * Example usage: - * return (int)$this->queryFactory->newMultipleInsert($data)->into('user')->execute()->lastInsertId();. - * - * @param array $arrayOfData [['col_name' => 'Value', 'other_col' => 'Other value'], ['col_name' => 'value']] - * - * @return InsertQuery - */ - public function insertQueryMultipleRows(array $arrayOfData): InsertQuery - { - $query = $this->connection->insertQuery()->insert(array_keys($arrayOfData[array_key_first($arrayOfData)])); - // According to the docs, chaining ->values is the way to go https://book.cakephp.org/4/en/orm/query-builder.html#inserting-data - foreach ($arrayOfData as $data) { - $query->values($data); - } - - return $query; - } -} diff --git a/tests/Integration/ErrorHandler/NonFatalErrorHandlerMiddlewareTest.php b/tests/Integration/ErrorHandler/NonFatalErrorHandlerMiddlewareTest.php new file mode 100644 index 0000000..485d9c3 --- /dev/null +++ b/tests/Integration/ErrorHandler/NonFatalErrorHandlerMiddlewareTest.php @@ -0,0 +1,42 @@ +app->get('/error', function ($request, $response, $args) { + // This will trigger a PHP notice because $undefinedVar is not defined + /** @phpstan-ignore-next-line */ + echo $undefinedVar; + + return $response; + }); + + $request = $this->createRequest('GET', '/error'); + + // Expect that an ErrorException is thrown + $this->expectException(\ErrorException::class); + $this->expectExceptionMessage('Undefined variable $undefinedVar'); + + // Process the request through the application + $this->app->handle($request); + } +}