-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathMappingManualFactory.php
More file actions
105 lines (84 loc) · 3.46 KB
/
Copy pathMappingManualFactory.php
File metadata and controls
105 lines (84 loc) · 3.46 KB
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
<?php
declare(strict_types=1);
namespace MauticPlugin\HelloWorldBundle\Sync\Mapping\Manual;
use MauticPlugin\HelloWorldBundle\Integration\Config;
use MauticPlugin\HelloWorldBundle\Integration\HelloWorldIntegration;
use MauticPlugin\HelloWorldBundle\Sync\Mapping\Field\Field;
use MauticPlugin\HelloWorldBundle\Sync\Mapping\Field\FieldRepository;
use MauticPlugin\IntegrationsBundle\Exception\InvalidValueException;
use MauticPlugin\IntegrationsBundle\Sync\DAO\Mapping\MappingManualDAO;
use MauticPlugin\IntegrationsBundle\Sync\DAO\Mapping\ObjectMappingDAO;
use MauticPlugin\IntegrationsBundle\Sync\SyncDataExchange\Internal\Object\Company;
use MauticPlugin\IntegrationsBundle\Sync\SyncDataExchange\Internal\Object\Contact;
class MappingManualFactory
{
public const CITIZEN_OBJECT = 'citizen';
public const WORLD_OBJECT = 'world';
/**
* @var FieldRepository
*/
private $fieldRepository;
/**
* @var Config
*/
private $config;
/**
* @var MappingManualDAO
*/
private $manual;
public function __construct(FieldRepository $fieldRepository, Config $config)
{
$this->fieldRepository = $fieldRepository;
$this->config = $config;
}
public function getManual(): MappingManualDAO
{
if ($this->manual) {
return $this->manual;
}
// Instructions to the sync engine on how to map fields and the direction of data should flow
$this->manual = new MappingManualDAO(HelloWorldIntegration::NAME);
// In this case, two objects are supported. Citizen to Mautic Contact and World to Mautic Company.
$this->configureObjectMapping(self::CITIZEN_OBJECT);
$this->configureObjectMapping(self::WORLD_OBJECT);
return $this->manual;
}
private function configureObjectMapping(string $objectName): void
{
// Get a list of available fields from the integration
$fields = $this->fieldRepository->getFields($objectName);
// Get a list of fields mapped by the user
$mappedFields = $this->config->getMappedFields($objectName);
// Generate an object mapping DAO for the given object. The object must be mapped to a supported Mautic object (i.e. contact or company)
$objectMappingDAO = new ObjectMappingDAO($this->getMauticObjectName($objectName), $objectName);
foreach ($mappedFields as $fieldAlias => $mauticFieldAlias) {
if (!isset($fields[$fieldAlias])) {
// The mapped field is no longer available
continue;
}
/** @var Field $field */
$field = $fields[$fieldAlias];
// Configure how fields should be handled by the sync engine as determined by the user's configuration.
$objectMappingDAO->addFieldMapping(
$mauticFieldAlias,
$fieldAlias,
$this->config->getFieldDirection($objectName, $fieldAlias),
$field->isRequired()
);
$this->manual->addObjectMapping($objectMappingDAO);
}
}
/**
* @throws InvalidValueException
*/
private function getMauticObjectName(string $objectName): string
{
switch ($objectName) {
case self::WORLD_OBJECT:
return Company::NAME;
case self::CITIZEN_OBJECT:
return Contact::NAME;
}
throw new InvalidValueException("$objectName could not be mapped to a Mautic object");
}
}