Skip to content

Commit 90f88b1

Browse files
committed
add doctrine event listeners - wip
1 parent f0defe8 commit 90f88b1

File tree

24 files changed

+702
-114
lines changed

24 files changed

+702
-114
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1. Attention à la création des communautés / places !
2+
Les fields et leurs entités doivent être persistés ensemble avant de flush

src/Community/Domain/Repository/CommunityRepositoryInterface.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface CommunityRepositoryInterface extends RepositoryInterface
1616
public function ofId(Uuid $communityid): ?Community;
1717

1818
/**
19-
* @param array<string> $ids
19+
* @param array<Uuid> $ids
2020
*/
2121
public function ofIds(array $ids): static;
2222

@@ -27,4 +27,6 @@ public function add(Community $community): void;
2727
public function withType(string $type): static;
2828

2929
public function withWikidataId(int $wikidataId): static;
30+
31+
public function withParentCommunityId(Uuid $parentId): static;
3032
}

src/Community/Infrastructure/ApiPlatform/State/Processor/CreateCommunityProcessor.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
3636
$community = new Community();
3737
$this->communityRepo->add($community);
3838

39-
$community->fields = $this->fieldService->upsertFields($community, $data->fields);
39+
$this->fieldService->upsertFields($community, $data->fields);
4040

4141
return CommunityResource::fromModel(
4242
$community,

src/Community/Infrastructure/ApiPlatform/State/Processor/UpdateCommunityProcessor.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
3333
Assert::isInstanceOf($data, CommunityResource::class);
3434

3535
$community = $this->communityRepo->ofId($data->id); // community cannot be null because we passed through CommunityItemProvider
36-
$community->fields = $this->fieldService->upsertFields($community, $data->fields);
36+
$this->fieldService->upsertFields($community, $data->fields);
3737

3838
return CommunityResource::fromModel($community);
3939
});

src/Community/Infrastructure/ApiPlatform/State/Provider/CommunityCollectionProvider.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use App\Core\Domain\Search\Service\SearchServiceInterface;
1515
use App\Field\Domain\Enum\FieldCommunity;
1616
use App\Shared\Infrastructure\ApiPlatform\State\Paginator;
17+
use Symfony\Component\Uid\Uuid;
1718

1819
/**
1920
* @implements ProviderInterface<CommunityResource>
@@ -60,7 +61,7 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
6061
}
6162

6263
$models = $this->communityRepo
63-
->ofIds($entityIds ?? [])
64+
->ofIds(array_map(fn (string $entityId) => Uuid::fromString($entityId), $entityIds ?? []))
6465
->withType($type)
6566
->withWikidataId((int) $wikidataId)
6667
->withPagination($page, $itemsPerPage);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Community\Infrastructure\Doctrine;
6+
7+
use App\Community\Domain\Enum\CommunityType;
8+
use App\Community\Domain\Model\Community;
9+
use App\Core\Domain\Search\Helper\SearchHelperInterface;
10+
use App\Field\Domain\Enum\FieldCommunity;
11+
use App\Shared\Domain\Enum\SearchIndex;
12+
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
13+
use Doctrine\ORM\Events;
14+
15+
#[AsEntityListener(event: Events::postPersist, method: 'postPersist', entity: Community::class)]
16+
final class DoctrineCommunityListener
17+
{
18+
public function __construct(
19+
private readonly SearchHelperInterface $searchHelper,
20+
) {
21+
}
22+
23+
public function postPersist(Community $community): void
24+
{
25+
$type = $community->getMostTrustableFieldByName(FieldCommunity::TYPE)?->getValue();
26+
if ($type === CommunityType::PARISH->value) {
27+
// A new parish has been inserted
28+
$parishName = $community->getMostTrustableFieldByName(FieldCommunity::NAME)?->getValue();
29+
$dioceseName = null;
30+
/** @var Community|null $diocese */
31+
$diocese = $community->getMostTrustableFieldByName(FieldCommunity::PARENT_COMMUNITY_ID)?->getValue();
32+
if ($diocese) {
33+
$dioceseName = $diocese->getMostTrustableFieldByName(FieldCommunity::NAME)?->getValue();
34+
}
35+
$this->searchHelper->upsertElement(
36+
SearchIndex::PARISH,
37+
$community->id->toString(),
38+
[
39+
'parishName' => $parishName,
40+
'dioceseName' => $dioceseName,
41+
]
42+
);
43+
}
44+
45+
if ($type === CommunityType::DIOCESE->value) {
46+
// A new diocese has been inserted
47+
$dioceseName = $community->getMostTrustableFieldByName(FieldCommunity::NAME)?->getValue();
48+
$this->searchHelper->upsertElement(
49+
SearchIndex::DIOCESE,
50+
$community->id->toString(),
51+
[
52+
'dioceseName' => $dioceseName,
53+
]
54+
);
55+
}
56+
}
57+
}

