Skip to content

Commit 18125a9

Browse files
committed
Add new classes for User reading
1 parent 6f18eed commit 18125a9

File tree

7 files changed

+171
-0
lines changed

7 files changed

+171
-0
lines changed

src/Action/User/ListUsersAction.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace App\Action\User;

src/Action/User/UserAction.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace App\Action\User;

src/Action/User/UserReadAction.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Action\User;
6+
7+
use App\Domain\User\Service\UserReader;
8+
use Psr\Http\Message\ResponseInterface;
9+
use Psr\Http\Message\ServerRequestInterface;
10+
11+
final class UserReadAction
12+
{
13+
private UserReader $userReader;
14+
15+
public function __construct(UserReader $userReader)
16+
{
17+
$this->userReader = $userReader;
18+
}
19+
20+
/**
21+
* Invoke.
22+
*
23+
* @param ServerRequestInterface $request The request
24+
* @param ResponseInterface $response The response
25+
* @param array<mixed> $args The route arguments
26+
*
27+
* @return ResponseInterface The response
28+
*/
29+
public function __invoke(
30+
ServerRequestInterface $request,
31+
ResponseInterface $response,
32+
array $args = []
33+
): ResponseInterface {
34+
// Collect input from the HTTP request
35+
$userId = (int)$args['id'];
36+
37+
// Invoke the Domain (application service) with inputs and keep the result
38+
$user = $this->userReader->getUserDetails($userId);
39+
40+
// Transform the result into the JSON representation
41+
$result = [
42+
'user_id' => $user->id,
43+
'username' => $user->username,
44+
'first_name' => $user->firstName,
45+
'last_name' => $user->lastName,
46+
'email' => $user->email,
47+
];
48+
49+
// Build the HTTP response
50+
$response->getBody()->write((string)json_encode($result));
51+
52+
return $response->withHeader('Content-Type', 'application/json')->withStatus(200);
53+
}
54+
}

src/Action/User/ViewUserAction.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace App\Action\User;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Domain\User\Data;
6+
7+
final class UserReaderResult
8+
{
9+
public int $id;
10+
11+
public string $username;
12+
13+
public string $firstName;
14+
15+
public string $lastName;
16+
17+
public string $email;
18+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace App\Domain\User\Repository;
4+
5+
use App\Domain\User\Data\UserReaderResult;
6+
use DomainException;
7+
use PDO;
8+
9+
class UserReaderRepository
10+
{
11+
private PDO $connection;
12+
13+
/**
14+
* Constructor.
15+
*
16+
* @param PDO $connection The database connection
17+
*/
18+
public function __construct(PDO $connection)
19+
{
20+
$this->connection = $connection;
21+
}
22+
23+
/**
24+
* Get user by the given user id.
25+
*
26+
* @param int $userId The user id
27+
*
28+
* @throws DomainException
29+
*
30+
* @return array The user row
31+
*/
32+
public function getUserById(int $userId): array
33+
{
34+
$sql = "SELECT id, username, first_name, last_name, email FROM users WHERE id = :id;";
35+
$statement = $this->connection->prepare($sql);
36+
$statement->execute(['id' => $userId]);
37+
38+
$row = $statement->fetch();
39+
40+
if (!$row) {
41+
throw new DomainException(sprintf('User not found: %s', $userId));
42+
}
43+
44+
return $row;
45+
}
46+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Domain\User\Service;
6+
7+
use App\Domain\User\Data\UserReaderResult;
8+
use App\Domain\User\Repository\UserReaderRepository;
9+
use App\Exception\ValidationException;
10+
11+
final class UserReader
12+
{
13+
private UserReaderRepository $repository;
14+
15+
public function __construct(UserReaderRepository $repository)
16+
{
17+
$this->repository = $repository;
18+
}
19+
20+
/**
21+
* Read a user by the given user id.
22+
*
23+
* @param int $userId The user id
24+
*
25+
* @throws ValidationException
26+
*
27+
* @return UserReaderResult The user data
28+
*/
29+
public function getUserDetails(int $userId): UserReaderResult
30+
{
31+
// Input validation
32+
if (empty($userId)) {
33+
throw new ValidationException('User ID required');
34+
}
35+
36+
$userRow = $this->repository->getUserById($userId);
37+
38+
// Optional: Do something complex here...
39+
40+
// Map array to data object
41+
$user = new UserReaderResult();
42+
$user->id = (int)$userRow['id'];
43+
$user->username = (string)$userRow['username'];
44+
$user->firstName = (string)$userRow['first_name'];
45+
$user->lastName = (string)$userRow['last_name'];
46+
$user->email = (string)$userRow['email'];
47+
48+
return $user;
49+
}
50+
}

0 commit comments

Comments
 (0)