mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 23:23:51 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
<?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);
|
||||
|
||||
namespace Chill\PersonBundle\Service\Import;
|
||||
|
@@ -1,5 +1,12 @@
|
||||
<?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);
|
||||
|
||||
namespace Chill\PersonBundle\Service\Import;
|
||||
@@ -21,17 +28,17 @@ use Exception;
|
||||
|
||||
final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
||||
{
|
||||
private SocialIssueRepository $socialIssueRepository;
|
||||
private EntityManagerInterface $entityManager;
|
||||
|
||||
private SocialActionRepository $socialActionRepository;
|
||||
private EvaluationRepository $evaluationRepository;
|
||||
|
||||
private GoalRepository $goalRepository;
|
||||
|
||||
private ResultRepository $resultRepository;
|
||||
|
||||
private EvaluationRepository $evaluationRepository;
|
||||
private SocialActionRepository $socialActionRepository;
|
||||
|
||||
private EntityManagerInterface $entityManager;
|
||||
private SocialIssueRepository $socialIssueRepository;
|
||||
|
||||
public function __construct(
|
||||
SocialIssueRepository $socialIssueRepository,
|
||||
@@ -63,79 +70,118 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
private function import1(array $row): void
|
||||
private function findByJson(ObjectRepository $repository, string $field, array $jsonCriterias): 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
|
||||
$qb = $this
|
||||
->entityManager
|
||||
->createQueryBuilder()
|
||||
->select('s')
|
||||
->from($repository->getClassName(), 's');
|
||||
|
||||
$socialIssue = $this->handleSocialIssue($row[0], $row[1]);
|
||||
$expr = $qb->expr();
|
||||
|
||||
$socialAction = $this->handleSocialAction($row[2], $row[3], $socialIssue);
|
||||
$temporaryJsonCriterias = $jsonParameters = [];
|
||||
|
||||
$goal = $this->handleGoal($row[4], $socialAction);
|
||||
foreach ($jsonCriterias as $key => $value) {
|
||||
$temporaryJsonCriterias[] = [$field, $key, $value, sprintf(':placeholder_%s_%s', $field, $key)];
|
||||
}
|
||||
|
||||
$result = $this->handleResult($row[5], $socialAction, $goal);
|
||||
$jsonParameters = array_reduce(
|
||||
$temporaryJsonCriterias,
|
||||
static function (array $carry, array $row): array {
|
||||
[,, $value, $placeholder] = $row;
|
||||
|
||||
$eval = $this->handleEvaluation($row[6], $socialAction);
|
||||
return array_merge(
|
||||
$carry,
|
||||
[
|
||||
$placeholder => sprintf('"%s"', $value),
|
||||
]
|
||||
);
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
$this->entityManager->flush();
|
||||
$jsonPredicates = array_map(
|
||||
static function (array $row) use ($expr): Comparison {
|
||||
[$field, $key,, $placeholder] = $row;
|
||||
|
||||
$left = sprintf(
|
||||
"GET_JSON_FIELD_BY_KEY(s.%s, '%s')",
|
||||
$field,
|
||||
$key
|
||||
);
|
||||
|
||||
return $expr
|
||||
->eq(
|
||||
$left,
|
||||
$placeholder
|
||||
);
|
||||
},
|
||||
$temporaryJsonCriterias
|
||||
);
|
||||
|
||||
$query = $qb
|
||||
->select('s')
|
||||
->where(...$jsonPredicates)
|
||||
->setParameters($jsonParameters)
|
||||
->getQuery();
|
||||
|
||||
return $query->getResult();
|
||||
}
|
||||
|
||||
private function handleSocialIssue(?string $socialIssueTitle = null, ?string $socialIssueChildrenTitle = null): SocialIssue
|
||||
private function getOrCreateEntity(ObjectRepository $repository, string $field, array $jsonCriterias = [])
|
||||
{
|
||||
if (null !== $socialIssueChildrenTitle) {
|
||||
/** @var SocialIssue $socialIssueChildren */
|
||||
$socialIssueChildren = $this->getOrCreateEntity($this->socialIssueRepository, 'title', ['fr' => $socialIssueChildrenTitle]);
|
||||
$socialIssueChildren->setTitle(['fr' => $socialIssueChildrenTitle]);
|
||||
$results = $this
|
||||
->findByJson(
|
||||
$repository,
|
||||
$field,
|
||||
$jsonCriterias
|
||||
);
|
||||
|
||||
$this->entityManager->persist($socialIssueChildren);
|
||||
$entity = null;
|
||||
|
||||
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()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/** @var SocialIssue $socialIssue */
|
||||
$socialIssue = $this->getOrCreateEntity($this->socialIssueRepository, 'title', ['fr' => $socialIssueTitle]);
|
||||
$socialIssue->setTitle(['fr' => $socialIssueTitle]);
|
||||
|
||||
if (null !== $socialIssueChildrenTitle) {
|
||||
$socialIssue->addChild($socialIssueChildren);
|
||||
if (null === $entity) {
|
||||
throw new Exception('Unable to create entity.');
|
||||
}
|
||||
|
||||
$this->entityManager->persist($socialIssue);
|
||||
|
||||
return null === $socialIssueChildrenTitle ? $socialIssue : $socialIssueChildren;
|
||||
return $entity;
|
||||
}
|
||||
|
||||
private function handleSocialAction(?string $socialActionTitle, ?string $socialActionChildrenTitle, SocialIssue $socialIssue): SocialAction
|
||||
private function handleEvaluation(?string $evaluationTitle, SocialAction $socialAction): ?Evaluation
|
||||
{
|
||||
if (null !== $socialActionChildrenTitle) {
|
||||
/** @var SocialAction $socialActionChildren */
|
||||
$socialActionChildren = $this->getOrCreateEntity($this->socialActionRepository, 'title', ['fr' => $socialActionChildrenTitle]);
|
||||
$socialActionChildren->setTitle(['fr' => $socialActionChildrenTitle]);
|
||||
|
||||
$this->entityManager->persist($socialActionChildren);
|
||||
if (null === $evaluationTitle) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var SocialIssue $socialIssue */
|
||||
$socialAction = $this->getOrCreateEntity($this->socialActionRepository, 'title', ['fr' => $socialActionTitle]);
|
||||
$socialAction->setTitle(['fr' => $socialActionTitle]);
|
||||
/** @var Evaluation $eval */
|
||||
$eval = $this->getOrCreateEntity($this->evaluationRepository, 'title', ['fr' => $evaluationTitle]);
|
||||
$eval->setTitle(['fr' => $evaluationTitle]);
|
||||
$eval->setSocialAction($socialAction);
|
||||
|
||||
if (null !== $socialActionChildrenTitle) {
|
||||
$socialActionChildren->setIssue($socialIssue);
|
||||
$this->entityManager->persist($socialActionChildren);
|
||||
$this->entityManager->persist($eval);
|
||||
|
||||
$socialAction->addChild($socialActionChildren);
|
||||
} else {
|
||||
$socialAction->setIssue($socialIssue);
|
||||
}
|
||||
|
||||
$this->entityManager->persist($socialAction);
|
||||
|
||||
return null === $socialActionChildrenTitle ? $socialAction : $socialActionChildren;
|
||||
return $eval;
|
||||
}
|
||||
|
||||
private function handleGoal(?string $goalTitle = null, ?SocialAction $socialAction = null): ?Goal
|
||||
@@ -188,117 +234,78 @@ final class SocialWorkMetadata implements SocialWorkMetadataInterface
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function handleEvaluation(?string $evaluationTitle, SocialAction $socialAction): ?Evaluation
|
||||
private function handleSocialAction(?string $socialActionTitle, ?string $socialActionChildrenTitle, SocialIssue $socialIssue): SocialAction
|
||||
{
|
||||
if (null === $evaluationTitle) {
|
||||
return null;
|
||||
if (null !== $socialActionChildrenTitle) {
|
||||
/** @var SocialAction $socialActionChildren */
|
||||
$socialActionChildren = $this->getOrCreateEntity($this->socialActionRepository, 'title', ['fr' => $socialActionChildrenTitle]);
|
||||
$socialActionChildren->setTitle(['fr' => $socialActionChildrenTitle]);
|
||||
|
||||
$this->entityManager->persist($socialActionChildren);
|
||||
}
|
||||
|
||||
/** @var Evaluation $eval */
|
||||
$eval = $this->getOrCreateEntity($this->evaluationRepository, 'title', ['fr' => $evaluationTitle]);
|
||||
$eval->setTitle(['fr' => $evaluationTitle]);
|
||||
$eval->setSocialAction($socialAction);
|
||||
/** @var SocialIssue $socialIssue */
|
||||
$socialAction = $this->getOrCreateEntity($this->socialActionRepository, 'title', ['fr' => $socialActionTitle]);
|
||||
$socialAction->setTitle(['fr' => $socialActionTitle]);
|
||||
|
||||
$this->entityManager->persist($eval);
|
||||
if (null !== $socialActionChildrenTitle) {
|
||||
$socialActionChildren->setIssue($socialIssue);
|
||||
$this->entityManager->persist($socialActionChildren);
|
||||
|
||||
return $eval;
|
||||
$socialAction->addChild($socialActionChildren);
|
||||
} else {
|
||||
$socialAction->setIssue($socialIssue);
|
||||
}
|
||||
|
||||
$this->entityManager->persist($socialAction);
|
||||
|
||||
return null === $socialActionChildrenTitle ? $socialAction : $socialActionChildren;
|
||||
}
|
||||
|
||||
private function findByJson(ObjectRepository $repository, string $field, array $jsonCriterias): array
|
||||
private function handleSocialIssue(?string $socialIssueTitle = null, ?string $socialIssueChildrenTitle = null): SocialIssue
|
||||
{
|
||||
$qb = $this
|
||||
->entityManager
|
||||
->createQueryBuilder()
|
||||
->select('s')
|
||||
->from($repository->getClassName(), 's');
|
||||
if (null !== $socialIssueChildrenTitle) {
|
||||
/** @var SocialIssue $socialIssueChildren */
|
||||
$socialIssueChildren = $this->getOrCreateEntity($this->socialIssueRepository, 'title', ['fr' => $socialIssueChildrenTitle]);
|
||||
$socialIssueChildren->setTitle(['fr' => $socialIssueChildrenTitle]);
|
||||
|
||||
$expr = $qb->expr();
|
||||
|
||||
$temporaryJsonCriterias = $jsonParameters = [];
|
||||
|
||||
foreach ($jsonCriterias as $key => $value) {
|
||||
$temporaryJsonCriterias[] = [$field, $key, $value, sprintf(':placeholder_%s_%s', $field, $key)];
|
||||
$this->entityManager->persist($socialIssueChildren);
|
||||
}
|
||||
|
||||
$jsonParameters = array_reduce(
|
||||
$temporaryJsonCriterias,
|
||||
static function (array $carry, array $row): array
|
||||
{
|
||||
[,, $value, $placeholder] = $row;
|
||||
/** @var SocialIssue $socialIssue */
|
||||
$socialIssue = $this->getOrCreateEntity($this->socialIssueRepository, 'title', ['fr' => $socialIssueTitle]);
|
||||
$socialIssue->setTitle(['fr' => $socialIssueTitle]);
|
||||
|
||||
return array_merge(
|
||||
$carry,
|
||||
[
|
||||
$placeholder => sprintf('"%s"', $value),
|
||||
]
|
||||
);
|
||||
},
|
||||
[]
|
||||
);
|
||||
if (null !== $socialIssueChildrenTitle) {
|
||||
$socialIssue->addChild($socialIssueChildren);
|
||||
}
|
||||
|
||||
$jsonPredicates = array_map(
|
||||
static function (array $row) use ($expr): Comparison
|
||||
{
|
||||
[$field, $key,, $placeholder] = $row;
|
||||
$this->entityManager->persist($socialIssue);
|
||||
|
||||
$left = sprintf(
|
||||
"GET_JSON_FIELD_BY_KEY(s.%s, '%s')",
|
||||
$field,
|
||||
$key
|
||||
);
|
||||
|
||||
return $expr
|
||||
->eq(
|
||||
$left,
|
||||
$placeholder
|
||||
);
|
||||
},
|
||||
$temporaryJsonCriterias
|
||||
);
|
||||
|
||||
$query = $qb
|
||||
->select('s')
|
||||
->where(...$jsonPredicates)
|
||||
->setParameters($jsonParameters)
|
||||
->getQuery();
|
||||
|
||||
return $query->getResult();
|
||||
return null === $socialIssueChildrenTitle ? $socialIssue : $socialIssueChildren;
|
||||
}
|
||||
|
||||
private function getOrCreateEntity(ObjectRepository $repository, string $field, array $jsonCriterias = [])
|
||||
private function import1(array $row): void
|
||||
{
|
||||
$results = $this
|
||||
->findByJson(
|
||||
$repository,
|
||||
$field,
|
||||
$jsonCriterias
|
||||
);
|
||||
// Structure:
|
||||
// Index 0: SocialIssue.parent
|
||||
// Index 1: SocialIssue
|
||||
// Index 2: SocialAction.parent
|
||||
// Index 3: SocialAction
|
||||
// Index 4: Goal
|
||||
// Index 5: Result
|
||||
// Index 6: Evaluation
|
||||
|
||||
$entity = null;
|
||||
$socialIssue = $this->handleSocialIssue($row[0], $row[1]);
|
||||
|
||||
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()
|
||||
)
|
||||
);
|
||||
}
|
||||
$socialAction = $this->handleSocialAction($row[2], $row[3], $socialIssue);
|
||||
|
||||
if (null === $entity) {
|
||||
throw new Exception('Unable to create entity.');
|
||||
}
|
||||
$goal = $this->handleGoal($row[4], $socialAction);
|
||||
|
||||
return $entity;
|
||||
$result = $this->handleResult($row[5], $socialAction, $goal);
|
||||
|
||||
$eval = $this->handleEvaluation($row[6], $socialAction);
|
||||
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -1,5 +1,12 @@
|
||||
<?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);
|
||||
|
||||
namespace Chill\PersonBundle\Service\Import;
|
||||
|
Reference in New Issue
Block a user