Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 7 | class Newsletter |
||
| 8 | { |
||
| 9 | /** @var \DrewM\MailChimp\MailChimp */ |
||
| 10 | protected $mailChimp; |
||
| 11 | |||
| 12 | /** * @var \Spatie\Newsletter\NewsletterListCollection */ |
||
| 13 | protected $lists; |
||
| 14 | |||
| 15 | public function __construct(MailChimp $mailChimp, NewsletterListCollection $lists) |
||
| 16 | { |
||
| 17 | $this->mailChimp = $mailChimp; |
||
| 18 | |||
| 19 | $this->lists = $lists; |
||
| 20 | } |
||
| 21 | |||
| 22 | public function subscribe(string $email, array $mergeFields = [], string $listName = '', array $options = []) |
||
| 23 | { |
||
| 24 | $list = $this->lists->findByName($listName); |
||
| 25 | |||
| 26 | $options = $this->getSubscriptionOptions($email, $mergeFields, $options); |
||
| 27 | |||
| 28 | $response = $this->mailChimp->post("lists/{$list->getId()}/members", $options); |
||
| 29 | |||
| 30 | if (! $this->lastActionSucceeded()) { |
||
| 31 | return false; |
||
| 32 | } |
||
| 33 | |||
| 34 | return $response; |
||
| 35 | } |
||
| 36 | |||
| 37 | public function subscribePending(string $email, array $mergeFields = [], string $listName = '', array $options = []) |
||
| 38 | { |
||
| 39 | $options = array_merge($options, ['status' => 'pending']); |
||
| 40 | |||
| 41 | return $this->subscribe($email, $mergeFields, $listName, ); |
||
|
|
|||
| 42 | } |
||
| 43 | |||
| 44 | public function subscribeOrUpdate(string $email, array $mergeFields = [], string $listName = '', array $options = []) |
||
| 45 | { |
||
| 46 | $list = $this->lists->findByName($listName); |
||
| 47 | |||
| 48 | $options = $this->getSubscriptionOptions($email, $mergeFields, $options, $options); |
||
| 49 | |||
| 50 | $response = $this->mailChimp->put("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}", $options); |
||
| 51 | |||
| 52 | if (! $this->lastActionSucceeded()) { |
||
| 53 | return false; |
||
| 54 | } |
||
| 55 | |||
| 56 | return $response; |
||
| 57 | } |
||
| 58 | |||
| 59 | public function getMembers(string $listName = '', array $parameters = []) |
||
| 60 | { |
||
| 61 | $list = $this->lists->findByName($listName); |
||
| 62 | |||
| 63 | return $this->mailChimp->get("lists/{$list->getId()}/members", $parameters); |
||
| 64 | } |
||
| 65 | |||
| 66 | public function getMember(string $email, string $listName = '') |
||
| 67 | { |
||
| 68 | $list = $this->lists->findByName($listName); |
||
| 69 | |||
| 70 | return $this->mailChimp->get("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}"); |
||
| 71 | } |
||
| 72 | |||
| 73 | public function getMemberActivity(string $email, string $listName = '') |
||
| 74 | { |
||
| 75 | $list = $this->lists->findByName($listName); |
||
| 76 | |||
| 77 | return $this->mailChimp->get("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}/activity"); |
||
| 78 | } |
||
| 79 | |||
| 80 | public function hasMember(string $email, string $listName = ''): bool |
||
| 81 | { |
||
| 82 | $response = $this->getMember($email, $listName); |
||
| 83 | |||
| 84 | if (! isset($response['email_address'])) { |
||
| 85 | return false; |
||
| 86 | } |
||
| 87 | |||
| 88 | if (strtolower($response['email_address']) != strtolower($email)) { |
||
| 89 | return false; |
||
| 90 | } |
||
| 91 | |||
| 92 | return true; |
||
| 93 | } |
||
| 94 | |||
| 95 | public function isSubscribed(string $email, string $listName = ''): bool |
||
| 96 | { |
||
| 97 | $response = $this->getMember($email, $listName); |
||
| 98 | |||
| 99 | if (! isset($response)) { |
||
| 100 | return false; |
||
| 101 | } |
||
| 102 | |||
| 103 | if ($response['status'] != 'subscribed') { |
||
| 104 | return false; |
||
| 105 | } |
||
| 106 | |||
| 107 | return true; |
||
| 108 | } |
||
| 109 | |||
| 110 | public function unsubscribe(string $email, string $listName = '') |
||
| 111 | { |
||
| 112 | $list = $this->lists->findByName($listName); |
||
| 113 | |||
| 114 | $response = $this->mailChimp->patch("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}", [ |
||
| 115 | 'status' => 'unsubscribed', |
||
| 116 | ]); |
||
| 117 | |||
| 118 | return $response; |
||
| 119 | } |
||
| 120 | |||
| 121 | public function updateEmailAddress(string $currentEmailAddress, string $newEmailAddress, string $listName = '') |
||
| 122 | { |
||
| 123 | $list = $this->lists->findByName($listName); |
||
| 124 | |||
| 125 | $response = $this->mailChimp->patch("lists/{$list->getId()}/members/{$this->getSubscriberHash($currentEmailAddress)}", [ |
||
| 126 | 'email_address' => $newEmailAddress, |
||
| 127 | ]); |
||
| 128 | |||
| 129 | return $response; |
||
| 130 | } |
||
| 131 | |||
| 132 | public function delete(string $email, string $listName = '') |
||
| 133 | { |
||
| 134 | $list = $this->lists->findByName($listName); |
||
| 135 | |||
| 136 | $response = $this->mailChimp->delete("lists/{$list->getId()}/members/{$this->getSubscriberHash($email)}"); |
||
| 137 | |||
| 138 | return $response; |
||
| 139 | } |
||
| 140 | |||
| 141 | public function createCampaign( |
||
| 142 | string $fromName, |
||
| 143 | string $replyTo, |
||
| 144 | string $subject, |
||
| 145 | string $html = '', |
||
| 146 | string $listName = '', |
||
| 147 | array $options = [], |
||
| 148 | array $contentOptions = []) |
||
| 149 | { |
||
| 150 | $list = $this->lists->findByName($listName); |
||
| 151 | |||
| 152 | $defaultOptions = [ |
||
| 153 | 'type' => 'regular', |
||
| 154 | 'recipients' => [ |
||
| 155 | 'list_id' => $list->getId(), |
||
| 156 | ], |
||
| 157 | 'settings' => [ |
||
| 158 | 'subject_line' => $subject, |
||
| 159 | 'from_name' => $fromName, |
||
| 160 | 'reply_to' => $replyTo, |
||
| 161 | ], |
||
| 162 | ]; |
||
| 163 | |||
| 164 | $options = array_merge($defaultOptions, $options); |
||
| 165 | |||
| 166 | $response = $this->mailChimp->post('campaigns', $options); |
||
| 167 | |||
| 168 | if (! $this->lastActionSucceeded()) { |
||
| 169 | return false; |
||
| 170 | } |
||
| 171 | |||
| 172 | if ($html === '') { |
||
| 173 | return $response; |
||
| 174 | } |
||
| 175 | |||
| 176 | if (! $this->updateContent($response['id'], $html, $contentOptions)) { |
||
| 177 | return false; |
||
| 178 | } |
||
| 179 | |||
| 180 | return $response; |
||
| 181 | } |
||
| 182 | |||
| 183 | public function updateContent(string $campaignId, string $html, array $options = []) |
||
| 184 | { |
||
| 185 | $defaultOptions = compact('html'); |
||
| 186 | |||
| 187 | $options = array_merge($defaultOptions, $options); |
||
| 188 | |||
| 189 | $response = $this->mailChimp->put("campaigns/{$campaignId}/content", $options); |
||
| 190 | |||
| 191 | if (! $this->lastActionSucceeded()) { |
||
| 192 | return false; |
||
| 193 | } |
||
| 194 | |||
| 195 | return $response; |
||
| 196 | } |
||
| 197 | |||
| 198 | public function getApi(): MailChimp |
||
| 199 | { |
||
| 200 | return $this->mailChimp; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return array|false |
||
| 205 | */ |
||
| 206 | public function getLastError() |
||
| 207 | { |
||
| 208 | return $this->mailChimp->getLastError(); |
||
| 209 | } |
||
| 210 | |||
| 211 | public function lastActionSucceeded(): bool |
||
| 212 | { |
||
| 213 | return $this->mailChimp->success(); |
||
| 214 | } |
||
| 215 | |||
| 216 | protected function getSubscriberHash(string $email): string |
||
| 217 | { |
||
| 218 | return $this->mailChimp->subscriberHash($email); |
||
| 219 | } |
||
| 220 | |||
| 221 | protected function getSubscriptionOptions(string $email, array $mergeFields, array $options): array |
||
| 222 | { |
||
| 223 | $defaultOptions = [ |
||
| 224 | 'email_address' => $email, |
||
| 225 | 'status' => 'subscribed', |
||
| 226 | 'email_type' => 'html', |
||
| 227 | ]; |
||
| 228 | |||
| 229 | if (count($mergeFields)) { |
||
| 230 | $defaultOptions['merge_fields'] = $mergeFields; |
||
| 231 | } |
||
| 238 |