src/Community/Infrastructure/Doctrine/DoctrineCommunityRepository.php

+19-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function addSelectField(): static
4141
}
4242

4343
/**
44-
* @param array<string> $ids
44+
* @param array<Uuid> $ids
4545
*/
4646
public function ofIds(array $ids): static
4747
{
@@ -52,7 +52,7 @@ public function ofIds(array $ids): static
5252
return
5353
$this->filter(static function (QueryBuilder $qb) use ($ids): void {
5454
$qb->andWhere('community.id IN (:ids)')
55-
->setParameter('ids', array_map(fn (string $id) => Uuid::fromString($id)->toBinary(), $ids));
55+
->setParameter('ids', array_map(fn (Uuid $id) => $id->toBinary(), $ids));
5656
});
5757
}
5858

@@ -89,4 +89,21 @@ public function withWikidataId(?int $value): static
8989
->setParameter('valueWikidata', $value);
9090
});
9191
}
92+
93+
public function withParentCommunityId(?Uuid $parentId): static
94+
{
95+
if (!$parentId) {
96+
return $this;
97+
}
98+
99+
return
100+
$this->filter(static function (QueryBuilder $qb) use ($parentId): void {
101+
$qb->andWhere("
102+
EXISTS (SELECT 1 FROM App\Field\Domain\Model\Field f_community_parent_id
103+
WHERE f_community_parent_id.community = community
104+
AND f_community_parent_id.name = 'parentCommunityId' AND IDENTITY(f_community_parent_id.communityVal) = :valueParentCommunity)
105+
")
106+
->setParameter('valueParentCommunity', $parentId->toBinary());
107+
});
108+
}
92109
}

src/Core/Domain/Search/Helper/SearchHelperInterface.php

+14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ public function bulkIndex(SearchIndex $index, array $ids, array $bodies): void;
1616

1717
public function createIndex(SearchIndex $index): mixed;
1818

19+
/**
20+
* @param array<mixed> $body
21+
*
22+
* @return array<mixed>
23+
*/
24+
public function upsertElement(SearchIndex $index, string $id, array $body): mixed;
25+
26+
public function existDocument(SearchIndex $index, string $id): bool;
27+
28+
/**
29+
* @return array<mixed>|null
30+
*/
31+
public function getDocument(SearchIndex $index, string $id): ?array;
32+
1933
/**
2034
* @return array<mixed>
2135
*/

src/Core/Domain/Search/Service/SearchServiceInterface.php

+6
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@
44

55
namespace App\Core\Domain\Search\Service;
66

7+
use App\Community\Domain\Model\Community;
8+
79
interface SearchServiceInterface
810
{
911
/**
1012
* @return string[]
1113
*/
1214
public function searchParishIds(string $text, int $limit, int $offset): array;
1315

16+
public function findParish(string $id): ?Community;
17+
1418
/**
1519
* @return string[]
1620
*/
1721
public function searchDioceseIds(string $text, int $limit, int $offset): array;
1822

23+
public function findDiocese(string $text): ?Community;
24+
1925
/**
2026
* @return string[]
2127
*/

src/Core/Infrastructure/Doctrine/FixDoctrineMigrationTableSchema.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ public function postGenerateSchema(GenerateSchemaEventArgs $args): void
4545

4646
$table->setPrimaryKey([$this->configuration->getVersionColumnName()]);
4747
}
48-
}
48+
}

src/Core/Infrastructure/ElasticSearch/Helper/OfficialElasticSearchHelper.php

+48
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,54 @@ public function createIndex(SearchIndex $index): Elasticsearch|Promise
162162
return $this->elasticsearchClient->indices()->create($params);
163163
}
164164

