| Conditions | 17 |
| Paths | 543 |
| Total Lines | 116 |
| Code Lines | 65 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 82 | public function configure_inbound_emails(HTTPRequest $req) |
||
| 83 | { |
||
| 84 | if (!Director::isDev() && !Permission::check('ADMIN')) { |
||
| 85 | return 'You must be in dev mode or be logged as an admin'; |
||
| 86 | } |
||
| 87 | |||
| 88 | $clearExisting = $req->getVar('clear_existing'); |
||
| 89 | $clearWebhooks = $req->getVar('clear_webhooks'); |
||
| 90 | $clearInbound = $req->getVar('clear_inbound'); |
||
| 91 | if ($clearExisting) { |
||
| 92 | echo '<strong>Existing inbounddomains and relay webhooks will be cleared</strong><br/>'; |
||
| 93 | } else { |
||
| 94 | echo 'You can clear existing inbound domains and relay webhooks by passing ?clear_existing=1&clear_webhooks=1&clear_inbound=1<br/>'; |
||
| 95 | } |
||
| 96 | |||
| 97 | $client = SparkPostHelper::getMasterClient(); |
||
| 98 | |||
| 99 | $inbound_domain = Environment::getEnv('SPARKPOST_INBOUND_DOMAIN'); |
||
| 100 | if (!$inbound_domain) { |
||
| 101 | die('You must define a key SPARKPOST_INBOUND_DOMAIN'); |
||
| 102 | } |
||
| 103 | |||
| 104 | /* |
||
| 105 | * "name": "Replies Webhook", |
||
| 106 | * "target": "https://webhooks.customer.example/replies", |
||
| 107 | * "auth_token": "5ebe2294ecd0e0f08eab7690d2a6ee69", |
||
| 108 | * "match": { |
||
| 109 | * "protocol": "SMTP", |
||
| 110 | * "domain": "email.example.com" |
||
| 111 | */ |
||
| 112 | |||
| 113 | $listWebhooks = $client->listRelayWebhooks(); |
||
| 114 | $listInboundDomains = $client->listInboundDomains(); |
||
| 115 | |||
| 116 | if ($clearExisting) { |
||
| 117 | // we need to delete relay webhooks first! |
||
| 118 | if ($clearWebhooks) { |
||
| 119 | foreach ($listWebhooks as $wh) { |
||
| 120 | $client->deleteRelayWebhook($wh['id']); |
||
| 121 | echo 'Delete relay webhook ' . $wh['id'] . '<br/>'; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | if ($clearInbound) { |
||
| 125 | foreach ($listInboundDomains as $id) { |
||
| 126 | $client->deleteInboundDomain($id['domain']); |
||
| 127 | echo 'Delete domain ' . $id['domain'] . '<br/>'; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | $listWebhooks = $client->listRelayWebhooks(); |
||
| 132 | $listInboundDomains = $client->listInboundDomains(); |
||
| 133 | } |
||
| 134 | |||
| 135 | echo '<pre>' . __FILE__ . ':' . __LINE__ . '<br/>'; |
||
| 136 | echo 'List Inbounds Domains:<br/>'; |
||
| 137 | print_r($listInboundDomains); |
||
| 138 | echo '</pre>'; |
||
| 139 | |||
| 140 | $found = false; |
||
| 141 | |||
| 142 | foreach ($listInboundDomains as $id) { |
||
| 143 | if ($id['domain'] == $inbound_domain) { |
||
| 144 | $found = true; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | if (!$found) { |
||
| 149 | echo "Domain is not found, we create it<br/>"; |
||
| 150 | |||
| 151 | // This is the domain that users will send email to. |
||
| 152 | $result = $client->createInboundDomain($inbound_domain); |
||
| 153 | |||
| 154 | echo '<pre>' . __FILE__ . ':' . __LINE__ . '<br/>'; |
||
| 155 | echo 'Create Inbound Domain:<br/>'; |
||
| 156 | print_r($result); |
||
| 157 | echo '</pre>'; |
||
| 158 | } else { |
||
| 159 | echo "Domain is already configured<br/>"; |
||
| 160 | } |
||
| 161 | |||
| 162 | // Now that you have your InboundDomain set up, you can create your Relay Webhook by sending a POST request to |
||
| 163 | // https://api.sparkpost.com/api/v1/relay-webhooks. This step links your consumer with the Inbound Domain. |
||
| 164 | |||
| 165 | echo '<pre>' . __FILE__ . ':' . __LINE__ . '<br/>'; |
||
| 166 | echo 'List Webhooks:<br/>'; |
||
| 167 | print_r($listWebhooks); |
||
| 168 | echo '</pre>'; |
||
| 169 | |||
| 170 | $found = false; |
||
| 171 | |||
| 172 | foreach ($listWebhooks as $wh) { |
||
| 173 | if ($wh['match']['domain'] == $inbound_domain) { |
||
| 174 | $found = true; |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | if (!$found) { |
||
| 179 | // The match.domain property should be the same as the Inbound Domain you set up in the previous step |
||
| 180 | $webhookResult = $client->createRelayWebhook([ |
||
| 181 | 'name' => 'Inbound Webhook', |
||
| 182 | 'target' => Director::absoluteURL('sparkpost/incoming'), |
||
| 183 | 'match' => [ |
||
| 184 | 'domain' => $inbound_domain |
||
| 185 | ] |
||
| 186 | ]); |
||
| 187 | |||
| 188 | echo '<pre>' . __FILE__ . ':' . __LINE__ . '<br/>'; |
||
| 189 | echo 'Webhook result:<br/>'; |
||
| 190 | print_r($webhookResult); |
||
| 191 | echo '</pre>'; |
||
| 192 | |||
| 193 | if ($webhookResult['id']) { |
||
| 194 | echo "New webhook created with id " . $webhookResult['id']; |
||
| 195 | } |
||
| 196 | } else { |
||
| 197 | echo "Webhook already configured"; |
||
| 198 | } |
||
| 355 |