Skip to content

Commit 8625939

Browse files
committed
update synchro script
1 parent b62768b commit 8625939

40 files changed

+685
-875
lines changed

scripts/synchro2.py scripts/synchro-old.py

+303-147
Large diffs are not rendered by default.

scripts/synchro.py

+200-549
Large diffs are not rendered by default.

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

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

5-
use App\FieldHolder\Community\Domain\Model\Community;
6-
use App\FieldHolder\Community\Domain\Repository\CommunityRepositoryInterface;
75
use App\Core\Domain\Search\Helper\SearchHelperInterface;
86
use App\Core\Domain\Search\Service\SearchServiceInterface;
7+
use App\FieldHolder\Community\Domain\Model\Community;
8+
use App\FieldHolder\Community\Domain\Repository\CommunityRepositoryInterface;
99
use App\Shared\Domain\Enum\SearchIndex;
1010
use Symfony\Component\Uid\Uuid;
1111

src/Core/Infrastructure/Symfony/Command/WriteIndexesCommand.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
namespace App\Core\Infrastructure\Symfony\Command;
66

7+
use App\Core\Domain\Search\Helper\SearchHelperInterface;
8+
use App\Field\Domain\Enum\FieldCommunity;
79
use App\FieldHolder\Community\Domain\Enum\CommunityType;
810
use App\FieldHolder\Community\Domain\Model\Community;
911
use App\FieldHolder\Community\Domain\Repository\CommunityRepositoryInterface;
10-
use App\Core\Domain\Search\Helper\SearchHelperInterface;
11-
use App\Field\Domain\Enum\FieldCommunity;
1212
use App\Shared\Domain\Enum\SearchIndex;
1313
use Doctrine\Common\Collections\Collection;
1414
use Symfony\Component\Console\Attribute\AsCommand;

src/Field/Application/FieldService.php

