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;
|
$this->entityManager = $entityManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
public function import(iterable $dataset): bool
|
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) {
|
foreach ($dataset as $row) {
|
||||||
$this->import1(
|
$result = $this
|
||||||
array_map(
|
->import1(
|
||||||
static fn (string $column): ?string => '' === $column ? null : $column,
|
// Columns cleanup before importing data.
|
||||||
array_map('trim', $row)
|
array_map(
|
||||||
)
|
static fn (string $column): ?string => '' === $column ? null : $column,
|
||||||
);
|
array_map('trim', $row)
|
||||||
|
),
|
||||||
|
$result
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
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:
|
$socialIssues = $this
|
||||||
// Index 0: SocialIssue.parent
|
->handleSocialIssue(
|
||||||
// Index 1: SocialIssue
|
$row[0],
|
||||||
// Index 2: SocialAction.parent
|
$row[1],
|
||||||
// Index 3: SocialAction
|
$previousRow['socialIssues']['socialIssue'] ?? null,
|
||||||
// Index 4: Goal
|
$previousRow['socialIssues']['socialIssueChild'] ?? null
|
||||||
// Index 5: Result
|
);
|
||||||
// Index 6: Evaluation
|
|
||||||
|
|
||||||
$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], $socialActions['socialAction']);
|
||||||
|
$result = $this->handleResult($row[5], $socialActions['socialAction'], $goal);
|
||||||
$goal = $this->handleGoal($row[4], $socialAction);
|
$eval = $this->handleEvaluation($row[6], $socialActions['socialAction']);
|
||||||
|
|
||||||
$result = $this->handleResult($row[5], $socialAction, $goal);
|
|
||||||
|
|
||||||
$eval = $this->handleEvaluation($row[6], $socialAction);
|
|
||||||
|
|
||||||
$this->entityManager->flush();
|
$this->entityManager->flush();
|
||||||
|
|
||||||
|
return [
|
||||||
|
'socialIssues' => $socialIssues,
|
||||||
|
'socialActions' => $socialActions,
|
||||||
|
'goal' => $goal,
|
||||||
|
'result' => $result,
|
||||||
|
'eval' => $eval,
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
private function handleSocialIssue(?string $socialIssueTitle = null, ?string $socialIssueChildrenTitle = null): SocialIssue
|
/**
|
||||||
{
|
* @return array<string, SocialIssue|null>
|
||||||
if (null !== $socialIssueChildrenTitle) {
|
*
|
||||||
/** @var SocialIssue $socialIssueChildren */
|
* @throws Exception
|
||||||
$socialIssueChildren = $this->getOrCreateEntity($this->socialIssueRepository, 'title', ['fr' => $socialIssueChildrenTitle]);
|
*/
|
||||||
$socialIssueChildren->setTitle(['fr' => $socialIssueChildrenTitle]);
|
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 */
|
/** @var SocialIssue $socialIssue */
|
||||||
$socialIssue = $this->getOrCreateEntity($this->socialIssueRepository, 'title', ['fr' => $socialIssueTitle]);
|
$socialIssue = $this
|
||||||
$socialIssue->setTitle(['fr' => $socialIssueTitle]);
|
->getOrCreateEntity(
|
||||||
|
$this->socialIssueRepository,
|
||||||
|
'title',
|
||||||
|
['fr' => $socialIssueTitle]
|
||||||
|
)
|
||||||
|
->setTitle(['fr' => $socialIssueTitle]);
|
||||||
|
|
||||||
if (null !== $socialIssueChildrenTitle) {
|
if (null !== $socialIssueChildTitle) {
|
||||||
$socialIssue->addChild($socialIssueChildren);
|
/** @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);
|
$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
|
/**
|
||||||
{
|
* @return array<string, SocialAction|null>
|
||||||
if (null !== $socialActionChildrenTitle) {
|
*
|
||||||
/** @var SocialAction $socialActionChildren */
|
* @throws Exception
|
||||||
$socialActionChildren = $this->getOrCreateEntity($this->socialActionRepository, 'title', ['fr' => $socialActionChildrenTitle]);
|
*/
|
||||||
$socialActionChildren->setTitle(['fr' => $socialActionChildrenTitle]);
|
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 */
|
/** @var SocialIssue $socialIssue */
|
||||||
$socialAction = $this->getOrCreateEntity($this->socialActionRepository, 'title', ['fr' => $socialActionTitle]);
|
$socialAction = $this->getOrCreateEntity($this->socialActionRepository, 'title', ['fr' => $socialActionTitle]);
|
||||||
$socialAction->setTitle(['fr' => $socialActionTitle]);
|
$socialAction->setTitle(['fr' => $socialActionTitle]);
|
||||||
|
|
||||||
if (null !== $socialActionChildrenTitle) {
|
if (null !== $socialActionChildTitle) {
|
||||||
$socialActionChildren->setIssue($socialIssue);
|
/** @var SocialAction $socialActionChild */
|
||||||
$this->entityManager->persist($socialActionChildren);
|
$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 {
|
} else {
|
||||||
$socialAction->setIssue($socialIssue);
|
$socialAction->setIssue($socialIssue);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->entityManager->persist($socialAction);
|
$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) {
|
if (null === $goalTitle) {
|
||||||
return null;
|
return null;
|
||||||
@ -148,19 +248,16 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
|||||||
$goal = $this->getOrCreateEntity($this->goalRepository, 'title', ['fr' => $goalTitle]);
|
$goal = $this->getOrCreateEntity($this->goalRepository, 'title', ['fr' => $goalTitle]);
|
||||||
$goal->setTitle(['fr' => $goalTitle]);
|
$goal->setTitle(['fr' => $goalTitle]);
|
||||||
|
|
||||||
if (null !== $socialAction) {
|
$socialAction->addGoal($goal);
|
||||||
$socialAction->addGoal($goal);
|
$goal->addSocialAction($socialAction);
|
||||||
$goal->addSocialAction($socialAction);
|
|
||||||
|
|
||||||
$this->entityManager->persist($socialAction);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
$this->entityManager->persist($socialAction);
|
||||||
$this->entityManager->persist($goal);
|
$this->entityManager->persist($goal);
|
||||||
|
|
||||||
return $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) {
|
if (null === $resultTitle) {
|
||||||
return null;
|
return null;
|
||||||
@ -175,8 +272,6 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
|||||||
$goal->addResult($result);
|
$goal->addResult($result);
|
||||||
|
|
||||||
$this->entityManager->persist($goal);
|
$this->entityManager->persist($goal);
|
||||||
} else {
|
|
||||||
$result->addSocialAction($socialAction);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$result->addSocialAction($socialAction);
|
$result->addSocialAction($socialAction);
|
||||||
@ -204,6 +299,9 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
|||||||
return $eval;
|
return $eval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, object>
|
||||||
|
*/
|
||||||
private function findByJson(ObjectRepository $repository, string $field, array $jsonCriterias): array
|
private function findByJson(ObjectRepository $repository, string $field, array $jsonCriterias): array
|
||||||
{
|
{
|
||||||
$qb = $this
|
$qb = $this
|
||||||
@ -214,7 +312,7 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
|||||||
|
|
||||||
$expr = $qb->expr();
|
$expr = $qb->expr();
|
||||||
|
|
||||||
$temporaryJsonCriterias = $jsonParameters = [];
|
$temporaryJsonCriterias = [];
|
||||||
|
|
||||||
foreach ($jsonCriterias as $key => $value) {
|
foreach ($jsonCriterias as $key => $value) {
|
||||||
$temporaryJsonCriterias[] = [$field, $key, $value, sprintf(':placeholder_%s_%s', $field, $key)];
|
$temporaryJsonCriterias[] = [$field, $key, $value, sprintf(':placeholder_%s_%s', $field, $key)];
|
||||||
@ -256,15 +354,19 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
|||||||
$temporaryJsonCriterias
|
$temporaryJsonCriterias
|
||||||
);
|
);
|
||||||
|
|
||||||
$query = $qb
|
return $qb
|
||||||
->select('s')
|
->select('s')
|
||||||
->where(...$jsonPredicates)
|
->where(...$jsonPredicates)
|
||||||
->setParameters($jsonParameters)
|
->setParameters($jsonParameters)
|
||||||
->getQuery();
|
->getQuery()
|
||||||
|
->getResult();
|
||||||
return $query->getResult();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return object
|
||||||
|
*
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
private function getOrCreateEntity(ObjectRepository $repository, string $field, array $jsonCriterias = [])
|
private function getOrCreateEntity(ObjectRepository $repository, string $field, array $jsonCriterias = [])
|
||||||
{
|
{
|
||||||
$results = $this
|
$results = $this
|
||||||
@ -274,24 +376,21 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
|||||||
$jsonCriterias
|
$jsonCriterias
|
||||||
);
|
);
|
||||||
|
|
||||||
switch (true) {
|
if ($results === []) {
|
||||||
case count($results) === 0:
|
$entity = $repository->getClassName();
|
||||||
$entity = $repository->getClassName();
|
|
||||||
$entity = new $entity();
|
return 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()
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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