various fixes

This commit is contained in:
Julien Fastré 2021-12-13 13:48:20 +01:00
parent fdae6c106a
commit d01eaa8065
3 changed files with 241 additions and 232 deletions

View File

@ -98,7 +98,7 @@ class SocialAction
{ {
if (!$this->children->contains($child)) { if (!$this->children->contains($child)) {
$this->children[] = $child; $this->children[] = $child;
$child->setParent($this); $child->setParent($this)->setIssue($this->getIssue());
} }
return $this; return $this;
@ -266,9 +266,16 @@ class SocialAction
{ {
$this->issue = $issue; $this->issue = $issue;
foreach ($this->getChildren() as $child) {
$child->setIssue($issue);
}
return $this; return $this;
} }
/**
* @internal use $parent->addChild() instead (@see{self::addChild()})
*/
public function setParent(?self $parent): self public function setParent(?self $parent): self
{ {
$this->parent = $parent; $this->parent = $parent;

View File

@ -305,6 +305,9 @@ class SocialIssue
return $this; return $this;
} }
/**
* @internal use @see{SocialIssue::addChild()} instead
*/
public function setParent(?self $parent): self public function setParent(?self $parent): self
{ {
$this->parent = $parent; $this->parent = $parent;

View File

@ -1,5 +1,12 @@
<?php <?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1); declare(strict_types=1);
namespace Chill\PersonBundle\Service\Import; namespace Chill\PersonBundle\Service\Import;
@ -18,20 +25,21 @@ use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\Expr\Comparison; use Doctrine\ORM\Query\Expr\Comparison;
use Doctrine\Persistence\ObjectRepository; use Doctrine\Persistence\ObjectRepository;
use Exception; use Exception;
use function count;
final class SocialWorkMetadata implements SocialWorkMetadataInterface final class SocialWorkMetadata implements SocialWorkMetadataInterface
{ {
private SocialIssueRepository $socialIssueRepository; private EntityManagerInterface $entityManager;
private SocialActionRepository $socialActionRepository; private EvaluationRepository $evaluationRepository;
private GoalRepository $goalRepository; private GoalRepository $goalRepository;
private ResultRepository $resultRepository; private ResultRepository $resultRepository;
private EvaluationRepository $evaluationRepository; private SocialActionRepository $socialActionRepository;
private EntityManagerInterface $entityManager; private SocialIssueRepository $socialIssueRepository;
public function __construct( public function __construct(
SocialIssueRepository $socialIssueRepository, SocialIssueRepository $socialIssueRepository,
@ -69,9 +77,10 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
'eval' => null, 'eval' => null,
]; ];
foreach ($dataset as $row) { foreach ($dataset as $key => $row) {
$result = $this $result = $this
->import1( ->import1(
$key,
// Columns cleanup before importing data. // Columns cleanup before importing data.
array_map( array_map(
static fn (string $column): ?string => '' === $column ? null : $column, static fn (string $column): ?string => '' === $column ? null : $column,
@ -84,221 +93,6 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
return true; 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<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
{
$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<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,
];
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<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,
];
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<int, object> * @return array<int, object>
*/ */
@ -320,8 +114,7 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
$jsonParameters = array_reduce( $jsonParameters = array_reduce(
$temporaryJsonCriterias, $temporaryJsonCriterias,
static function (array $carry, array $row): array static function (array $carry, array $row): array {
{
[,, $value, $placeholder] = $row; [,, $value, $placeholder] = $row;
return array_merge( return array_merge(
@ -335,8 +128,7 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
); );
$jsonPredicates = array_map( $jsonPredicates = array_map(
static function (array $row) use ($expr): Comparison static function (array $row) use ($expr): Comparison {
{
[$field, $key,, $placeholder] = $row; [$field, $key,, $placeholder] = $row;
$left = sprintf( $left = sprintf(
@ -363,9 +155,9 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
} }
/** /**
* @return object
*
* @throws Exception * @throws Exception
*
* @return object
*/ */
private function getOrCreateEntity(ObjectRepository $repository, string $field, array $jsonCriterias = []) private function getOrCreateEntity(ObjectRepository $repository, string $field, array $jsonCriterias = [])
{ {
@ -376,10 +168,10 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
$jsonCriterias $jsonCriterias
); );
if ($results === []) { if ([] === $results) {
$entity = $repository->getClassName(); $entity = $repository->getClassName();
return new $entity; return new $entity();
} }
if (count($results) === 1) { if (count($results) === 1) {
@ -387,11 +179,218 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
} }
throw new Exception( 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<string, SocialAction|null>
*/
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<string, SocialIssue|null>
*/
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<int, ?string> $row
* @param array<string, array<string, <|array|null>|SocialIssue|string, SocialAction|null>|Evaluation|Goal|Result> $previousRow
*
* @throws Exception
*
* @return array<string, array<string, <|array|null>|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,
];
}
} }