+13-11
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
use ApiPlatform\Validator\Exception\ValidationException;
66
use App\Agent\Domain\Model\Agent;
7-
use App\FieldHolder\Community\Domain\Model\Community;
8-
use App\FieldHolder\Community\Domain\Repository\CommunityRepositoryInterface;
97
use App\Field\Domain\Enum\FieldCommunity;
108
use App\Field\Domain\Enum\FieldPlace;
119
use App\Field\Domain\Exception\FieldEntityNotFoundException;
1210
use App\Field\Domain\Exception\FieldInvalidNameException;
1311
use App\Field\Domain\Exception\FieldUnicityViolationException;
1412
use App\Field\Domain\Model\Field;
1513
use App\Field\Domain\Repository\FieldRepositoryInterface;
14+
use App\FieldHolder\Community\Domain\Model\Community;
15+
use App\FieldHolder\Community\Domain\Repository\CommunityRepositoryInterface;
1616
use App\FieldHolder\Place\Domain\Model\Place;
1717
use App\FieldHolder\Place\Domain\Repository\PlaceRepositoryInterface;
1818
use Symfony\Bundle\SecurityBundle\Security;
@@ -39,7 +39,6 @@ public function __construct(
3939
*/
4040
public function upsertFields(Place|Community $entity, array $fieldPayloads): void
4141
{
42-
4342
/** @var Agent $agent */
4443
$agent = $this->security->getUser();
4544

@@ -49,6 +48,11 @@ public function upsertFields(Place|Community $entity, array $fieldPayloads): voi
4948
Community::class => FieldCommunity::tryFrom($fieldPayload->name),
5049
default => null,
5150
};
51+
$aliases = match ($entity::class) {
52+
Place::class => FieldPlace::ALIASES,
53+
Community::class => FieldCommunity::ALIASES,
54+
default => null,
55+
};
5256
if (null === $enumValue) {
5357
throw new FieldInvalidNameException($fieldPayload->name);
5458
}
@@ -61,21 +65,21 @@ public function upsertFields(Place|Community $entity, array $fieldPayloads): voi
6165
$value = $this->maybeTransformEntities($enumValue, $fieldPayload->value);
6266
if (Community::class === $entity::class) {
6367
$field->community = $entity;
64-
if (FieldCommunity::PARENT_WIKIDATA_ID === $enumValue) {
65-
$fieldPayload->name = FieldCommunity::PARENT_COMMUNITY_ID->value;
66-
}
6768
} else {
6869
$field->place = $entity;
6970
}
7071

72+
if (in_array($enumValue->name, array_keys($aliases))) {
73+
$fieldPayload->name = $aliases[$enumValue->name]->value;
74+
}
7175
$field->name = $fieldPayload->name;
7276
$field->value = $value;
7377
$field->engine = $fieldPayload->engine;
7478
$field->reliability = $fieldPayload->reliability;
7579
$field->source = $fieldPayload->source;
7680
$field->explanation = $fieldPayload->explanation;
7781
$field->touch();
78-
82+
7983
// Unique constraints validation (TODO use custom Assert instead)
8084
if (null !== $field->value
8185
&& in_array($field->name, Field::UNIQUE_CONSTRAINTS, true)
@@ -148,8 +152,7 @@ private function maybeTransformEntities(FieldCommunity|FieldPlace $nameEnum, mix
148152

149153
if ($nameEnum->name === FieldPlace::PARENT_WIKIDATA_IDS->name) {
150154
$instances = $repo->withWikidataIds($value)->asCollection();
151-
}
152-
else {
155+
} else {
153156
$instances = $repo->ofIds(array_map(fn (string $id) => UuidV7::fromString($id), $value))->asCollection();
154157
}
155158

@@ -163,8 +166,7 @@ private function maybeTransformEntities(FieldCommunity|FieldPlace $nameEnum, mix
163166
if ($nameEnum->name === FieldCommunity::PARENT_WIKIDATA_ID->name) {
164167
assert(is_int($value));
165168
$instance = $repo->withWikidataId($value)->asCollection()[0];
166-
}
167-
else {
169+
} else {
168170
// That's an object
169171
assert(is_string($value));
170172
$instance = $repo->ofId(Uuid::fromString($value));

src/Field/Domain/Enum/FieldCommunity.php

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ enum FieldCommunity: string
2929
self::REPLACES->value => 'Community[]',
3030
];
3131

32+
public const ALIASES = [
33+
self::PARENT_WIKIDATA_ID->name => self::PARENT_COMMUNITY_ID,
34+
];
35+
3236
case NAME = 'name';
3337
case TYPE = 'type';
3438
case STATE = 'state';

src/Field/Domain/Enum/FieldPlace.php

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ enum FieldPlace: string
3030
self::WIKIDATA_UPDATED_AT->value => Types::DATETIME_IMMUTABLE,
3131
];
3232

33+
public const ALIASES = [
34+
self::PARENT_WIKIDATA_IDS->name => self::PARENT_COMMUNITIES,
35+
];
36+
3337
case NAME = 'name';
3438
case TYPE = 'type';
3539
case WEBSITE = 'website';

src/Field/Domain/Model/Field.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
namespace App\Field\Domain\Model;
44

55
use App\Agent\Domain\Model\Agent;
6-
use App\FieldHolder\Community\Domain\Model\Community;
76
use App\Field\Domain\Enum\FieldCommunity;
87
use App\Field\Domain\Enum\FieldEngine;
98
use App\Field\Domain\Enum\FieldPlace;
109
use App\Field\Domain\Enum\FieldReliability;
10+
use App\FieldHolder\Community\Domain\Model\Community;
1111
use App\FieldHolder\Place\Domain\Model\Place;
1212
use App\Shared\Infrastructure\Doctrine\Trait\DoctrineTimestampableTrait;
13-
use DateTimeImmutable;
1413
use Doctrine\Common\Collections\ArrayCollection;
1514
use Doctrine\Common\Collections\Collection;
1615
use Doctrine\DBAL\Types\Types;
@@ -262,10 +261,10 @@ public function applyValue(): void
262261
}
263262

264263
if ('datetimeVal' === $propertyName) {
265-
$value = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $value);
264+
$value = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $value);
266265
}
267266
if ('dateVal' === $propertyName) {
268-
$value = DateTimeImmutable::createFromFormat('Y-m-d', $value);
267+
$value = \DateTimeImmutable::createFromFormat('Y-m-d', $value);
269268
}
270269

