Spaces:
No application file
No application file
File size: 8,869 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
<?php
declare(strict_types=1);
namespace Mautic\IntegrationsBundle\Sync\Helper;
use Mautic\IntegrationsBundle\Entity\ObjectMapping;
use Mautic\IntegrationsBundle\Entity\ObjectMappingRepository;
use Mautic\IntegrationsBundle\Event\InternalObjectFindEvent;
use Mautic\IntegrationsBundle\IntegrationEvents;
use Mautic\IntegrationsBundle\Sync\DAO\Mapping\MappingManualDAO;
use Mautic\IntegrationsBundle\Sync\DAO\Mapping\RemappedObjectDAO;
use Mautic\IntegrationsBundle\Sync\DAO\Mapping\UpdatedObjectMappingDAO;
use Mautic\IntegrationsBundle\Sync\DAO\Sync\Order\ObjectChangeDAO;
use Mautic\IntegrationsBundle\Sync\DAO\Sync\Report\ObjectDAO;
use Mautic\IntegrationsBundle\Sync\Exception\FieldNotFoundException;
use Mautic\IntegrationsBundle\Sync\Exception\ObjectDeletedException;
use Mautic\IntegrationsBundle\Sync\Exception\ObjectNotFoundException;
use Mautic\IntegrationsBundle\Sync\Exception\ObjectNotSupportedException;
use Mautic\IntegrationsBundle\Sync\SyncDataExchange\Internal\ObjectProvider;
use Mautic\IntegrationsBundle\Sync\SyncDataExchange\MauticSyncDataExchange;
use Mautic\LeadBundle\Model\FieldModel;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class MappingHelper
{
public function __construct(
private FieldModel $fieldModel,
private ObjectMappingRepository $objectMappingRepository,
private ObjectProvider $objectProvider,
private EventDispatcherInterface $dispatcher
) {
}
/**
* @throws ObjectDeletedException
* @throws ObjectNotFoundException
* @throws ObjectNotSupportedException
*/
public function findMauticObject(MappingManualDAO $mappingManualDAO, string $internalObjectName, ObjectDAO $integrationObjectDAO): ObjectDAO
{
// Check if this contact is already tracked
if ($internalObject = $this->objectMappingRepository->getInternalObject(
$mappingManualDAO->getIntegration(),
$integrationObjectDAO->getObject(),
$integrationObjectDAO->getObjectId(),
$internalObjectName
)) {
if ($internalObject['is_deleted']) {
throw new ObjectDeletedException();
}
return new ObjectDAO(
$internalObjectName,
$internalObject['internal_object_id'],
new \DateTime($internalObject['last_sync_date'], new \DateTimeZone('UTC'))
);
}
// We don't know who this is so search Mautic
$uniqueIdentifierFields = $this->fieldModel->getUniqueIdentifierFields(['object' => $internalObjectName]);
$identifiers = [];
foreach ($uniqueIdentifierFields as $field => $fieldLabel) {
try {
$integrationField = $mappingManualDAO->getIntegrationMappedField($integrationObjectDAO->getObject(), $internalObjectName, $field);
if ($integrationValue = $integrationObjectDAO->getField($integrationField)) {
$identifiers[$field] = $integrationValue->getValue()->getNormalizedValue();
}
} catch (FieldNotFoundException) {
}
}
if (empty($identifiers)) {
// No fields found to search for contact so return null
return new ObjectDAO($internalObjectName, null);
}
try {
$event = new InternalObjectFindEvent(
$this->objectProvider->getObjectByName($internalObjectName)
);
} catch (ObjectNotFoundException) {
// Throw this exception for BC.
throw new ObjectNotSupportedException(MauticSyncDataExchange::NAME, $internalObjectName);
}
$event->setFieldValues($identifiers);
$this->dispatcher->dispatch(
$event,
IntegrationEvents::INTEGRATION_FIND_INTERNAL_RECORDS,
);
$foundObjects = $event->getFoundObjects();
if (!$foundObjects) {
// No contacts were found
return new ObjectDAO($internalObjectName, null);
}
// Match found!
$objectId = $foundObjects[0]['id'];
// Let's store the relationship since we know it
$objectMapping = new ObjectMapping();
$objectMapping->setLastSyncDate($integrationObjectDAO->getChangeDateTime())
->setIntegration($mappingManualDAO->getIntegration())
->setIntegrationObjectName($integrationObjectDAO->getObject())
->setIntegrationObjectId($integrationObjectDAO->getObjectId())
->setInternalObjectName($internalObjectName)
->setInternalObjectId($objectId);
$this->saveObjectMapping($objectMapping);
return new ObjectDAO($internalObjectName, $objectId);
}
/**
* Returns corresponding Mautic entity class name for the given Mautic object.
*
* @throws ObjectNotSupportedException
*/
public function getMauticEntityClassName(string $internalObject): string
{
try {
return $this->objectProvider->getObjectByName($internalObject)->getEntityName();
} catch (ObjectNotFoundException) {
// Throw this exception instead to keep BC.
throw new ObjectNotSupportedException(MauticSyncDataExchange::NAME, $internalObject);
}
}
/**
* @throws ObjectDeletedException
*/
public function findIntegrationObject(string $integration, string $integrationObjectName, ObjectDAO $internalObjectDAO): ObjectDAO
{
if ($integrationObject = $this->objectMappingRepository->getIntegrationObject(
$integration,
$internalObjectDAO->getObject(),
$internalObjectDAO->getObjectId(),
$integrationObjectName
)) {
if ($integrationObject['is_deleted']) {
throw new ObjectDeletedException();
}
return new ObjectDAO(
$integrationObjectName,
$integrationObject['integration_object_id'],
new \DateTime($integrationObject['last_sync_date'], new \DateTimeZone('UTC'))
);
}
return new ObjectDAO($integrationObjectName, null);
}
/**
* @param ObjectMapping[] $mappings
*/
public function saveObjectMappings(array $mappings): void
{
foreach ($mappings as $mapping) {
$this->saveObjectMapping($mapping);
}
}
public function updateObjectMappings(array $mappings): void
{
foreach ($mappings as $mapping) {
try {
$this->updateObjectMapping($mapping);
} catch (ObjectNotFoundException) {
continue;
}
}
}
/**
* @param RemappedObjectDAO[] $mappings
*/
public function remapIntegrationObjects(array $mappings): void
{
foreach ($mappings as $mapping) {
$this->objectMappingRepository->updateIntegrationObject(
$mapping->getIntegration(),
$mapping->getOldObjectName(),
$mapping->getOldObjectId(),
$mapping->getNewObjectName(),
$mapping->getNewObjectId()
);
}
}
/**
* @param ObjectChangeDAO[] $objects
*/
public function markAsDeleted(array $objects): void
{
foreach ($objects as $object) {
$this->objectMappingRepository->markAsDeleted($object->getIntegration(), $object->getObject(), $object->getObjectId());
}
}
private function saveObjectMapping(ObjectMapping $objectMapping): void
{
$this->objectMappingRepository->saveEntity($objectMapping);
$this->objectMappingRepository->detachEntity($objectMapping);
}
/**
* @throws ObjectNotFoundException
*/
private function updateObjectMapping(UpdatedObjectMappingDAO $updatedObjectMappingDAO): void
{
/** @var ObjectMapping $objectMapping */
$objectMapping = $this->objectMappingRepository->findOneBy(
[
'integration' => $updatedObjectMappingDAO->getIntegration(),
'integrationObjectName' => $updatedObjectMappingDAO->getIntegrationObjectName(),
'integrationObjectId' => $updatedObjectMappingDAO->getIntegrationObjectId(),
]
);
if (!$objectMapping) {
throw new ObjectNotFoundException($updatedObjectMappingDAO->getIntegrationObjectName().':'.$updatedObjectMappingDAO->getIntegrationObjectId());
}
$objectMapping->setLastSyncDate($updatedObjectMappingDAO->getObjectModifiedDate());
$this->saveObjectMapping($objectMapping);
// Make the ObjectMapping available to the IntegrationEvents::INTEGRATION_BATCH_SYNC_COMPLETED_* events
$updatedObjectMappingDAO->setObjectMapping($objectMapping);
}
}
|