File size: 5,533 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
<?php

declare(strict_types=1);

namespace Mautic\IntegrationsBundle\Helper;

use Mautic\IntegrationsBundle\Exception\InvalidFormOptionException;
use Mautic\IntegrationsBundle\Integration\Interfaces\ConfigFormSyncInterface;
use Mautic\IntegrationsBundle\Mapping\MappedFieldInfoInterface;
use Mautic\IntegrationsBundle\Sync\DAO\Mapping\ObjectMappingDAO;

class FieldMergerHelper
{
    /**
     * @var MappedFieldInfoInterface[]
     */
    private ?array $allFields = null;

    public function __construct(
        private ConfigFormSyncInterface $integrationObject,
        private array $currentFieldMappings
    ) {
    }

    /**
     * @throws InvalidFormOptionException
     */
    public function mergeSyncFieldMapping(string $object, array $updatedFieldMappings): void
    {
        $this->allFields = $this->integrationObject->getAllFieldsForMapping($object);

        $this->removeNonExistentFieldMappings($object);

        $this->bindUpdatedFieldMappings($object, $updatedFieldMappings);
    }

    public function getFieldMappings(): array
    {
        return $this->currentFieldMappings;
    }

    private function removeNonExistentFieldMappings(string $object): void
    {
        if (!isset($this->currentFieldMappings[$object])) {
            $this->currentFieldMappings[$object] = [];
        }

        // Remove any fields that no longer exist
        $this->currentFieldMappings[$object] = array_intersect_key($this->currentFieldMappings[$object], $this->allFields);
    }

    /**
     * @throws InvalidFormOptionException
     */
    private function bindUpdatedFieldMappings(string $object, array $updatedFieldMappings): void
    {
        // Merge updated fields into current fields
        foreach ($updatedFieldMappings as $fieldName => $fieldMapping) {
            if (!isset($this->allFields[$fieldName])) {
                // Ignore as this field doesn't exist
                continue;
            }

            if (isset($this->currentFieldMappings[$object][$fieldName]) && !$fieldMapping) {
                // Mapping was deleted
                unset($this->currentFieldMappings[$object][$fieldName]);

                continue;
            }

            if (isset($this->currentFieldMappings[$object][$fieldName])) {
                // Merge the data
                $this->currentFieldMappings[$object][$fieldName] = [
                    'mappedField'   => $this->getMergedMappedField($fieldMapping, $object, $fieldName),
                    'syncDirection' => $this->getMergedSyncDirection($fieldMapping, $object, $fieldName),
                ];

                continue;
            }

            if (empty($fieldMapping['mappedField'])) {
                // Ignore this one because just direction was updated without a mapped field

                continue;
            }

            if (empty($fieldMapping['syncDirection'])) {
                $fieldMapping['syncDirection'] = $this->getDefaultSyncDirection($object, $fieldName);
            }

            $this->currentFieldMappings[$object][$fieldName] = $fieldMapping;
        }
    }

    /**
     * @throws InvalidFormOptionException
     */
    private function getDefaultSyncDirection(string $object, string $fieldName): string
    {
        $supportedDirections = $this->getSupportedSyncDirections($fieldName);

        if (!empty($this->currentFieldMappings[$object][$fieldName]['syncDirection'])
            && in_array(
                $this->currentFieldMappings[$object][$fieldName]['syncDirection'],
                $supportedDirections
            )) {
            // Keep the already configured value
            return $this->currentFieldMappings[$object][$fieldName]['syncDirection'];
        }

        return reset($supportedDirections);
    }

    /**
     * @throws InvalidFormOptionException
     */
    private function getSupportedSyncDirections(string $fieldName): array
    {
        $field               = $this->allFields[$fieldName];
        $supportedDirections = [];

        if ($field->isBidirectionalSyncEnabled()) {
            $supportedDirections[] = ObjectMappingDAO::SYNC_BIDIRECTIONALLY;
        }

        if ($field->isToIntegrationSyncEnabled()) {
            $supportedDirections[] = ObjectMappingDAO::SYNC_TO_INTEGRATION;
        }

        if ($field->isToMauticSyncEnabled()) {
            $supportedDirections[] = ObjectMappingDAO::SYNC_TO_MAUTIC;
        }

        if (empty($supportedDirections)) {
            throw new InvalidFormOptionException('field "'.$field->getName().'" must allow at least 1 direction for sync');
        }

        return $supportedDirections;
    }

    private function getMergedSyncDirection(array $fieldMapping, string $object, string $fieldName): string
    {
        if (empty($fieldMapping['syncDirection'])) {
            return $this->getDefaultSyncDirection($object, $fieldName);
        }

        $supportedDirections = $this->getSupportedSyncDirections($fieldName);
        if (in_array($fieldMapping['syncDirection'], $supportedDirections)) {
            return $fieldMapping['syncDirection'];
        }

        return reset($supportedDirections);
    }

    private function getMergedMappedField(array $fieldMapping, string $object, string $fieldName): string
    {
        if (empty($fieldMapping['mappedField'])) {
            // Updating just the sync direction so return original value
            return $this->currentFieldMappings[$object][$fieldName]['mappedField'];
        }

        return $fieldMapping['mappedField'];
    }
}