|
3 | 3 | namespace Hiccup\MailChimpApi\Api; |
4 | 4 |
|
5 | 5 | use GuzzleHttp\Client; |
| 6 | +use GuzzleHttp\RequestOptions; |
| 7 | +use Hiccup\MailChimpApi\Exception\InvalidParametersException; |
| 8 | +use Hiccup\MailChimpApi\MailChimp; |
6 | 9 |
|
7 | 10 | /** |
8 | 11 | * Class Member |
@@ -48,15 +51,78 @@ public function __construct(Client $client) |
48 | 51 | /** |
49 | 52 | * @param string $listId |
50 | 53 | * @param array $parameters see MailChimp doc for list of available parameters |
| 54 | + * @return array |
| 55 | + * @throws InvalidParametersException |
| 56 | + */ |
| 57 | + public function subscribe(string $listId, array $parameters): array |
| 58 | + { |
| 59 | + if (isset($parameters['email_address']) === false) { |
| 60 | + throw new InvalidParametersException(['email_address' => 'required']); |
| 61 | + } |
| 62 | + |
| 63 | + $parameters['status_if_new'] = Member::STATUS_SUBSCRIBED; |
| 64 | + $parameters['status'] = Member::STATUS_SUBSCRIBED; |
| 65 | + $member = $this->put($listId, $parameters); |
| 66 | + |
| 67 | + return $member; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @param string $listId |
| 72 | + * @param array $parameters |
| 73 | + * @return array |
| 74 | + * @throws InvalidParametersException |
| 75 | + * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#edit-put_lists_list_id_members_subscriber_hash |
| 76 | + */ |
| 77 | + public function put(string $listId, array $parameters): array |
| 78 | + { |
| 79 | + if (isset($parameters['email_address']) === false) { |
| 80 | + throw new InvalidParametersException(['email_address' => 'required']); |
| 81 | + } |
| 82 | + |
| 83 | + $response = $this->client->put( |
| 84 | + sprintf('/%s/lists/%s/members/%s', MailChimp::VERSION, $listId, md5($parameters['email_address'])), |
| 85 | + [RequestOptions::JSON => $parameters] |
| 86 | + ); |
| 87 | + |
| 88 | + return json_decode((string) $response->getBody(), true); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @param string $listId |
| 93 | + * @param array $parameters |
| 94 | + * @return array |
| 95 | + * @throws InvalidParametersException |
| 96 | + * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#edit-patch_lists_list_id_members_subscriber_hash |
| 97 | + */ |
| 98 | + public function patch(string $listId, array $parameters): array |
| 99 | + { |
| 100 | + if (isset($parameters['email_address']) === false) { |
| 101 | + throw new InvalidParametersException(['email_address' => 'required']); |
| 102 | + } |
| 103 | + |
| 104 | + $response = $this->client->patch( |
| 105 | + sprintf('/%s/lists/%s/members/%s', MailChimp::VERSION, $listId, md5($parameters['email_address'])), |
| 106 | + [RequestOptions::JSON => $parameters] |
| 107 | + ); |
| 108 | + |
| 109 | + return json_decode((string) $response->getBody(), true); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @param string $listId |
| 114 | + * @param string $email |
51 | 115 | * |
52 | 116 | * @return array |
53 | 117 | * |
54 | | - * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#create-post_lists_list_id_members |
| 118 | + * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#read-get_lists_list_id_members_subscriber_hash |
55 | 119 | */ |
56 | | - public function create(string $listId, array $parameters): array |
| 120 | + public function get(string $listId, string $email): array |
57 | 121 | { |
58 | | - $response = $this->client->post(sprintf('/lists/%s/members', $listId), $parameters); |
| 122 | + $response = $this->client->get( |
| 123 | + sprintf('/%s/lists/%s/members/%s', MailChimp::VERSION, $listId, md5($email)) |
| 124 | + ); |
59 | 125 |
|
60 | | - return json_decode((string) $response, true); |
| 126 | + return json_decode((string) $response->getBody(), true); |
61 | 127 | } |
62 | 128 | } |
0 commit comments