Skip to content

Commit 7bf6dd7

Browse files
Logic for upserting to CRM in some cases
1 parent 868e8c2 commit 7bf6dd7

4 files changed

Lines changed: 445 additions & 155 deletions

File tree

app/Synchronizer/Config.php

Lines changed: 73 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
class Config
1313
{
1414
private const CRM_ID_KEY = 'id';
15-
15+
1616
private $fields;
1717
private $auth;
1818
private $dataOwner;
1919
private $mailchimp;
2020
private $errors;
2121
private $crmEmailKey;
2222
private $webling;
23-
23+
2424
/**
2525
* Config constructor.
2626
*
@@ -32,7 +32,7 @@ public function __construct(string $configFileName)
3232
{
3333
$this->loadConfig($configFileName);
3434
}
35-
35+
3636
/**
3737
* Read the config file and populate object
3838
*
@@ -45,52 +45,52 @@ private function loadConfig(string $configFileName)
4545
$configFileName = ltrim($configFileName, './');
4646
$configFolderPath = rtrim(config('app.config_base_path'), '/');
4747
$configFilePath = base_path($configFolderPath . '/' . $configFileName);
48-
48+
4949
if (!file_exists($configFilePath)) {
5050
throw new ConfigException('The config file was not found.');
5151
}
52-
52+
5353
try {
5454
$config = Yaml::parseFile($configFilePath);
5555
} catch (ParseException $e) {
5656
throw new ConfigException("YAML parse error: {$e->getMessage()}");
5757
}
58-
58+
5959
// prevalidate config
6060
if ($config['auth']) {
6161
$this->auth = $config['auth'];
6262
} else {
6363
throw new ConfigException("Missing 'auth' section.");
6464
}
65-
65+
6666
if ($config['dataOwner']) {
6767
$this->dataOwner = $config['dataOwner'];
6868
} else {
6969
throw new ConfigException("Missing 'dataOwner' section.");
7070
}
71-
71+
7272
if ($config['mailchimp']) {
7373
$this->mailchimp = $config['mailchimp'];
7474
} else {
7575
throw new ConfigException("Missing 'mailchimp' section.");
7676
}
77-
77+
7878
if ($config['fields']) {
7979
$this->fields = $config['fields'];
8080
} else {
8181
throw new ConfigException("Missing 'fields' section.");
8282
}
83-
83+
8484
if (isset($config['webling'])) {
8585
$this->webling = $config['webling'];
8686
}
8787
}
88-
88+
8989
public static function getCrmIdKey()
9090
{
9191
return self::CRM_ID_KEY;
9292
}
93-
93+
9494
/**
9595
* Return array with crm credentials
9696
*
@@ -100,17 +100,18 @@ public static function getCrmIdKey()
100100
*/
101101
public function getCrmCredentials(): array
102102
{
103-
if (empty($this->auth['crm'])
103+
if (
104+
empty($this->auth['crm'])
104105
|| empty($this->auth['crm']['clientId'])
105106
|| empty($this->auth['crm']['clientSecret'])
106107
|| empty($this->auth['crm']['url'])
107108
) {
108109
throw new ConfigException("Missing CRM credentials.");
109110
}
110-
111+
111112
return $this->auth['crm'];
112113
}
113-
114+
114115
/**
115116
* Return array with mailchimp credentials
116117
*
@@ -120,15 +121,16 @@ public function getCrmCredentials(): array
120121
*/
121122
public function getMailchimpCredentials(): array
122123
{
123-
if (empty($this->auth['mailchimp'])
124+
if (
125+
empty($this->auth['mailchimp'])
124126
|| empty($this->auth['mailchimp']['apikey'])
125127
) {
126128
throw new ConfigException("Missing Mailchimp credentials.");
127129
}
128-
130+
129131
return $this->auth['mailchimp'];
130132
}
131-
133+
132134
/**
133135
* Return array with name and email of data owner
134136
*
@@ -138,16 +140,17 @@ public function getMailchimpCredentials(): array
138140
*/
139141
public function getDataOwner(): array
140142
{
141-
if (empty($this->dataOwner)
143+
if (
144+
empty($this->dataOwner)
142145
|| empty($this->dataOwner['email'])
143146
|| empty($this->dataOwner['name'])
144147
) {
145148
throw new ConfigException("Missing data owner details.");
146149
}
147-
150+
148151
return $this->dataOwner;
149152
}
150-
153+
151154
/**
152155
* Return bool that indicates if all records should be synced even if they dont have
153156
* relevant subscriptions
@@ -159,7 +162,7 @@ public function getSyncAll(): bool
159162
if (array_key_exists('syncAll', $this->mailchimp)) {
160163
return filter_var($this->mailchimp['syncAll'], FILTER_VALIDATE_BOOLEAN);
161164
}
162-
165+
163166
return false;
164167
}
165168

@@ -176,7 +179,38 @@ public function getIgnoreSubscribeThroughMailchimp(): bool
176179

177180
return false;
178181
}
179-
182+
183+
/**
184+
* Return array of keys that should trigger an upsert to CRM when set to 'yes'
185+
* If the configuration is an array, returns that array
186+
* Otherwise returns an empty array (no upsert)
187+
*
188+
* @return array
189+
*/
190+
public function getUpsertToCrmTriggers(): array
191+
{
192+
if (array_key_exists('upsertToCrmTriggers', $this->mailchimp)) {
193+
$config = $this->mailchimp['upsertToCrmTriggers'];
194+
195+
// If it's an array, use it directly
196+
if (is_array($config)) {
197+
return $config;
198+
}
199+
}
200+
201+
return [];
202+
}
203+
204+
/**
205+
* Return bool that indicates if upserting to CRM is enabled
206+
*
207+
* @return bool
208+
*/
209+
public function isUpsertToCrmEnabled(): bool
210+
{
211+
return !empty($this->getUpsertToCrmTriggers());
212+
}
213+
180214
/**
181215
* Return the default list id in Mailchimp
182216
*
@@ -218,14 +252,14 @@ public function getMailchimpKeyOfCrmId(): string
218252
foreach ($this->getFieldMaps() as $map) {
219253
if (self::CRM_ID_KEY === $map->getCrmKey()) {
220254
$keys = array_keys($map->getMailchimpDataArray());
221-
255+
222256
return reset($keys);
223257
}
224258
}
225-
259+
226260
throw new ConfigException('Missing "' . self::CRM_ID_KEY . '" field.');
227261
}
228-
262+
229263
/**
230264
* Return array with the field maps
231265
*
@@ -238,15 +272,15 @@ public function getFieldMaps(): array
238272
if (!is_array($this->fields)) {
239273
throw new ConfigException("Fields configuration must be an array.");
240274
}
241-
275+
242276
$fields = [];
243277
foreach ($this->fields as $config) {
244278
$fields[] = new FieldMapFacade($config);
245279
}
246-
280+
247281
return $fields;
248282
}
249-
283+
250284
/**
251285
* Return the field key in the crm that corresponds to the email field
252286
*
@@ -258,18 +292,18 @@ public function getCrmEmailKey(): string
258292
if ($this->crmEmailKey) {
259293
return $this->crmEmailKey;
260294
}
261-
295+
262296
foreach ($this->getFieldMaps() as $map) {
263297
if ($map->isEmail()) {
264298
$this->crmEmailKey = $map->getCrmKey();
265-
299+
266300
return $this->crmEmailKey;
267301
}
268302
}
269-
303+
270304
throw new ConfigException('Missing email field.');
271305
}
272-
306+
273307
/**
274308
* Return array of the Webling group ids of the prioritized groups
275309
*
@@ -279,7 +313,7 @@ public function getPrioritizedGroups(): array
279313
{
280314
return $this->webling['prioritizedGroups'] ?? [];
281315
}
282-
316+
283317
/**
284318
* Return the validation errors of this config
285319
*
@@ -290,10 +324,10 @@ public function getErrors(): array
290324
if (is_null($this->errors)) {
291325
$this->isValid();
292326
}
293-
327+
294328
return $this->errors;
295329
}
296-
330+
297331
/**
298332
* Return true, if the given config is valid
299333
*
@@ -309,7 +343,7 @@ public function isValid(): bool
309343
'getMailchimpKeyOfCrmId',
310344
'getMailchimpListId'
311345
];
312-
346+
313347
$this->errors = [];
314348
foreach ($methods as $method) {
315349
// we throw errors and catch them, because one can also change the config
@@ -321,7 +355,7 @@ public function isValid(): bool
321355
$this->errors[] = $e->getMessage();
322356
}
323357
}
324-
358+
325359
return empty($this->errors);
326360
}
327-
}
361+
}

0 commit comments

Comments
 (0)