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