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] 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, + ]; + } }