|
| 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 | +} |
0 commit comments