diff --git a/src/Form/CloudFlareCacheClearForm.php b/src/Form/CloudFlareCacheClearForm.php index fed5fb1..5cc1479 100644 --- a/src/Form/CloudFlareCacheClearForm.php +++ b/src/Form/CloudFlareCacheClearForm.php @@ -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; @@ -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(); } /** @@ -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); } /**