165+
public function existDocument(SearchIndex $index, string $id): bool
166+
{
167+
$params = [
168+
'index' => $index->value,
169+
'id' => $id,
170+
];
171+
172+
return $this->elasticsearchClient->exists($params)->asBool();
173+
}
174+
175+
public function getDocument(SearchIndex $index, string $id): ?array
176+
{
177+
$params = [
178+
'index' => $index->value,
179+
'id' => $id,
180+
];
181+
182+
if (!$this->existDocument($index, $id)) {
183+
return null;
184+
}
185+
186+
return $this->elasticsearchClient->get($params)->asArray();
187+
}
188+
189+
/**
190+
* @param array<mixed> $body
191+
*
192+
* @return array<mixed>
193+
*/
194+
public function upsertElement(SearchIndex $index, string $id, array $body): array
195+
{
196+
$params = [
197+
'index' => $index->value,
198+
'id' => $id,
199+
'body' => $body,
200+
];
201+
202+
if ($this->existDocument($index, $id)) {
203+
$params['body'] = [
204+
'doc' => $body,
205+
];
206+
207+
return $this->elasticsearchClient->update($params)->asArray();
208+
}
209+
210+
return $this->elasticsearchClient->index($params)->asArray();
211+
}
212+
165213
/**
166214
* @param array<string, mixed> $body
167215
*

src/Core/Infrastructure/ElasticSearch/Service/OfficialElasticSearchService.php

+24
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22

33
namespace App\Core\Infrastructure\ElasticSearch\Service;
44

5+
use App\Community\Domain\Model\Community;
6+
use App\Community\Domain\Repository\CommunityRepositoryInterface;
57
use App\Core\Domain\Search\Helper\SearchHelperInterface;
68
use App\Core\Domain\Search\Service\SearchServiceInterface;
79
use App\Shared\Domain\Enum\SearchIndex;
10+
use Symfony\Component\Uid\Uuid;
811

912
class OfficialElasticSearchService implements SearchServiceInterface
1013
{
1114
public function __construct(
1215
private SearchHelperInterface $elasticSearchHelper,
16+
private CommunityRepositoryInterface $communityRepo,
1317
) {
1418
}
1519

@@ -94,6 +98,26 @@ private function buildQueryForDioceses(string $text, int $limit, int $offset): a
9498
];
9599
}
96100

101+
public function findParish(string $id): ?Community
102+
{
103+
$document = $this->elasticSearchHelper->getDocument(SearchIndex::PARISH, $id);
104+
if ($document) {
105+
return $this->communityRepo->ofId(Uuid::fromString($document['id']));
106+
}
107+
108+
return null;
109+
}
110+
111+
public function findDiocese(string $id): ?Community
112+
{
113+
$document = $this->elasticSearchHelper->getDocument(SearchIndex::DIOCESE, $id);
114+
if ($document) {
115+
return $this->communityRepo->ofId(Uuid::fromString($document['id']));
116+
}
117+
118+
return null;
119+
}
120+
97121
/** @return string[] */
98122
public function searchDioceseIds(string $text, int $limit, int $offset): array
99123
{

src/Field/Application/FieldService.php

+2-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use App\Field\Domain\Repository\FieldRepositoryInterface;
1616
use App\Place\Domain\Model\Place;
1717
use App\Place\Domain\Repository\PlaceRepositoryInterface;
18-
use Doctrine\Common\Collections\ArrayCollection;
1918
use Doctrine\Common\Collections\Collection;
2019
use Symfony\Bundle\SecurityBundle\Security;
2120
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@@ -37,14 +36,9 @@ public function __construct(
3736

3837
/**
3938
* @param Field[] $fieldPayloads
40-
*
41-
* @return Collection<int, Field>
4239
*/
43-
public function upsertFields(Place|Community $entity, array $fieldPayloads): Collection
40+
public function upsertFields(Place|Community $entity, array $fieldPayloads): void
4441
{
45-
/** @var Collection<int, Field> $insertedFields */
46-
$insertedFields = new ArrayCollection();
47-
4842
/** @var Agent $agent */
4943
$agent = $this->security->getUser();
5044

@@ -93,12 +87,8 @@ public function upsertFields(Place|Community $entity, array $fieldPayloads): Col
9387
}
9488

9589
$field->applyValue(); // Dynamycally set the value to the correct property (intVal, stringVal, ...)
96-
97-
$this->fieldRepo->add($field);
98-
$insertedFields[] = $field;
90+
$entity->addField($field);
9991
}
100-
101-
return $insertedFields;
10292
}
10393

10494
private function getOrCreate(Place|Community $entity, FieldPlace|FieldCommunity $nameEnum, Agent $agent): Field

0 commit comments

Comments
 (0)