Skip to content

Reduce duplication in ImportExportService. #2965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 39 additions & 63 deletions webapp/src/Service/ImportExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -1139,75 +1139,51 @@ protected function importTeamData(array $teamData, ?string &$message, ?array &$s
// It is legitimate that a team has no affiliation. Do not add it then.
$teamAffiliation = null;
$teamCategory = null;
if (!empty($teamItem['team_affiliation']['shortname'])) {
// First look up if the affiliation already exists.
$teamAffiliation = $this->em->getRepository(TeamAffiliation::class)->findOneBy(['shortname' => $teamItem['team_affiliation']['shortname']]);
if (!$teamAffiliation) {
foreach ($createdAffiliations as $createdAffiliation) {
if ($createdAffiliation->getShortname() === $teamItem['team_affiliation']['shortname']) {
$teamAffiliation = $createdAffiliation;
break;
foreach (['shortname', 'externalid'] as $key) {
if (!empty($teamItem['team_affiliation'][$key])) {
// First look up if the affiliation already exists.
$teamAffiliation = $this->em->getRepository(TeamAffiliation::class)->findOneBy([$key => $teamItem['team_affiliation'][$key]]);
if (!$teamAffiliation) {
foreach ($createdAffiliations as $createdAffiliation) {
$value = $key === 'shortname' ? $createdAffiliation->getShortname() : $createdAffiliation->getExternalid();
if ($value === $teamItem['team_affiliation'][$key]) {
$teamAffiliation = $createdAffiliation;
break;
}
}
}
}
if (!$teamAffiliation) {
$teamAffiliation = new TeamAffiliation();
$propertyAccessor = PropertyAccess::createPropertyAccessor();
foreach ($teamItem['team_affiliation'] as $field => $value) {
$propertyAccessor->setValue($teamAffiliation, $field, $value);
}

$errors = $this->validator->validate($teamAffiliation);
if ($errors->count()) {
$messages = [];
/** @var ConstraintViolationInterface $error */
foreach ($errors as $error) {
$messages[] = sprintf(' • `%s`: %s', $error->getPropertyPath(), $error->getMessage());
if (!$teamAffiliation) {
$teamAffiliation = new TeamAffiliation();
if ($key === 'shortname') {
$propertyAccessor = PropertyAccess::createPropertyAccessor();
foreach ($teamItem['team_affiliation'] as $field => $value) {
$propertyAccessor->setValue($teamAffiliation, $field, $value);
}
} else {
$teamAffiliation
->setExternalid($teamItem['team_affiliation']['externalid'])
->setName($teamItem['team_affiliation']['externalid'] . ' - auto-create during import')
->setShortname($teamItem['team_affiliation']['externalid'] . ' - auto-create during import');
}

$message .= sprintf("Organization for team at index %d (%s) has errors:\n%s\n\n",
$index,
json_encode($teamItem),
implode("\n", $messages));
$anyErrors = true;
} else {
$createdAffiliations[] = $teamAffiliation;
}
}
} elseif (!empty($teamItem['team_affiliation']['externalid'])) {
$teamAffiliation = $this->em->getRepository(TeamAffiliation::class)->findOneBy(['externalid' => $teamItem['team_affiliation']['externalid']]);
if (!$teamAffiliation) {
foreach ($createdAffiliations as $createdAffiliation) {
if ($createdAffiliation->getExternalid() === $teamItem['team_affiliation']['externalid']) {
$teamAffiliation = $createdAffiliation;
break;
$errors = $this->validator->validate($teamAffiliation);
if ($errors->count()) {
$messages = [];
/** @var ConstraintViolationInterface $error */
foreach ($errors as $error) {
$messages[] = sprintf(' • `%s`: %s', $error->getPropertyPath(), $error->getMessage());
}

$message .= sprintf("Organization for team at index %d (%s) has errors:\n%s\n\n",
$index,
json_encode($teamItem),
implode("\n", $messages));
$anyErrors = true;
} else {
$createdAffiliations[] = $teamAffiliation;
}
}
}

if (!$teamAffiliation) {
$teamAffiliation = new TeamAffiliation();
$teamAffiliation
->setExternalid($teamItem['team_affiliation']['externalid'])
->setName($teamItem['team_affiliation']['externalid'] . ' - auto-create during import')
->setShortname($teamItem['team_affiliation']['externalid'] . ' - auto-create during import');

$errors = $this->validator->validate($teamAffiliation);
if ($errors->count()) {
$messages = [];
/** @var ConstraintViolationInterface $error */
foreach ($errors as $error) {
$messages[] = sprintf(' • `%s`: %s', $error->getPropertyPath(), $error->getMessage());
}

$message .= sprintf("Organization for team at index %d (%s) has errors:\n%s\n\n",
$index,
json_encode($teamItem),
implode("\n", $messages));
$anyErrors = true;
} else {
$createdAffiliations[] = $teamAffiliation;
}
break;
}
}
$teamItem['team']['affiliation'] = $teamAffiliation;
Expand Down
Loading