Skip to content

Commit

Permalink
Revised CloudFlareCacheClearForm.php to use CloudFlare service contai…
Browse files Browse the repository at this point in the history
…ner.
  • Loading branch information
aweingarten committed Sep 29, 2015
1 parent c5ef0a8 commit 17bac23
Showing 1 changed file with 4 additions and 62 deletions.
66 changes: 4 additions & 62 deletions src/Form/CloudFlareCacheClearForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {

$form['cache_clearing']['clear_zone_button'] = [
'#type' => 'submit',
'#value' => $this->t('Clear Entire Cachee'),
'#value' => $this->t('Clear Entire Cache'),
'#submit' => ['::purgeEntireZone'],
];
return $form;
Expand Down Expand Up @@ -102,37 +102,7 @@ public function validatePathClear(array &$form, FormStateInterface $form_state)
* The current state of the form.
*/
public function purgeEntireZone(array &$form, FormStateInterface $form_state) {
$config = \Drupal::config('cloudflare.settings');
$api_key = $config->get('apikey');
$email = $config->get('email');
$zone = $config->get('zone');

try {
$this->zoneApi = new ZoneApi($api_key, $email);

// @todo rethink how to handle cloudflare zones in Drupal.
if (is_null($zone)) {
$zones = $this->zoneApi->listZones();
$zone = $zones[0]->getZoneId();
}

$this->zoneApi->purgeAllFiles($zone);
}

catch (CloudFlareHttpException $e) {
drupal_set_message("Unable to clear zone cache. " . $e->getMessage(), 'error');
\Drupal::logger('cloudflare')->error($e->getMessage());
return;
}

catch (CloudFlareApiException $e) {
drupal_set_message("Unable to clear zone cache. " . $e->getMessage(), 'error');
\Drupal::logger('cloudflare')->error($e->getMessage());
return;
}

// If no exceptions have been thrown then the request has been successful.
drupal_set_message("The zone: $zone was successfully cleared.");
\Drupal::service('cloudflare')->invalidateZone();
}

/**
Expand All @@ -144,36 +114,8 @@ public function purgeEntireZone(array &$form, FormStateInterface $form_state) {
* The current state of the form.
*/
public function purgePaths(array &$form, FormStateInterface $form_state) {
$config = $this->config('cloudflare.settings');
$api_key = $config->get('apikey');
$email = $config->get('email');
$zone = $config->get('zone');

try {
$zone_api = new ZoneApi($api_key, $email);

// @todo rethink how to handle cloudflare zones in Drupal.
if (is_null($zone)) {
$zones = $zone_api->listZones();
$zone = $zones[0]->getZoneId();
}
$zone_api->purgeAllFiles($zone);
}

catch (CloudFlareHttpException $e) {
drupal_set_message("Unable to clear paths. " . $e->getMessage(), 'error');
\Drupal::logger('cloudflare')->error($e->getMessage());
return;
}

catch (CloudFlareApiException $e) {
drupal_set_message("Unable to clear paths. " . $e->getMessage(), 'error');
\Drupal::logger('cloudflare')->error($e->getMessage());
return;
}

// If no exceptions have been thrown then the request has been successful.
drupal_set_message("The zone: $zone was successfully cleared.");
$paths = $form_state->getValue('paths');
\Drupal::service('cloudflare')->invalidateByPath($paths);
}

/**
Expand Down

0 comments on commit 17bac23

Please sign in to comment.