-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMailingList.php
More file actions
84 lines (72 loc) · 2.37 KB
/
MailingList.php
File metadata and controls
84 lines (72 loc) · 2.37 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
<?php
declare(strict_types=1);
namespace MatchBot\Client;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Log\LoggerInterface;
use MatchBot\Application\Settings;
/**
* Client to handle mailing list signups via Salesforce
*/
class MailingList extends Common
{
use HashTrait;
public function __construct(
Settings $settings,
LoggerInterface $logger
) {
parent::__construct($settings, $logger);
}
/**
* Send a mailing list signup request to Salesforce
*
* @param string $mailingList Either 'donor' or 'charity'
* @param string $firstName First name of the person signing up
* @param string $lastName Last name of the person signing up
* @param string $emailAddress Email address of the person signing up
* @param string|null $jobTitle Job title (required for charity mailing list)
* @param string|null $organisationName Organisation name
* @return bool Whether the signup was successful
* @throws BadRequestException
* @throws BadResponseException
* @throws GuzzleException
*/
public function signup(
string $mailingList,
string $firstName,
string $lastName,
string $emailAddress,
?string $jobTitle = null,
?string $organisationName = null
): bool {
$uri = $this->sfApiBaseUrl . '/donations/services/apexrest/v2.0/mailing-list-signup/';
$payload = [
'mailingList' => $mailingList,
'firstName' => $firstName,
'lastName' => $lastName,
'emailAddress' => $emailAddress,
];
if ($jobTitle !== null) {
$payload['jobTitle'] = $jobTitle;
}
if ($organisationName !== null) {
$payload['organisationName'] = $organisationName;
}
try {
$response = $this->getHttpClient()->post(
$uri,
[
'json' => $payload,
'headers' => $this->getVerifyHeaders(json_encode($payload, \JSON_THROW_ON_ERROR)),
]
);
return $response->getStatusCode() === 200;
} catch (GuzzleException $ex) {
$this->logger->error(sprintf(
'Mailing list signup exception: %s: %s',
get_class($ex),
$ex->getMessage()
));
throw $ex;
}
}
}