|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Community\Infrastructure\ApiPlatform\State\Processor; |
| 6 | + |
| 7 | +use ApiPlatform\Metadata\Exception\ProblemExceptionInterface; |
| 8 | +use ApiPlatform\Metadata\Operation; |
| 9 | +use ApiPlatform\State\ProcessorInterface; |
| 10 | +use ApiPlatform\Validator\Exception\ValidationException; |
| 11 | +use App\Community\Domain\Model\Community; |
| 12 | +use App\Community\Domain\Repository\CommunityRepositoryInterface; |
| 13 | +use App\Community\Infrastructure\ApiPlatform\Input\CommunityFieldsInput; |
| 14 | +use App\Field\Application\FieldService; |
| 15 | +use App\Field\Domain\Enum\FieldCommunity; |
| 16 | +use App\Field\Domain\Exception\FieldWikidataIdMissingException; |
| 17 | +use App\Field\Domain\Model\Field; |
| 18 | +use App\Shared\Domain\Manager\TransactionManagerInterface; |
| 19 | +use Exception; |
| 20 | +use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
| 21 | +use Symfony\Component\Uid\UuidV7; |
| 22 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 23 | +use Webmozart\Assert\Assert; |
| 24 | + |
| 25 | +use function Zenstruck\Foundry\Persistence\delete; |
| 26 | + |
| 27 | +/** |
| 28 | + * @implements ProcessorInterface<CommunityFieldsInput, CommunityFieldsInput> |
| 29 | + */ |
| 30 | +final class UpsertCommunityProcessor implements ProcessorInterface |
| 31 | +{ |
| 32 | + public function __construct( |
| 33 | + private CommunityRepositoryInterface $communityRepo, |
| 34 | + private TransactionManagerInterface $transactionManager, |
| 35 | + private HttpClientInterface $httpClient, |
| 36 | + private UpdateCommunityProcessor $updateCommunityProcessor, |
| 37 | + private FieldService $fieldService, |
| 38 | + private DenormalizerInterface $denormalizer, |
| 39 | + ) { |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @param CommunityFieldsInput $data |
| 44 | + */ |
| 45 | + public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): array |
| 46 | + { |
| 47 | + return $this->transactionManager->transactional(function () use ($data) { |
| 48 | + Assert::isInstanceOf($data, CommunityFieldsInput::class); |
| 49 | + |
| 50 | + $wikidataIdFields = []; |
| 51 | + $result = []; |
| 52 | + |
| 53 | + $wikidataIds = array_map(function (array $fields) use(&$wikidataIdFields) { |
| 54 | + $wikidataField = $this->getFieldByName($fields, FieldCommunity::WIKIDATA_ID->value); |
| 55 | + if (!$wikidataField) throw new FieldWikidataIdMissingException(); |
| 56 | + |
| 57 | + $wikidataId = $wikidataField['value']; |
| 58 | + $wikidataIdFields[$wikidataId] = $fields; |
| 59 | + return $wikidataId; |
| 60 | + }, $data->wikidataEntities); |
| 61 | + |
| 62 | + // Update... |
| 63 | + $communities = $this->communityRepo->addSelectField()->withWikidataIds($wikidataIds)->asCollection(); |
| 64 | + foreach ($communities as $community) { |
| 65 | + $wikidataId = $community->getMostTrustableFieldByName(FieldCommunity::WIKIDATA_ID)->getValue(); |
| 66 | + $openChurchWikidataUpdatedAt = $community->getMostTrustableFieldByName(FieldCommunity::WIKIDATA_UPDATED_AT); |
| 67 | + $wikidataUpdatedAt = $this->getFieldByName($wikidataIdFields[$wikidataId], FieldCommunity::WIKIDATA_UPDATED_AT->value); |
| 68 | + |
| 69 | + if (!$openChurchWikidataUpdatedAt || $wikidataUpdatedAt['value'] !== $openChurchWikidataUpdatedAt->getValue()) { |
| 70 | + // WikidataUpdatedAt is not the same. We have to update the data |
| 71 | + try { |
| 72 | + $this->fieldService->upsertFields($community, $this->arrayToFields($wikidataIdFields[$wikidataId])); |
| 73 | + $result[$wikidataId] = 'Updated'; |
| 74 | + } |
| 75 | + catch (Exception $e) { |
| 76 | + $result[$wikidataId] = $this->handleError($e); |
| 77 | + } |
| 78 | + } |
| 79 | + unset($wikidataIdFields[$wikidataId]); |
| 80 | + } |
| 81 | + |
| 82 | + // Insert... |
| 83 | + foreach ($wikidataIdFields as $wikidataId => $fields) { |
| 84 | + try { |
| 85 | + $community = new Community(); |
| 86 | + $this->communityRepo->add($community); |
| 87 | + $this->fieldService->upsertFields($community, $this->arrayToFields($fields)); |
| 88 | + $result[$wikidataId] = 'Inserted'; |
| 89 | + } |
| 90 | + catch (Exception $e) { |
| 91 | + $result[$wikidataId] = $this->handleError($e); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return $result; |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + private function arrayToFields(array $fields) : array { |
| 100 | + return array_map( |
| 101 | + fn (array $field) => $this->denormalizer->denormalize($field, Field::class), |
| 102 | + $fields |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + private function getFieldByName(array $fields, string $fieldName) : mixed { |
| 107 | + $result = array_values(array_filter($fields, |
| 108 | + fn (array $field) => $field['name'] === $fieldName |
| 109 | + )); |
| 110 | + |
| 111 | + if (count($result) > 0) { |
| 112 | + return $result[0]; |
| 113 | + } |
| 114 | + |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + private function handleError(Exception $e) : string { |
| 119 | + $this->communityRepo->clear(); |
| 120 | + if ($e instanceof ValidationException) { |
| 121 | + return $e->getMessage(); |
| 122 | + } |
| 123 | + if ($e instanceof ProblemExceptionInterface) { |
| 124 | + return $e->getDetail(); |
| 125 | + } |
| 126 | + |
| 127 | + return $e->getMessage(); |
| 128 | + } |
| 129 | +} |
0 commit comments