Skip to content

Commit e54bc72

Browse files
Fix webling sync
1 parent feff2d2 commit e54bc72

10 files changed

Lines changed: 386 additions & 337 deletions

app/Http/CrmClient.php

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class CrmClient
1616
private $clientSecret;
1717
private $token;
1818
private $guzzle;
19-
19+
2020
/**
2121
* CrmClient constructor
2222
*
@@ -29,10 +29,10 @@ public function __construct(int $clientId, string $clientSecret, string $apiUrl)
2929
$this->clientId = $clientId;
3030
$this->clientSecret = $clientSecret;
3131
$this->guzzle = new Client(['base_uri' => $this->forceSSL($apiUrl)]);
32-
32+
3333
$this->loadToken();
3434
}
35-
35+
3636
/**
3737
* Make sure the given url is always using httpS
3838
*
@@ -44,28 +44,28 @@ private function forceSSL(string $url)
4444
{
4545
$url = preg_replace('/^\/\//', 'https://', $url);
4646
$url = preg_replace('/^http:\/\//', 'https://', $url);
47-
47+
4848
return $url;
4949
}
50-
50+
5151
/**
5252
* Get the oauth token of this client
5353
*/
5454
private function loadToken()
5555
{
5656
$client = OAuthClient::find($this->clientId);
57-
57+
5858
if (!$client) {
5959
$client = $this->addClient();
6060
}
61-
61+
6262
$this->token = $client->token;
63-
63+
6464
if (!$this->isTokenValid()) {
6565
$this->refreshToken($client);
6666
}
6767
}
68-
68+
6969
/**
7070
* Add new client
7171
*
@@ -74,14 +74,14 @@ private function loadToken()
7474
private function addClient(): OAuthClient
7575
{
7676
$client = new OAuthClient();
77-
77+
7878
$client->client_id = $this->clientId;
7979
$client->client_secret = $this->clientSecret;
8080
$client->save();
81-
81+
8282
return $client;
8383
}
84-
84+
8585
/**
8686
* Test the current token against the api to chekc if it is (still) valid
8787
*
@@ -93,13 +93,13 @@ private function isTokenValid(): bool
9393
{
9494
try {
9595
$this->get('/api/v1/auth');
96-
96+
9797
return true;
9898
} catch (ClientException $e) {
9999
return false;
100100
}
101101
}
102-
102+
103103
/**
104104
* Get
105105
*
@@ -115,10 +115,10 @@ public function get(string $relativeUrl)
115115
'Authorization' => 'Bearer ' . $this->token,
116116
'Accept' => 'application/json',
117117
];
118-
118+
119119
return $this->guzzle->get($relativeUrl, ['headers' => $headers]);
120120
}
121-
121+
122122
/**
123123
* Get a fresh access token
124124
*
@@ -128,7 +128,9 @@ public function get(string $relativeUrl)
128128
*/
129129
private function refreshToken(OAuthClient $client)
130130
{
131-
$res = $this->guzzle->post('/oauth/token', [
131+
$res = $this->guzzle->post(
132+
'/oauth/token',
133+
[
132134
'form_params' => [
133135
'grant_type' => 'client_credentials',
134136
'client_id' => $this->clientId,
@@ -137,14 +139,14 @@ private function refreshToken(OAuthClient $client)
137139
]
138140
]
139141
);
140-
142+
141143
$body = json_decode((string)$res->getBody());
142144
$this->token = $body->access_token;
143-
145+
144146
$client->token = $this->token;
145147
$client->save();
146148
}
147-
149+
148150
/**
149151
* Upsert
150152
*
@@ -158,11 +160,15 @@ private function refreshToken(OAuthClient $client)
158160
public function post(string $relativeUrl, array $data)
159161
{
160162
return $this->guzzle->post($relativeUrl, [
161-
'headers' => ['Authorization' => 'Bearer ' . $this->token],
162-
'body' => json_encode($data)
163+
'headers' => [
164+
'Authorization' => 'Bearer ' . $this->token,
165+
'Accept' => 'application/json',
166+
],
167+
'json' => $data,
168+
'allow_redirects' => false,
163169
]);
164170
}
165-
171+
166172
/**
167173
* Update
168174
*
@@ -180,4 +186,4 @@ public function put(string $relativeUrl, array $data)
180186
'body' => json_encode($data)
181187
]);
182188
}
183-
}
189+
}

app/Http/MailChimpClient.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -431,18 +431,12 @@ public function removeTagFromSubscriber(string $subscriberId, string $tagName)
431431
* @return array|false
432432
* @throws MailchimpClientException
433433
*/
434-
public function updateSubscriberInterests(string $subscriberId, array $mergeFields, string $interestId)
434+
public function updateMergeFields(string $subscriberId, array $mergeFields)
435435
{
436436
$data = [
437437
'merge_fields' => $mergeFields,
438438
];
439439

440-
if (!empty($interestId)) {
441-
$data['interests'] = [
442-
$interestId => true
443-
];
444-
}
445-
446440
$patch = $this->client->patch("lists/{$this->listId}/members/$subscriberId", $data, self::API_WRITE_TIMEOUT);
447441

448442
$this->validateResponseStatus('PATCH subscriber interests', $patch);

app/Synchronizer/Config.php

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,17 @@ public function getLanguageTagsFromConfig(): array
338338
$fieldMap->canSyncToMailchimp() &&
339339
$fieldMap->getCrmKey() === 'language'
340340
) {
341-
$mailchimpData = $fieldMap->getMailchimpDataArray();
342-
foreach ($mailchimpData as $tagName => $value) {
341+
$reflection = new \ReflectionObject($fieldMap);
342+
$property = $reflection->getProperty('field');
343+
$property->setAccessible(true);
344+
$field = $property->getValue($fieldMap);
345+
346+
$tagReflection = new \ReflectionObject($field);
347+
$tagNameProperty = $tagReflection->getProperty('mailchimpTagName');
348+
$tagNameProperty->setAccessible(true);
349+
$tagName = $tagNameProperty->getValue($field);
350+
351+
if (!empty($tagName)) {
343352
$languageTags[] = $tagName;
344353
}
345354
}
@@ -392,34 +401,36 @@ public function getInterestsToSync(): array
392401
}
393402

394403
/**
395-
* Return configurable field that is used to determine if a member should be synced
396-
*
397-
* @return string
404+
* Return the number of months to consider for the changed within filter
398405
*
399-
* @throws ConfigException
406+
* @return int
400407
*/
401-
public function getSyncCriteriaField(): string
408+
public function getChangedWithinMonths(): int
402409
{
403-
if (empty($this->mailchimpToCrm['syncCriteriaField'])) {
404-
throw new ConfigException('Missing "syncCriteriaField" field.');
405-
}
406-
407-
return $this->mailchimpToCrm['syncCriteriaField'];
410+
return $this->mailchimpToCrm['changedWithinMonths'] ?? 6;
408411
}
409412

410413
/**
411-
* Return configurable threshold used together with syncCriteriaField.
414+
* Return the number of months to consider for the opt-in older than filter
412415
*
413416
* @return int
417+
*/
418+
public function getOptInOlderThanMonths(): int
419+
{
420+
return $this->mailchimpToCrm['optInOlderThanMonths'] ?? 2;
421+
}
422+
423+
/**
424+
* Return the group where new people should be added in the crm
414425
*
415-
* @throws ConfigException
426+
* @return int
416427
*/
417-
public function getSyncCriteriaThreshold(): int
428+
public function getGroupForNewMembers(): int
418429
{
419-
if (empty($this->mailchimpToCrm['syncCriteriaThreshold'])) {
420-
throw new ConfigException('Missing "syncCriteriaThreshold" field.');
430+
if (empty($this->mailchimpToCrm['groupForNewMembers'])) {
431+
throw new ConfigException('Missing "groupForNewMembers" field.');
421432
}
422433

423-
return $this->mailchimpToCrm['syncCriteriaThreshold'];
434+
return $this->mailchimpToCrm['groupForNewMembers'];
424435
}
425436
}

0 commit comments

Comments
 (0)