Skip to content

Commit 58ce70d

Browse files
authored
Merge pull request #20 from sweetspotio/feature/collection-query-bus-example
Collection query bus example
2 parents 74bba05 + 3ebf140 commit 58ce70d

File tree

8 files changed

+110
-14
lines changed

8 files changed

+110
-14
lines changed

config/api_platform/User/User.yaml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ Acme\Domain\User\User:
55
collectionOperations:
66
get:
77
method: GET
8-
output: Acme\UI\Http\Rest\Presentation\User\UserView
9-
normalization_context:
10-
groups: [ profile ]
11-
get_v2:
12-
path: v2/users
13-
method: GET
8+
filters: [ 'user.search_filter' ]
9+
output: Acme\UI\Http\Rest\Presentation\User\UserView
10+
normalization_context:
11+
groups: [ profile ]
12+
get_v2:
13+
path: v2/users
14+
method: GET
15+
query: Acme\Application\UseCase\Query\User\GetUsers\GetUsersQuery
16+
filters: [ 'user.search_filter' ]
1417
output: Acme\UI\Http\Rest\Presentation\User\UserProfileView
1518
post:
1619
method: POST

config/packages/api_platform.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,20 @@ api_platform:
1212
jsonhal: [ 'application/hal+json' ]
1313
jsonld: [ 'application/ld+json' ]
1414
html: [ 'text/html' ]
15+
collection:
16+
pagination:
17+
client_items_per_page: true
18+
items_per_page_parameter_name: limit
19+
items_per_page: 50
1520
swagger:
1621
versions: [3]
1722
api_keys:
1823
Bearer:
1924
name: Bearer
2025
type: header
26+
27+
path_segment_name_generator: api_platform.path_segment_name_generator.dash
28+
2129
exception_to_status:
2230
# The 4 following handlers are registered by default, keep those lines to prevent unexpected side effects
2331
Symfony\Component\Serializer\Exception\ExceptionInterface: 400 # Use a raw status code (recommended)

config/services.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,20 @@ services:
5858
resource: '../src/UI/Http/Web/Controller/*'
5959
tags: [ 'controller.service_arguments' ]
6060

61+
Acme\Application\UseCase\Query\CollectionQueryFactory:
62+
arguments:
63+
$collectionOptions: '%api_platform.collection.pagination%'
64+
65+
acme.user_resource.search_filter:
66+
parent: 'api_platform.doctrine.orm.search_filter'
67+
arguments: [ { credentials.email: "partial" } ]
68+
tags: [ { name: 'api_platform.filter', id: 'user.search_filter' } ]
69+
# The following are mandatory only if a _defaults section is defined
70+
# You may want to isolate filters in a dedicated file to avoid adding them
71+
autowire: false
72+
autoconfigure: false
73+
74+
Acme\Application\UseCase\Query\User\GetUsers\GetUsersQueryHandler:
75+
arguments: ['api_platform.doctrine.orm.default.collection_data_provider']
6176
# add more service definitions when explicit configuration is needed
6277
# please note that last definitions always *replace* previous ones
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Acme\Application\UseCase\Query\User\GetUsers;
6+
7+
use Acme\Application\UseCase\Query\CollectionQuery;
8+
use Acme\Domain\User\User;
9+
10+
class GetUsersQuery extends CollectionQuery
11+
{
12+
public static function createWithContext(array $context): CollectionQuery
13+
{
14+
return new self($context);
15+
}
16+
17+
public static function resourceClass(): string
18+
{
19+
return User::class;
20+
}
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Acme\Application\UseCase\Query\User\GetUsers;
6+
7+
use Acme\Application\UseCase\Query\CollectionQueryFactory;
8+
use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
9+
10+
class GetUsersQueryDataTransformer implements DataTransformerInterface
11+
{
12+
private CollectionQueryFactory $factory;
13+
14+
public function __construct(CollectionQueryFactory $factory)
15+
{
16+
$this->factory = $factory;
17+
}
18+
19+
public function supportsTransformation($data, string $to, array $context = []): bool
20+
{
21+
return GetUsersQuery::class === ($context['query'] ?? false);
22+
}
23+
24+
public function transform($object, string $to, array $context = [])
25+
{
26+
return $this->factory->createCollectionQuery(GetUsersQuery::class, $context);
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Acme\Application\UseCase\Query\User\GetUsers;
6+
7+
use Acme\Infrastructure\Shared\Bus\Query\QueryHandlerInterface;
8+
use ApiPlatform\Core\Bridge\Doctrine\Orm\CollectionDataProvider;
9+
10+
class GetUsersQueryHandler implements QueryHandlerInterface
11+
{
12+
private CollectionDataProvider $provider;
13+
14+
public function __construct(CollectionDataProvider $provider)
15+
{
16+
$this->provider = $provider;
17+
}
18+
19+
public function __invoke(GetUsersQuery $data): iterable
20+
{
21+
return $this->provider->getCollection(
22+
$data->resourceClass(),
23+
$data->collectionOperationName(),
24+
$data->context()
25+
);
26+
}
27+
28+
}

src/Domain/User/User.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ public static function create(
4343
return $user;
4444
}
4545

46-
public function getUuid(): string
47-
{
48-
return $this->getAggregateRootId();
49-
}
50-
5146
public function signIn(string $plainPassword): void
5247
{
5348
if (!$this->credentials->password->match($plainPassword)) {

src/UI/Http/Rest/Controller/User/GetUserByEmailController.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ public function __invoke(Request $request, string $email)
2222
{
2323
$query = new FindByEmailQuery($email);
2424

25-
$user = $this->ask($query);
26-
27-
return UserView::create($user);
25+
return $this->ask($query);
2826
}
2927
}

0 commit comments

Comments
 (0)