mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 13:54:23 +00:00
Update Social Work Metadata importer based on change request.
This commit is contained in:
parent
94e84fbdf0
commit
226c3eedef
@ -49,96 +49,196 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
||||
$this->entityManager = $entityManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function import(iterable $dataset): bool
|
||||
{
|
||||
// Initialisation of the previous result row with the proper data structure.
|
||||
$result = [
|
||||
'socialIssues' => [
|
||||
'socialIssue' => null,
|
||||
'socialIssueChild' => null,
|
||||
],
|
||||
'socialActions' => [
|
||||
'socialAction' => null,
|
||||
'socialActionChild' => null,
|
||||
],
|
||||
'goal' => null,
|
||||
'result' => null,
|
||||
'eval' => null,
|
||||
];
|
||||
|
||||
foreach ($dataset as $row) {
|
||||
$this->import1(
|
||||
array_map(
|
||||
static fn (string $column): ?string => '' === $column ? null : $column,
|
||||
array_map('trim', $row)
|
||||
)
|
||||
);
|
||||
$result = $this
|
||||
->import1(
|
||||
// Columns cleanup before importing data.
|
||||
array_map(
|
||||
static fn (string $column): ?string => '' === $column ? null : $column,
|
||||
array_map('trim', $row)
|
||||
),
|
||||
$result
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function import1(array $row): void
|
||||
/**
|
||||
* Row Structure:
|
||||
*
|
||||
* Index 0: Parent SocialIssue
|
||||
* Index 1: Child SocialIssue
|
||||
* Index 2: Parent SocialAction
|
||||
* Index 3: Child SocialAction
|
||||
* Index 4: Goal
|
||||
* Index 5: Result
|
||||
* Index 6: Evaluation
|
||||
*
|
||||
* @param array<int, ?string> $row
|
||||
* @param array<string, array<string, SocialIssue|null>|array<string, SocialAction|null>|Goal|Result|Evaluation> $previousRow
|
||||
*
|
||||
* @return array<string, array<string, SocialIssue|null>|array<string, SocialAction|null>|Goal|Result|Evaluation>
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
private function import1(array $row, array $previousRow): array
|
||||
{
|
||||
// Structure:
|
||||
// Index 0: SocialIssue.parent
|
||||
// Index 1: SocialIssue
|
||||
// Index 2: SocialAction.parent
|
||||
// Index 3: SocialAction
|
||||
// Index 4: Goal
|
||||
// Index 5: Result
|
||||
// Index 6: Evaluation
|
||||
$socialIssues = $this
|
||||
->handleSocialIssue(
|
||||
$row[0],
|
||||
$row[1],
|
||||
$previousRow['socialIssues']['socialIssue'] ?? null,
|
||||
$previousRow['socialIssues']['socialIssueChild'] ?? null
|
||||
);
|
||||
|
||||
$socialIssue = $this->handleSocialIssue($row[0], $row[1]);
|
||||
$socialActions = $this
|
||||
->handleSocialAction(
|
||||
$row[2],
|
||||
$row[3],
|
||||
$socialIssues['socialIssue'],
|
||||
$previousRow['socialActions']['socialAction'] ?? null,
|
||||
$previousRow['socialActions']['socialActionChild'] ?? null
|
||||
);
|
||||
|
||||
$socialAction = $this->handleSocialAction($row[2], $row[3], $socialIssue);
|
||||
|
||||
$goal = $this->handleGoal($row[4], $socialAction);
|
||||
|
||||
$result = $this->handleResult($row[5], $socialAction, $goal);
|
||||
|
||||
$eval = $this->handleEvaluation($row[6], $socialAction);
|
||||
$goal = $this->handleGoal($row[4], $socialActions['socialAction']);
|
||||
$result = $this->handleResult($row[5], $socialActions['socialAction'], $goal);
|
||||
$eval = $this->handleEvaluation($row[6], $socialActions['socialAction']);
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
return [
|
||||
'socialIssues' => $socialIssues,
|
||||
'socialActions' => $socialActions,
|
||||
'goal' => $goal,
|
||||
'result' => $result,
|
||||
'eval' => $eval,
|
||||
];
|
||||
}
|
||||
|
||||
private function handleSocialIssue(?string $socialIssueTitle = null, ?string $socialIssueChildrenTitle = null): SocialIssue
|
||||
{
|
||||
if (null !== $socialIssueChildrenTitle) {
|
||||
/** @var SocialIssue $socialIssueChildren */
|
||||
$socialIssueChildren = $this->getOrCreateEntity($this->socialIssueRepository, 'title', ['fr' => $socialIssueChildrenTitle]);
|
||||
$socialIssueChildren->setTitle(['fr' => $socialIssueChildrenTitle]);
|
||||
/**
|
||||
* @return array<string, SocialIssue|null>
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
private function handleSocialIssue(
|
||||
?string $socialIssueTitle,
|
||||
?string $socialIssueChildTitle,
|
||||
?SocialIssue $previousSocialIssue,
|
||||
?SocialIssue $previousSocialIssueChild,
|
||||
): array {
|
||||
if (null !== $previousSocialIssue && ($socialIssueTitle === $previousSocialIssue->getTitle())) {
|
||||
$return = [
|
||||
'socialIssue' => $previousSocialIssue,
|
||||
];
|
||||
|
||||
$this->entityManager->persist($socialIssueChildren);
|
||||
return $return + [
|
||||
'socialIssueChild' => (null !== $previousSocialIssueChild && ($socialIssueChildTitle === $previousSocialIssueChild->getTitle())) ? $previousSocialIssueChild : null,
|
||||
];
|
||||
}
|
||||
|
||||
/** @var SocialIssue $socialIssue */
|
||||
$socialIssue = $this->getOrCreateEntity($this->socialIssueRepository, 'title', ['fr' => $socialIssueTitle]);
|
||||
$socialIssue->setTitle(['fr' => $socialIssueTitle]);
|
||||
$socialIssue = $this
|
||||
->getOrCreateEntity(
|
||||
$this->socialIssueRepository,
|
||||
'title',
|
||||
['fr' => $socialIssueTitle]
|
||||
)
|
||||
->setTitle(['fr' => $socialIssueTitle]);
|
||||
|
||||
if (null !== $socialIssueChildrenTitle) {
|
||||
$socialIssue->addChild($socialIssueChildren);
|
||||
if (null !== $socialIssueChildTitle) {
|
||||
/** @var SocialIssue $socialIssueChild */
|
||||
$socialIssueChild = $this
|
||||
->getOrCreateEntity(
|
||||
$this->socialIssueRepository,
|
||||
'title',
|
||||
['fr' => $socialIssueChildTitle]
|
||||
)
|
||||
->setTitle(['fr' => $socialIssueChildTitle]);
|
||||
|
||||
$this->entityManager->persist($socialIssueChild);
|
||||
|
||||
$socialIssue->addChild($socialIssueChild);
|
||||
}
|
||||
|
||||
$this->entityManager->persist($socialIssue);
|
||||
|
||||
return null === $socialIssueChildrenTitle ? $socialIssue : $socialIssueChildren;
|
||||
return [
|
||||
'socialIssue' => $socialIssue,
|
||||
'socialIssueChild' => $socialIssueChild ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
private function handleSocialAction(?string $socialActionTitle, ?string $socialActionChildrenTitle, SocialIssue $socialIssue): SocialAction
|
||||
{
|
||||
if (null !== $socialActionChildrenTitle) {
|
||||
/** @var SocialAction $socialActionChildren */
|
||||
$socialActionChildren = $this->getOrCreateEntity($this->socialActionRepository, 'title', ['fr' => $socialActionChildrenTitle]);
|
||||
$socialActionChildren->setTitle(['fr' => $socialActionChildrenTitle]);
|
||||
/**
|
||||
* @return array<string, SocialAction|null>
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
private function handleSocialAction(
|
||||
?string $socialActionTitle,
|
||||
?string $socialActionChildTitle,
|
||||
SocialIssue $socialIssue,
|
||||
?SocialAction $previousSocialAction,
|
||||
?SocialAction $previousSocialActionChild
|
||||
): array {
|
||||
if (null !== $previousSocialAction && ($socialActionTitle === $previousSocialAction->getTitle())) {
|
||||
$return = [
|
||||
'socialAction' => $previousSocialAction,
|
||||
];
|
||||
|
||||
$this->entityManager->persist($socialActionChildren);
|
||||
return $return + [
|
||||
'socialActionChild' => (null !== $previousSocialActionChild && ($socialActionChildTitle === $previousSocialActionChild->getTitle())) ? $previousSocialActionChild : null,
|
||||
];
|
||||
}
|
||||
|
||||
/** @var SocialIssue $socialIssue */
|
||||
$socialAction = $this->getOrCreateEntity($this->socialActionRepository, 'title', ['fr' => $socialActionTitle]);
|
||||
$socialAction->setTitle(['fr' => $socialActionTitle]);
|
||||
|
||||
if (null !== $socialActionChildrenTitle) {
|
||||
$socialActionChildren->setIssue($socialIssue);
|
||||
$this->entityManager->persist($socialActionChildren);
|
||||
if (null !== $socialActionChildTitle) {
|
||||
/** @var SocialAction $socialActionChild */
|
||||
$socialActionChild = $this->getOrCreateEntity($this->socialActionRepository, 'title', ['fr' => $socialActionChildTitle]);
|
||||
$socialActionChild->setTitle(['fr' => $socialActionChildTitle]);
|
||||
|
||||
$socialAction->addChild($socialActionChildren);
|
||||
$this->entityManager->persist($socialActionChild);
|
||||
|
||||
$socialActionChild->setIssue($socialIssue);
|
||||
$this->entityManager->persist($socialActionChild);
|
||||
|
||||
$socialAction->addChild($socialActionChild);
|
||||
} else {
|
||||
$socialAction->setIssue($socialIssue);
|
||||
}
|
||||
|
||||
$this->entityManager->persist($socialAction);
|
||||
|
||||
return null === $socialActionChildrenTitle ? $socialAction : $socialActionChildren;
|
||||
return [
|
||||
'socialAction' => $socialAction,
|
||||
'socialActionChild' => $socialActionChild ?? null
|
||||
];
|
||||
}
|
||||
|
||||
private function handleGoal(?string $goalTitle = null, ?SocialAction $socialAction = null): ?Goal
|
||||
private function handleGoal(?string $goalTitle, SocialAction $socialAction): ?Goal
|
||||
{
|
||||
if (null === $goalTitle) {
|
||||
return null;
|
||||
@ -148,19 +248,16 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
||||
$goal = $this->getOrCreateEntity($this->goalRepository, 'title', ['fr' => $goalTitle]);
|
||||
$goal->setTitle(['fr' => $goalTitle]);
|
||||
|
||||
if (null !== $socialAction) {
|
||||
$socialAction->addGoal($goal);
|
||||
$goal->addSocialAction($socialAction);
|
||||
|
||||
$this->entityManager->persist($socialAction);
|
||||
}
|
||||
$socialAction->addGoal($goal);
|
||||
$goal->addSocialAction($socialAction);
|
||||
|
||||
$this->entityManager->persist($socialAction);
|
||||
$this->entityManager->persist($goal);
|
||||
|
||||
return $goal;
|
||||
}
|
||||
|
||||
private function handleResult(?string $resultTitle = null, ?SocialAction $socialAction = null, ?Goal $goal = null): ?Result
|
||||
private function handleResult(?string $resultTitle, SocialAction $socialAction, ?Goal $goal): ?Result
|
||||
{
|
||||
if (null === $resultTitle) {
|
||||
return null;
|
||||
@ -175,8 +272,6 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
||||
$goal->addResult($result);
|
||||
|
||||
$this->entityManager->persist($goal);
|
||||
} else {
|
||||
$result->addSocialAction($socialAction);
|
||||
}
|
||||
|
||||
$result->addSocialAction($socialAction);
|
||||
@ -204,6 +299,9 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
||||
return $eval;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, object>
|
||||
*/
|
||||
private function findByJson(ObjectRepository $repository, string $field, array $jsonCriterias): array
|
||||
{
|
||||
$qb = $this
|
||||
@ -214,7 +312,7 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
||||
|
||||
$expr = $qb->expr();
|
||||
|
||||
$temporaryJsonCriterias = $jsonParameters = [];
|
||||
$temporaryJsonCriterias = [];
|
||||
|
||||
foreach ($jsonCriterias as $key => $value) {
|
||||
$temporaryJsonCriterias[] = [$field, $key, $value, sprintf(':placeholder_%s_%s', $field, $key)];
|
||||
@ -256,15 +354,19 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
||||
$temporaryJsonCriterias
|
||||
);
|
||||
|
||||
$query = $qb
|
||||
return $qb
|
||||
->select('s')
|
||||
->where(...$jsonPredicates)
|
||||
->setParameters($jsonParameters)
|
||||
->getQuery();
|
||||
|
||||
return $query->getResult();
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return object
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
private function getOrCreateEntity(ObjectRepository $repository, string $field, array $jsonCriterias = [])
|
||||
{
|
||||
$results = $this
|
||||
@ -274,24 +376,21 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
||||
$jsonCriterias
|
||||
);
|
||||
|
||||
switch (true) {
|
||||
case count($results) === 0:
|
||||
$entity = $repository->getClassName();
|
||||
$entity = new $entity();
|
||||
break;
|
||||
case count($results) === 1;
|
||||
$entity = current($results);
|
||||
break;
|
||||
case count($results) > 1;
|
||||
throw new Exception(
|
||||
sprintf(
|
||||
'More than one entity(%s) found.',
|
||||
$repository->getClassName()
|
||||
)
|
||||
);
|
||||
if ($results === []) {
|
||||
$entity = $repository->getClassName();
|
||||
|
||||
return new $entity;
|
||||
}
|
||||
|
||||
return $entity;
|
||||
if (count($results) === 1) {
|
||||
return reset($results);
|
||||
}
|
||||
|
||||
throw new Exception(
|
||||
sprintf(
|
||||
'Unable to create entity.'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user