From 226c3eedef8c402576ad237990302944f6771ebb Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Tue, 2 Nov 2021 13:27:51 +0100 Subject: [PATCH 1/3] Update Social Work Metadata importer based on change request. --- .../Service/Import/SocialWorkMetadata.php | 255 ++++++++++++------ 1 file changed, 177 insertions(+), 78 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Service/Import/SocialWorkMetadata.php b/src/Bundle/ChillPersonBundle/Service/Import/SocialWorkMetadata.php index 4683344dd..28bb212ea 100644 --- a/src/Bundle/ChillPersonBundle/Service/Import/SocialWorkMetadata.php +++ b/src/Bundle/ChillPersonBundle/Service/Import/SocialWorkMetadata.php @@ -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 $row + * @param array|array|Goal|Result|Evaluation> $previousRow + * + * @return array|array|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 + * + * @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 + * + * @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 + */ 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.' + ) + ); } From 90b256daafde518449a9cd5d70f3c73a3ee576bd Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Wed, 3 Nov 2021 10:14:10 +0100 Subject: [PATCH 2/3] fix: Remove extra comma. --- .../ChillPersonBundle/Service/Import/SocialWorkMetadata.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bundle/ChillPersonBundle/Service/Import/SocialWorkMetadata.php b/src/Bundle/ChillPersonBundle/Service/Import/SocialWorkMetadata.php index 28bb212ea..f7fb59742 100644 --- a/src/Bundle/ChillPersonBundle/Service/Import/SocialWorkMetadata.php +++ b/src/Bundle/ChillPersonBundle/Service/Import/SocialWorkMetadata.php @@ -145,7 +145,7 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface ?string $socialIssueTitle, ?string $socialIssueChildTitle, ?SocialIssue $previousSocialIssue, - ?SocialIssue $previousSocialIssueChild, + ?SocialIssue $previousSocialIssueChild ): array { if (null !== $previousSocialIssue && ($socialIssueTitle === $previousSocialIssue->getTitle())) { $return = [ From d01eaa8065a28d1ca73d40e519575105b7fb424c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 13 Dec 2021 13:48:20 +0100 Subject: [PATCH 3/3] various fixes --- .../Entity/SocialWork/SocialAction.php | 9 +- .../Entity/SocialWork/SocialIssue.php | 3 + .../Service/Import/SocialWorkMetadata.php | 461 +++++++++--------- 3 files changed, 241 insertions(+), 232 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php index c5ca7d18a..64e4e79d8 100644 --- a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php +++ b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php @@ -98,7 +98,7 @@ class SocialAction { if (!$this->children->contains($child)) { $this->children[] = $child; - $child->setParent($this); + $child->setParent($this)->setIssue($this->getIssue()); } return $this; @@ -266,9 +266,16 @@ class SocialAction { $this->issue = $issue; + foreach ($this->getChildren() as $child) { + $child->setIssue($issue); + } + return $this; } + /** + * @internal use $parent->addChild() instead (@see{self::addChild()}) + */ public function setParent(?self $parent): self { $this->parent = $parent; diff --git a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php index 80b106b4e..b0be1ae00 100644 --- a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php +++ b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php @@ -305,6 +305,9 @@ class SocialIssue return $this; } + /** + * @internal use @see{SocialIssue::addChild()} instead + */ public function setParent(?self $parent): self { $this->parent = $parent; diff --git a/src/Bundle/ChillPersonBundle/Service/Import/SocialWorkMetadata.php b/src/Bundle/ChillPersonBundle/Service/Import/SocialWorkMetadata.php index f7fb59742..101695463 100644 --- a/src/Bundle/ChillPersonBundle/Service/Import/SocialWorkMetadata.php +++ b/src/Bundle/ChillPersonBundle/Service/Import/SocialWorkMetadata.php @@ -1,5 +1,12 @@ null, ]; - foreach ($dataset as $row) { + foreach ($dataset as $key => $row) { $result = $this ->import1( + $key, // Columns cleanup before importing data. array_map( static fn (string $column): ?string => '' === $column ? null : $column, @@ -84,221 +93,6 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface return true; } - /** - * 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 $row - * @param array|array|Goal|Result|Evaluation> $previousRow - * - * @return array|array|Goal|Result|Evaluation> - * - * @throws Exception - */ - private function import1(array $row, array $previousRow): array - { - $socialIssues = $this - ->handleSocialIssue( - $row[0], - $row[1], - $previousRow['socialIssues']['socialIssue'] ?? null, - $previousRow['socialIssues']['socialIssueChild'] ?? null - ); - - $socialActions = $this - ->handleSocialAction( - $row[2], - $row[3], - $socialIssues['socialIssue'], - $previousRow['socialActions']['socialAction'] ?? null, - $previousRow['socialActions']['socialActionChild'] ?? null - ); - - $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, - ]; - } - - /** - * @return array - * - * @throws Exception - */ - private function handleSocialIssue( - ?string $socialIssueTitle, - ?string $socialIssueChildTitle, - ?SocialIssue $previousSocialIssue, - ?SocialIssue $previousSocialIssueChild - ): array { - if (null !== $previousSocialIssue && ($socialIssueTitle === $previousSocialIssue->getTitle())) { - $return = [ - 'socialIssue' => $previousSocialIssue, - ]; - - return $return + [ - 'socialIssueChild' => (null !== $previousSocialIssueChild && ($socialIssueChildTitle === $previousSocialIssueChild->getTitle())) ? $previousSocialIssueChild : null, - ]; - } - - /** @var SocialIssue $socialIssue */ - $socialIssue = $this - ->getOrCreateEntity( - $this->socialIssueRepository, - 'title', - ['fr' => $socialIssueTitle] - ) - ->setTitle(['fr' => $socialIssueTitle]); - - 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 [ - 'socialIssue' => $socialIssue, - 'socialIssueChild' => $socialIssueChild ?? null, - ]; - } - - /** - * @return array - * - * @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, - ]; - - 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 !== $socialActionChildTitle) { - /** @var SocialAction $socialActionChild */ - $socialActionChild = $this->getOrCreateEntity($this->socialActionRepository, 'title', ['fr' => $socialActionChildTitle]); - $socialActionChild->setTitle(['fr' => $socialActionChildTitle]); - - $this->entityManager->persist($socialActionChild); - - $socialActionChild->setIssue($socialIssue); - $this->entityManager->persist($socialActionChild); - - $socialAction->addChild($socialActionChild); - } else { - $socialAction->setIssue($socialIssue); - } - - $this->entityManager->persist($socialAction); - - return [ - 'socialAction' => $socialAction, - 'socialActionChild' => $socialActionChild ?? null - ]; - } - - private function handleGoal(?string $goalTitle, SocialAction $socialAction): ?Goal - { - if (null === $goalTitle) { - return null; - } - - /** @var Goal $goal */ - $goal = $this->getOrCreateEntity($this->goalRepository, 'title', ['fr' => $goalTitle]); - $goal->setTitle(['fr' => $goalTitle]); - - $socialAction->addGoal($goal); - $goal->addSocialAction($socialAction); - - $this->entityManager->persist($socialAction); - $this->entityManager->persist($goal); - - return $goal; - } - - private function handleResult(?string $resultTitle, SocialAction $socialAction, ?Goal $goal): ?Result - { - if (null === $resultTitle) { - return null; - } - - /** @var Result $result */ - $result = $this->getOrCreateEntity($this->resultRepository, 'title', ['fr' => $resultTitle]); - $result->setTitle(['fr' => $resultTitle]); - - if (null !== $goal) { - $result->addGoal($goal); - $goal->addResult($result); - - $this->entityManager->persist($goal); - } - - $result->addSocialAction($socialAction); - $socialAction->addResult($result); - - $this->entityManager->persist($result); - $this->entityManager->persist($socialAction); - - return $result; - } - - private function handleEvaluation(?string $evaluationTitle, SocialAction $socialAction): ?Evaluation - { - if (null === $evaluationTitle) { - return null; - } - - /** @var Evaluation $eval */ - $eval = $this->getOrCreateEntity($this->evaluationRepository, 'title', ['fr' => $evaluationTitle]); - $eval->setTitle(['fr' => $evaluationTitle]); - $eval->setSocialAction($socialAction); - - $this->entityManager->persist($eval); - - return $eval; - } - /** * @return array */ @@ -320,8 +114,7 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface $jsonParameters = array_reduce( $temporaryJsonCriterias, - static function (array $carry, array $row): array - { + static function (array $carry, array $row): array { [,, $value, $placeholder] = $row; return array_merge( @@ -335,8 +128,7 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface ); $jsonPredicates = array_map( - static function (array $row) use ($expr): Comparison - { + static function (array $row) use ($expr): Comparison { [$field, $key,, $placeholder] = $row; $left = sprintf( @@ -363,9 +155,9 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface } /** - * @return object - * * @throws Exception + * + * @return object */ private function getOrCreateEntity(ObjectRepository $repository, string $field, array $jsonCriterias = []) { @@ -376,10 +168,10 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface $jsonCriterias ); - if ($results === []) { + if ([] === $results) { $entity = $repository->getClassName(); - return new $entity; + return new $entity(); } if (count($results) === 1) { @@ -387,11 +179,218 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface } throw new Exception( - sprintf( - 'Unable to create entity.' - ) + 'Unable to create entity.' ); } + private function handleEvaluation(?string $evaluationTitle, SocialAction $socialAction): ?Evaluation + { + if (null === $evaluationTitle) { + return null; + } + /** @var Evaluation $eval */ + $eval = $this->getOrCreateEntity($this->evaluationRepository, 'title', ['fr' => $evaluationTitle]); + $eval->setTitle(['fr' => $evaluationTitle]); + $eval->setSocialAction($socialAction); + + $this->entityManager->persist($eval); + + return $eval; + } + + private function handleGoal(?string $goalTitle, SocialAction $socialAction): ?Goal + { + if (null === $goalTitle) { + return null; + } + + /** @var Goal $goal */ + $goal = $this->getOrCreateEntity($this->goalRepository, 'title', ['fr' => $goalTitle]); + $goal->setTitle(['fr' => $goalTitle]); + + $socialAction->addGoal($goal); + $goal->addSocialAction($socialAction); + + //$this->entityManager->persist($socialAction); + $this->entityManager->persist($goal); + + return $goal; + } + + private function handleResult(?string $resultTitle, SocialAction $socialAction, ?Goal $goal): ?Result + { + if (null === $resultTitle) { + return null; + } + + /** @var Result $result */ + $result = $this->getOrCreateEntity($this->resultRepository, 'title', ['fr' => $resultTitle]); + $result->setTitle(['fr' => $resultTitle]); + + if (null !== $goal) { + $result->addGoal($goal); + $goal->addResult($result); + + $this->entityManager->persist($goal); + } else { + $result->addSocialAction($socialAction); + $socialAction->addResult($result); + } + + $this->entityManager->persist($result); + //$this->entityManager->persist($socialAction); + + return $result; + } + + /** + * @throws Exception + * + * @return array + */ + private function handleSocialAction( + ?string $socialActionTitle, + ?string $socialActionChildTitle, + SocialIssue $socialIssue, + ?SocialAction $previousSocialAction, + ?SocialAction $previousSocialActionChild + ): array { + if (null === $socialActionTitle) { + return [ + 'socialAction' => null, + 'socialActionChild' => null, + ]; + } + + $return = []; + + if (null !== $previousSocialAction && ($previousSocialAction->getTitle()['fr'] === $socialActionTitle)) { + $return = [ + 'socialAction' => $parent = $previousSocialAction, + ]; + $parentIsSame = true; + } else { + $return['socialAction'] = $parent = (new SocialAction())->setTitle(['fr' => $socialActionTitle]); + $parent->setIssue($socialIssue); + $this->entityManager->persist($parent); + $parentIsSame = false; + } + + if (null === $socialActionChildTitle) { + $return['socialActionChild'] = null; + } elseif ($parentIsSame && null !== $previousSocialActionChild && $previousSocialActionChild->getTitle()['fr'] === $socialActionChildTitle) { + $return['socialActionChild'] = $previousSocialActionChild; + } else { + $return['socialActionChild'] = $child = (new SocialAction())->setTitle(['fr' => $socialActionChildTitle]); + $parent->addChild($child); + $this->entityManager->persist($child); + } + + return $return; + } + + /** + * @throws Exception + * + * @return array + */ + private function handleSocialIssue( + ?string $socialIssueTitle, + ?string $socialIssueChildTitle, + ?SocialIssue $previousSocialIssue, + ?SocialIssue $previousSocialIssueChild + ): array { + $return = []; + + if (null !== $previousSocialIssue && ($previousSocialIssue->getTitle()['fr'] === $socialIssueTitle)) { + $return = [ + 'socialIssue' => $parent = $previousSocialIssue, + ]; + $parentIsSame = true; + } elseif (null !== $socialIssueTitle) { + $return['socialIssue'] = $parent = (new SocialIssue())->setTitle(['fr' => $socialIssueTitle]); + $this->entityManager->persist($parent); + $parentIsSame = false; + } else { + return [ + 'socialIssue' => null, + 'socialIssueChild' => null, + ]; + } + + if ($parentIsSame && null !== $previousSocialIssueChild && ($previousSocialIssueChild->getTitle()['fr'] === $socialIssueChildTitle)) { + $return['socialIssueChild'] = $previousSocialIssueChild; + } elseif (null !== $socialIssueChildTitle) { + $return['socialIssueChild'] = $child = (new SocialIssue())->setTitle(['fr' => $socialIssueChildTitle]); + $parent->addChild($child); + $this->entityManager->persist($child); + } else { + $return['socialIssueChild'] = null; + } + + return $return; + } + + /** + * 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 $row + * @param array|SocialIssue|string, SocialAction|null>|Evaluation|Goal|Result> $previousRow + * + * @throws Exception + * + * @return array|SocialIssue|string, SocialAction|null>|Evaluation|Goal|Result> + */ + private function import1(int $key, array $row, array $previousRow): array + { + $socialIssues = $this + ->handleSocialIssue( + $row[0], + $row[1], + $previousRow['socialIssues']['socialIssue'] ?? null, + $previousRow['socialIssues']['socialIssueChild'] ?? null + ); + + $socialIssue = $socialIssues['socialIssueChild'] ?? $socialIssues['socialIssue']; + + if (null === $socialIssue) { + throw new Exception(sprintf("no social issue on row {$key}, values: %s", implode(', ', $row))); + } + + $socialActions = $this + ->handleSocialAction( + $row[2], + $row[3], + $socialIssues['socialIssue'] ?? $socialIssues, + $previousRow['socialActions']['socialAction'] ?? null, + $previousRow['socialActions']['socialActionChild'] ?? null + ); + + $socialAction = $socialActions['socialActionChild'] ?? $socialActions['socialAction']; + + if (null !== $socialAction) { + $goal = $this->handleGoal($row[4], $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 ?? null, + 'result' => $result ?? null, + 'eval' => $eval ?? null, + ]; + } }