Skip to content

Commit 242691f

Browse files
committed
Reduce duplication in ImportExportService.
1 parent 441070d commit 242691f

File tree

1 file changed

+39
-63
lines changed

1 file changed

+39
-63
lines changed

webapp/src/Service/ImportExportService.php

Lines changed: 39 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,75 +1139,51 @@ protected function importTeamData(array $teamData, ?string &$message, ?array &$s
11391139
// It is legitimate that a team has no affiliation. Do not add it then.
11401140
$teamAffiliation = null;
11411141
$teamCategory = null;
1142-
if (!empty($teamItem['team_affiliation']['shortname'])) {
1143-
// First look up if the affiliation already exists.
1144-
$teamAffiliation = $this->em->getRepository(TeamAffiliation::class)->findOneBy(['shortname' => $teamItem['team_affiliation']['shortname']]);
1145-
if (!$teamAffiliation) {
1146-
foreach ($createdAffiliations as $createdAffiliation) {
1147-
if ($createdAffiliation->getShortname() === $teamItem['team_affiliation']['shortname']) {
1148-
$teamAffiliation = $createdAffiliation;
1149-
break;
1142+
foreach (['shortname', 'externalid'] as $key) {
1143+
if (!empty($teamItem['team_affiliation'][$key])) {
1144+
// First look up if the affiliation already exists.
1145+
$teamAffiliation = $this->em->getRepository(TeamAffiliation::class)->findOneBy([$key => $teamItem['team_affiliation'][$key]]);
1146+
if (!$teamAffiliation) {
1147+
foreach ($createdAffiliations as $createdAffiliation) {
1148+
$value = $key === 'shortname' ? $createdAffiliation->getShortname() : $createdAffiliation->getExternalid();
1149+
if ($value === $teamItem['team_affiliation'][$key]) {
1150+
$teamAffiliation = $createdAffiliation;
1151+
break;
1152+
}
11501153
}
11511154
}
1152-
}
1153-
if (!$teamAffiliation) {
1154-
$teamAffiliation = new TeamAffiliation();
1155-
$propertyAccessor = PropertyAccess::createPropertyAccessor();
1156-
foreach ($teamItem['team_affiliation'] as $field => $value) {
1157-
$propertyAccessor->setValue($teamAffiliation, $field, $value);
1158-
}
1159-
1160-
$errors = $this->validator->validate($teamAffiliation);
1161-
if ($errors->count()) {
1162-
$messages = [];
1163-
/** @var ConstraintViolationInterface $error */
1164-
foreach ($errors as $error) {
1165-
$messages[] = sprintf(' • `%s`: %s', $error->getPropertyPath(), $error->getMessage());
1155+
if (!$teamAffiliation) {
1156+
$teamAffiliation = new TeamAffiliation();
1157+
if ($key === 'shortname') {
1158+
$propertyAccessor = PropertyAccess::createPropertyAccessor();
1159+
foreach ($teamItem['team_affiliation'] as $field => $value) {
1160+
$propertyAccessor->setValue($teamAffiliation, $field, $value);
1161+
}
1162+
} else {
1163+
$teamAffiliation
1164+
->setExternalid($teamItem['team_affiliation']['externalid'])
1165+
->setName($teamItem['team_affiliation']['externalid'] . ' - auto-create during import')
1166+
->setShortname($teamItem['team_affiliation']['externalid'] . ' - auto-create during import');
11661167
}
11671168

1168-
$message .= sprintf("Organization for team at index %d (%s) has errors:\n%s\n\n",
1169-
$index,
1170-
json_encode($teamItem),
1171-
implode("\n", $messages));
1172-
$anyErrors = true;
1173-
} else {
1174-
$createdAffiliations[] = $teamAffiliation;
1175-
}
1176-
}
1177-
} elseif (!empty($teamItem['team_affiliation']['externalid'])) {
1178-
$teamAffiliation = $this->em->getRepository(TeamAffiliation::class)->findOneBy(['externalid' => $teamItem['team_affiliation']['externalid']]);
1179-
if (!$teamAffiliation) {
1180-
foreach ($createdAffiliations as $createdAffiliation) {
1181-
if ($createdAffiliation->getExternalid() === $teamItem['team_affiliation']['externalid']) {
1182-
$teamAffiliation = $createdAffiliation;
1183-
break;
1169+
$errors = $this->validator->validate($teamAffiliation);
1170+
if ($errors->count()) {
1171+
$messages = [];
1172+
/** @var ConstraintViolationInterface $error */
1173+
foreach ($errors as $error) {
1174+
$messages[] = sprintf(' • `%s`: %s', $error->getPropertyPath(), $error->getMessage());
1175+
}
1176+
1177+
$message .= sprintf("Organization for team at index %d (%s) has errors:\n%s\n\n",
1178+
$index,
1179+
json_encode($teamItem),
1180+
implode("\n", $messages));
1181+
$anyErrors = true;
1182+
} else {
1183+
$createdAffiliations[] = $teamAffiliation;
11841184
}
11851185
}
1186-
}
1187-
1188-
if (!$teamAffiliation) {
1189-
$teamAffiliation = new TeamAffiliation();
1190-
$teamAffiliation
1191-
->setExternalid($teamItem['team_affiliation']['externalid'])
1192-
->setName($teamItem['team_affiliation']['externalid'] . ' - auto-create during import')
1193-
->setShortname($teamItem['team_affiliation']['externalid'] . ' - auto-create during import');
1194-
1195-
$errors = $this->validator->validate($teamAffiliation);
1196-
if ($errors->count()) {
1197-
$messages = [];
1198-
/** @var ConstraintViolationInterface $error */
1199-
foreach ($errors as $error) {
1200-
$messages[] = sprintf(' • `%s`: %s', $error->getPropertyPath(), $error->getMessage());
1201-
}
1202-
1203-
$message .= sprintf("Organization for team at index %d (%s) has errors:\n%s\n\n",
1204-
$index,
1205-
json_encode($teamItem),
1206-
implode("\n", $messages));
1207-
$anyErrors = true;
1208-
} else {
1209-
$createdAffiliations[] = $teamAffiliation;
1210-
}
1186+
break;
12111187
}
12121188
}
12131189
$teamItem['team']['affiliation'] = $teamAffiliation;

0 commit comments

Comments
 (0)