Skip to content

Commit 90f88b1

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

24 files changed

Lines changed: 702 additions & 114 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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);
Lines changed: 57 additions & 0 deletions
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

Lines changed: 19 additions & 2 deletions
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

Lines changed: 14 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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+
}

0 commit comments

Comments
 (0)