mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Create thirdparty merge manager
This commit is contained in:
parent
40eb71f95a
commit
a562690512
125
src/Bundle/ChillThirdPartyBundle/Actions/MergeThirdparty/ThirdpartyMergeManager.php
vendored
Normal file
125
src/Bundle/ChillThirdPartyBundle/Actions/MergeThirdparty/ThirdpartyMergeManager.php
vendored
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Chill\ThirdPartyBundle\Actions\MergeThirdparty;
|
||||||
|
|
||||||
|
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
||||||
|
class ThirdpartyMergeManager
|
||||||
|
{
|
||||||
|
public function __construct(private readonly EntityManagerInterface $em, private iterable $handlers) {}
|
||||||
|
|
||||||
|
public function merge(ThirdParty $toKeep, ThirdParty $toDelete): void
|
||||||
|
{
|
||||||
|
// Transfer non-duplicate data
|
||||||
|
$this->transferData($toKeep, $toDelete);
|
||||||
|
|
||||||
|
// Update linked entities
|
||||||
|
$this->updateReferences($toKeep, $toDelete);
|
||||||
|
|
||||||
|
$this->em->remove($toDelete);
|
||||||
|
$this->em->flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function transferData(ThirdParty $toKeep, ThirdParty $toDelete): void
|
||||||
|
{
|
||||||
|
$excludedProperties = ['id', 'createdAt'];
|
||||||
|
$reflection = new \ReflectionClass(ThirdParty::class);
|
||||||
|
|
||||||
|
foreach ($reflection->getProperties() as $property) {
|
||||||
|
if (in_array($property->getName(), $excludedProperties, true)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$toKeepValue = $property->getValue($toKeep);
|
||||||
|
$toDeleteValue = $property->getValue($toDelete);
|
||||||
|
|
||||||
|
if (null === $toKeepValue && null !== $toDeleteValue) {
|
||||||
|
$property->setValue($toKeep, $toDeleteValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($toKeepValue instanceof \Doctrine\Common\Collections\Collection
|
||||||
|
&& $toDeleteValue instanceof \Doctrine\Common\Collections\Collection) {
|
||||||
|
foreach ($toDeleteValue as $item) {
|
||||||
|
if (!$toKeepValue->contains($item)) {
|
||||||
|
$toKeepValue->add($item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function updateReferences(ThirdParty $toKeep, ThirdParty $toDelete): void
|
||||||
|
{
|
||||||
|
$allMeta = $this->em->getMetadataFactory()->getAllMetadata();
|
||||||
|
|
||||||
|
foreach ($allMeta as $meta) {
|
||||||
|
foreach ($meta->getAssociationMappings() as $assoc) {
|
||||||
|
if (ThirdParty::class !== $assoc['targetEntity']) {
|
||||||
|
continue; // Skip unrelated associations
|
||||||
|
}
|
||||||
|
|
||||||
|
$entityClass = $meta->getName();
|
||||||
|
$associationField = $assoc['fieldName'];
|
||||||
|
|
||||||
|
if ($assoc['type'] & \Doctrine\ORM\Mapping\ClassMetadata::TO_ONE) {
|
||||||
|
// Handle ManyToOne or OneToOne
|
||||||
|
$qb = $this->em->createQueryBuilder();
|
||||||
|
$qb->update($entityClass, 'e')
|
||||||
|
->set("e.{$associationField}", ':toKeep')
|
||||||
|
->where("e.{$associationField} = :toDelete")
|
||||||
|
->setParameter('toKeep', $toKeep)
|
||||||
|
->setParameter('toDelete', $toDelete)
|
||||||
|
->getQuery()
|
||||||
|
->execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($assoc['type'] & \Doctrine\ORM\Mapping\ClassMetadata::TO_MANY) {
|
||||||
|
// Handle ManyToMany or OneToMany (inverse side)
|
||||||
|
$repo = $this->em->getRepository($entityClass);
|
||||||
|
$linkedEntities = $repo->createQueryBuilder('e')
|
||||||
|
->join("e.{$associationField}", 't')
|
||||||
|
->where('t = :toDelete')
|
||||||
|
->setParameter('toDelete', $toDelete)
|
||||||
|
->getQuery()
|
||||||
|
->getResult();
|
||||||
|
|
||||||
|
foreach ($linkedEntities as $entity) {
|
||||||
|
$getter = 'get'.ucfirst($associationField);
|
||||||
|
$setter = 'set'.ucfirst($associationField);
|
||||||
|
$adder = 'add'.ucfirst(rtrim($associationField, 's'));
|
||||||
|
$remover = 'remove'.ucfirst(rtrim($associationField, 's'));
|
||||||
|
|
||||||
|
if (method_exists($entity, $getter) && method_exists($entity, $setter)) {
|
||||||
|
// For OneToMany owning side
|
||||||
|
$collection = $entity->{$getter}();
|
||||||
|
if ($collection->contains($toDelete)) {
|
||||||
|
$collection->removeElement($toDelete);
|
||||||
|
if (!$collection->contains($toKeep)) {
|
||||||
|
$collection->add($toKeep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} elseif (method_exists($entity, $adder) && method_exists($entity, $remover)) {
|
||||||
|
// For ManyToMany
|
||||||
|
$entity->{$remover}($toDelete);
|
||||||
|
$entity->{$adder}($toKeep);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->em->persist($entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->em->flush();
|
||||||
|
}
|
||||||
|
}
|
@ -51,6 +51,7 @@ class ChillThirdPartyExtension extends Extension implements PrependExtensionInte
|
|||||||
$loader->load('services/serializer.yaml');
|
$loader->load('services/serializer.yaml');
|
||||||
$loader->load('services/repository.yaml');
|
$loader->load('services/repository.yaml');
|
||||||
$loader->load('services/doctrineEventListener.yaml');
|
$loader->load('services/doctrineEventListener.yaml');
|
||||||
|
$loader->load('services/actions.yaml');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function prepend(ContainerBuilder $container)
|
public function prepend(ContainerBuilder $container)
|
||||||
|
@ -0,0 +1,6 @@
|
|||||||
|
services:
|
||||||
|
_defaults:
|
||||||
|
autowire: true
|
||||||
|
autoconfigure: true
|
||||||
|
|
||||||
|
Chill\ThirdPartyBundle\Actions\MergeThirdparty\ThirdpartyMergeManager: ~
|
Loading…
x
Reference in New Issue
Block a user