Skip to content

Commit 9c76567

Browse files
committed
refactor: better ADR pattern try
1 parent 5d4308f commit 9c76567

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

src/Controller/ListUsersAction.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
namespace App\Controller;
66

7-
use App\Repository\UserRepository;
8-
use Doctrine\DBAL\Connection;
7+
use App\Data\Controller\ListUsersActionData;
98
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
109
use Symfony\Component\HttpFoundation\Response;
1110
use Symfony\Component\HttpKernel\Attribute\AsController;
@@ -15,11 +14,10 @@
1514
final class ListUsersAction extends AbstractController
1615
{
1716
#[Route(path: '/users', name: self::class)]
18-
public function __invoke(Connection $conn, UserRepository $userRepository): Response
17+
public function __invoke(ListUsersActionData $listUsersActionData): Response
1918
{
2019
return $this->render(self::class.'.html.twig', [
21-
'users_dbal' => $conn->fetchAllAssociative('SELECT * FROM user'),
22-
'users_orm' => $userRepository->findAll(),
20+
'data' => $listUsersActionData->getData(),
2321
]);
2422
}
2523
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Controller;
6+
7+
use App\Entity\User;
8+
9+
final class ListUsersActionDto
10+
{
11+
/**
12+
* @param list<array<string, mixed>> $usersDbal
13+
* @param array<User> $usersOrm
14+
*/
15+
public function __construct(
16+
public array $usersDbal,
17+
public array $usersOrm,
18+
) {
19+
}
20+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Data\Controller;
6+
7+
use App\Controller\ListUsersActionDto;
8+
use App\Repository\UserRepository;
9+
use Doctrine\DBAL\Connection;
10+
11+
final readonly class ListUsersActionData
12+
{
13+
public function __construct(
14+
private Connection $conn,
15+
private UserRepository $userRepository,
16+
) {
17+
}
18+
19+
public function getData(): ListUsersActionDto
20+
{
21+
return new ListUsersActionDto(
22+
usersDbal: $this->conn->fetchAllAssociative('SELECT * FROM user'),
23+
usersOrm: $this->userRepository->findAll(),
24+
);
25+
}
26+
}

templates/App/Controller/ListUsersAction.html.twig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<h3>Doctrine DBAL</h3>
99

1010
<code>
11-
'users_dbal' => $conn->fetchAllAssociative('SELECT * FROM user'),
11+
usersDbal: $this->conn->fetchAllAssociative('SELECT * FROM user'),
1212
</code>
1313

1414
<table class="striped">
@@ -22,7 +22,7 @@
2222
</tr>
2323
</thead>
2424
<tbody>
25-
{% for user in users_dbal %}
25+
{% for user in data.usersDbal %}
2626
<tr>
2727
<td>{{ user.id }}</td>
2828
<td>{{ user.email }}</td>
@@ -35,7 +35,7 @@
3535
<tfoot>
3636
<tr>
3737
<th colspan="5" scope="row">
38-
There are <b>{{ users_dbal|length }}</b> user(s).
38+
There are <b>{{ data.usersDbal|length }}</b> user(s).
3939
</th>
4040
</tr>
4141
</tfoot>
@@ -46,7 +46,7 @@
4646
<h3>Doctrine ORM</h3>
4747

4848
<code>
49-
'users_orm' => $userRepository->findAll(),
49+
usersOrm: $this->userRepository->findAll(),
5050
</code>
5151

5252
<table class="striped">
@@ -60,7 +60,7 @@
6060
</tr>
6161
</thead>
6262
<tbody>
63-
{% for user in users_orm %}
63+
{% for user in data.usersOrm %}
6464
<tr>
6565
<td>{{ user.id }}</td>
6666
<td>{{ user.email }}</td>
@@ -73,7 +73,7 @@
7373
<tfoot>
7474
<tr>
7575
<th colspan="5" scope="row">
76-
There are <b>{{ users_orm|length }}</b> user(s).
76+
There are <b>{{ data.usersOrm|length }}</b> user(s).
7777
</th>
7878
</tr>
7979
</tfoot>

0 commit comments

Comments
 (0)