-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSynchronization.php
138 lines (123 loc) · 5.66 KB
/
Synchronization.php
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
<?php
namespace OCA\OpenConnector\Db;
use DateTime;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
class Synchronization extends Entity implements JsonSerializable
{
protected ?string $uuid = null;
protected ?string $name = null; // The name of the synchronization
protected ?string $description = null; // The description of the synchronization
protected ?string $version = null; // The version of the synchronization
// Source
protected ?string $sourceId = null; // The id of the source object
protected ?string $sourceType = null; // The type of the source object (e.g. api, database, register/schema.)
protected ?string $sourceHash = null; // The hash of the source object when it was last synced.
protected ?string $sourceTargetMapping = null; // The mapping of the source object to the target object
protected ?array $sourceConfig = null; // The configuration of the object in the source
protected ?DateTime $sourceLastChanged = null; // The last changed date of the source object
protected ?DateTime $sourceLastChecked = null; // The last checked date of the source object
protected ?DateTime $sourceLastSynced = null; // The last synced date of the source object
// Target
protected ?string $targetId = null; // The id of the target object
protected ?string $targetType = null; // The type of the target object (e.g. api, database, register/schema.)
protected ?string $targetHash = null; // The hash of the target object
protected ?string $targetSourceMapping = null; // The mapping of the target object to the source object
protected ?array $targetConfig = null; // The configuration of the object in the target
protected ?DateTime $targetLastChanged = null; // The last changed date of the target object
protected ?DateTime $targetLastChecked = null; // The last checked date of the target object
protected ?DateTime $targetLastSynced = null; // The last synced date of the target object
// General
protected ?DateTime $created = null; // The date and time the synchronization was created
protected ?DateTime $updated = null; // The date and time the synchronization was updated
protected array $conditions = [];
protected array $followUps = [];
public function __construct() {
$this->addType('uuid', 'string');
$this->addType('name', 'string');
$this->addType('description', 'string');
$this->addType('version', 'string');
$this->addType('sourceId', 'string');
$this->addType('sourceType', 'string');
$this->addType('sourceHash', 'string');
$this->addType('sourceTargetMapping', 'string');
$this->addType('sourceConfig', 'json');
$this->addType('sourceLastChanged', 'datetime');
$this->addType('sourceLastChecked', 'datetime');
$this->addType('sourceLastSynced', 'datetime');
$this->addType('targetId', 'string');
$this->addType('targetType', 'string');
$this->addType('targetHash', 'string');
$this->addType('targetSourceMapping', 'string');
$this->addType('targetConfig', 'json');
$this->addType('targetLastChanged', 'datetime');
$this->addType('targetLastChecked', 'datetime');
$this->addType('targetLastSynced', 'datetime');
$this->addType('created', 'datetime');
$this->addType('updated', 'datetime');
$this->addType(fieldName:'conditions', type: 'json');
$this->addType(fieldName:'followUps', type: 'json');
}
public function usesPagination(): bool
{
if (isset($this->sourceConfig['usesPagination']) === true && ($this->sourceConfig['usesPagination'] === false || $this->sourceConfig['usesPagination'] === 'false')) {
return false;
}
// By default sources use basic pagination.
return true;
}
public function getJsonFields(): array
{
return array_keys(
array_filter($this->getFieldTypes(), function ($field) {
return $field === 'json';
})
);
}
public function hydrate(array $object): self
{
$jsonFields = $this->getJsonFields();
foreach ($object as $key => $value) {
if (in_array($key, $jsonFields) === true && $value === []) {
$value = [];
}
$method = 'set'.ucfirst($key);
try {
$this->$method($value);
} catch (\Exception $exception) {
// Error handling could be improved here
}
}
return $this;
}
public function jsonSerialize(): array
{
return [
'id' => $this->id,
'uuid' => $this->uuid,
'name' => $this->name,
'description' => $this->description,
'version' => $this->version,
'sourceId' => $this->sourceId,
'sourceType' => $this->sourceType,
'sourceHash' => $this->sourceHash,
'sourceTargetMapping' => $this->sourceTargetMapping,
'sourceConfig' => $this->sourceConfig,
'sourceLastChanged' => isset($this->sourceLastChanged) === true ? $this->sourceLastChanged->format('c') : null,
'sourceLastChecked' => isset($this->sourceLastChecked) === true ? $this->sourceLastChecked->format('c') : null,
'sourceLastSynced' => isset($this->sourceLastSynced) === true ? $this->sourceLastSynced->format('c') : null,
'targetId' => $this->targetId,
'targetType' => $this->targetType,
'targetHash' => $this->targetHash,
'targetSourceMapping' => $this->targetSourceMapping,
'targetConfig' => $this->targetConfig,
'targetLastChanged' => isset($this->targetLastChanged) === true ? $this->targetLastChanged->format('c') : null,
'targetLastChecked' => isset($this->targetLastChecked) === true ? $this->targetLastChecked->format('c') : null,
'targetLastSynced' => isset($this->targetLastSynced) === true ? $this->targetLastSynced->format('c') : null,
'created' => isset($this->created) === true ? $this->created->format('c') : null,
'updated' => isset($this->updated) === true ? $this->updated->format('c') : null,
'conditions' => $this->conditions,
'followUps' => $this->followUps,
];
}
}