271270
$propertyAccessor = new PropertyAccessor();

src/Field/Infrastructure/Doctrine/DoctrineFieldListener.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
namespace App\Field\Infrastructure\Doctrine;
66

77
use App\Agent\Domain\Model\Agent;
8-
use App\FieldHolder\Community\Domain\Enum\CommunityType;
9-
use App\FieldHolder\Community\Domain\Repository\CommunityRepositoryInterface;
108
use App\Core\Domain\Search\Helper\SearchHelperInterface;
119
use App\Field\Domain\Enum\FieldCommunity;
1210
use App\Field\Domain\Model\Field;
11+
use App\FieldHolder\Community\Domain\Enum\CommunityType;
12+
use App\FieldHolder\Community\Domain\Repository\CommunityRepositoryInterface;
1313
use App\Shared\Domain\Enum\SearchIndex;
1414
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
1515
use Doctrine\ORM\Events;

src/FieldHolder/Application/FieldHolderUpsertService.php

+8-19
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,27 @@
55
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
66
use ApiPlatform\Validator\Exception\ValidationException;
77
use App\Field\Domain\Model\Field;
8-
use Exception;
9-
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
108

119
final class FieldHolderUpsertService
1210
{
13-
public function __construct(
14-
private DenormalizerInterface $denormalizer,
15-
) {
16-
}
17-
18-
public function getFieldByName(array $fields, string $fieldName): mixed
11+
/**
12+
* @param Field[] $fields
13+
*/
14+
public function getFieldByName(array $fields, string $fieldName): ?Field
1915
{
20-
$result = array_values(array_filter($fields,
21-
fn (array $field) => $field['name'] === $fieldName
16+
$result = array_values(array_filter($fields,
17+
fn (Field $field) => $field->name === $fieldName
2218
));
2319

2420
return count($result) > 0 ? $result[0] : null;
2521
}
2622

27-
public function handleError(object $entity, Exception $e, callable $detachCallback): string
23+
public function handleError(object $entity, \Exception $e, callable $detachCallback): string
2824
{
2925
foreach ($entity->fields as $field) {
3026
call_user_func($detachCallback, $field);
31-
call_user_func($detachCallback, $entity);
3227
}
28+
call_user_func($detachCallback, $entity);
3329

3430
if ($e instanceof ValidationException) {
3531
return $e->getMessage();
@@ -40,11 +36,4 @@ public function handleError(object $entity, Exception $e, callable $detachCallba
4036

4137
return $e->getMessage();
4238
}
43-
44-
public function arrayToFields(array $fields) : array {
45-
return array_map(
46-
fn (array $field) => $this->denormalizer->denormalize($field, Field::class),
47-
$fields
48-
);
49-
}
5039
}

src/FieldHolder/Community/Domain/Model/Community.php

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ class Community extends FieldHolder
2626
#[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')]
2727
public ?Uuid $id = null;
2828

29+
/**
30+
* @var Collection<int, Field>
31+
*/
32+
#[ORM\OneToMany(targetEntity: Field::class, mappedBy: 'community')]
33+
public Collection $fields;
34+
2935
/**
3036
* @var Collection<int, Field>
3137
*/

src/FieldHolder/Community/Domain/Repository/CommunityRepositoryInterface.php

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public function withType(string $type): static;
2828

2929
public function withWikidataId(int $wikidataId): static;
3030

31+
/**
32+
* @param array<int> $wikidataIds
33+
*/
3134
public function withWikidataIds(array $wikidataIds): static;
3235

3336
public function withParentCommunityId(Uuid $parentId): static;

src/FieldHolder/Community/Infrastructure/ApiPlatform/Input/CommunityWikidataInput.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
namespace App\FieldHolder\Community\Infrastructure\ApiPlatform\Input;
66

7-
final class CommunityWikidataInput {
7+
use App\Field\Domain\Model\Field;
88

9+
final class CommunityWikidataInput
10+
{
911
/**
10-
* @param array<Field[]> $fields
12+
* @var array<Field[]>
1113
*/
1214
public array $wikidataEntities = [];
13-
}
15+
}

src/FieldHolder/Community/Infrastructure/ApiPlatform/Resource/CommunityResource.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
use ApiPlatform\Metadata\Patch;
1212
use ApiPlatform\Metadata\Post;
1313
use ApiPlatform\Metadata\Put;
14+
use App\Field\Domain\Model\Field;
15+
use App\Field\Infrastructure\ApiPlatform\Filter\FieldNameFilter;
16+
use App\Field\Infrastructure\ApiPlatform\Filter\FieldTypeFilter;
17+
use App\Field\Infrastructure\ApiPlatform\Filter\FieldWikidataIdFilter;
1418
use App\FieldHolder\Community\Domain\Model\Community;
1519
use App\FieldHolder\Community\Infrastructure\ApiPlatform\Input\CommunityWikidataInput;
1620
use App\FieldHolder\Community\Infrastructure\ApiPlatform\State\Processor\CreateCommunityProcessor;
1721
use App\FieldHolder\Community\Infrastructure\ApiPlatform\State\Processor\UpdateCommunityProcessor;
1822
use App\FieldHolder\Community\Infrastructure\ApiPlatform\State\Processor\UpsertCommunityProcessor;
1923
use App\FieldHolder\Community\Infrastructure\ApiPlatform\State\Provider\CommunityCollectionProvider;
2024
use App\FieldHolder\Community\Infrastructure\ApiPlatform\State\Provider\CommunityItemProvider;
21-
use App\Field\Domain\Model\Field;
22-
use App\Field\Infrastructure\ApiPlatform\Filter\FieldNameFilter;
23-
use App\Field\Infrastructure\ApiPlatform\Filter\FieldTypeFilter;
24-
use App\Field\Infrastructure\ApiPlatform\Filter\FieldWikidataIdFilter;
2525
use Symfony\Component\Serializer\Annotation\Groups;
2626
use Symfony\Component\Uid\Uuid;
2727

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
use ApiPlatform\Metadata\Operation;
88
use ApiPlatform\State\ProcessorInterface;
9+
use App\Field\Application\FieldService;
910
use App\FieldHolder\Community\Domain\Model\Community;
1011
use App\FieldHolder\Community\Domain\Repository\CommunityRepositoryInterface;
1112
use App\FieldHolder\Community\Infrastructure\ApiPlatform\Resource\CommunityResource;
12-
use App\Field\Application\FieldService;
1313
use App\Shared\Domain\Manager\TransactionManagerInterface;
1414
use Webmozart\Assert\Assert;
1515

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
use ApiPlatform\Metadata\Operation;
88
use ApiPlatform\State\ProcessorInterface;
9+
use App\Field\Application\FieldService;
910
use App\FieldHolder\Community\Domain\Repository\CommunityRepositoryInterface;
1011
use App\FieldHolder\Community\Infrastructure\ApiPlatform\Resource\CommunityResource;
11-
use App\Field\Application\FieldService;
1212
use App\Shared\Domain\Manager\TransactionManagerInterface;
1313
use Webmozart\Assert\Assert;
1414

0 commit comments

Comments
 (0)