diff --git a/src/AkamaiAuthentication.php b/src/AkamaiAuthentication.php index f2c1723..2ee57c4 100644 --- a/src/AkamaiAuthentication.php +++ b/src/AkamaiAuthentication.php @@ -5,7 +5,6 @@ * Contains \Drupal\akamai\AkamaiAuthentication. */ - namespace Drupal\akamai; use Drupal\Core\Config\Config; diff --git a/src/AkamaiClient.php b/src/AkamaiClient.php index b462a2f..2f46d17 100644 --- a/src/AkamaiClient.php +++ b/src/AkamaiClient.php @@ -139,7 +139,7 @@ class AkamaiClient extends Client { */ public function isAuthorized() { try { - $response = $this->_getQueue(); + $response = $this->doGetQueue(); } catch (\GuzzleHttp\Exception\ClientException $e) { // @todo better handling @@ -195,15 +195,17 @@ class AkamaiClient extends Client { ); // Note that the response has useful data that we need to record. // Example response body: + // @code // { - // "estimatedSeconds": 420, - // "progressUri": "/ccu/v2/purges/57799d8b-10e4-11e4-9088-62ece60caaf0", - // "purgeId": "57799d8b-10e4-11e4-9088-62ece60caaf0", - // "supportId": "17PY1405953363409286-284546144", - // "httpStatus": 201, - // "detail": "Request accepted.", - // "pingAfterSeconds": 420 - // }. + // "estimatedSeconds": 420, + // "progressUri": "/ccu/v2/purges/57799d8b-10e4-11e4-9088-62ece60caaf0", + // "purgeId": "57799d8b-10e4-11e4-9088-62ece60caaf0", + // "supportId": "17PY1405953363409286-284546144", + // "httpStatus": 201, + // "detail": "Request accepted.", + // "pingAfterSeconds": 420 + // } + // @endcode return $response; } catch (ClientException $e) { @@ -260,7 +262,7 @@ class AkamaiClient extends Client { * @link https://developer.akamai.com/api/purge/ccu/reference.html */ public function getQueue($queue_name = 'default') { - return Json::decode($this->_getQueue($queue_name)->getBody()); + return Json::decode($this->doGetQueue($queue_name)->getBody()); } /** @@ -273,8 +275,9 @@ class AkamaiClient extends Client { * The queue name to check. Defaults to 'default'. * * @return \Psr\Http\Message\ResponseInterface + * The HTTP response. */ - private function _getQueue($queue_name = 'default') { + private function doGetQueue($queue_name = 'default') { return $this->get("/ccu/v2/queues/{$queue_name}"); } @@ -310,10 +313,22 @@ class AkamaiClient extends Client { } } + /** + * Sets the queue name to clear. + * + * @param string $queue + * The queue name to clear. + */ public function setQueue($queue) { $this->queue = $queue; } + /** + * Set the action to perform on the queue. + * + * @param string $action + * The action to perform on the queue. + */ public function setAction($action) { $valid_actions = array('remove', 'invalidate'); if (in_array($action, $valid_actions)) { @@ -324,6 +339,12 @@ class AkamaiClient extends Client { } } + /** + * Sets the type of purge. + * + * @param string $type + * The type of purge, either 'arl' or 'cpcode'. + */ public function setType($type) { $valid_types = array('cpcode', 'arl'); if (in_array($type, $valid_types)) { @@ -334,6 +355,12 @@ class AkamaiClient extends Client { } } + /** + * Sets the domain to clear. + * + * @param string $domain + * The domain to clear, either 'production' or 'staging'. + */ public function setDomain($domain) { $valid_domains = array('staging', 'production'); if (in_array($domain, $valid_domains)) { @@ -344,6 +371,15 @@ class AkamaiClient extends Client { } } + /** + * Formats a JSON error response into a string. + * + * @param \GuzzleHttp\Exception\ClientException $e + * The ClientException containing the JSON error response. + * + * @return string + * The formatted error message as a string. + */ protected function formatExceptionMessage(ClientException $e) { // Get the full response to avoid truncation. // @see https://laracasts.com/discuss/channels/general-discussion/guzzle-error-message-gets-truncated diff --git a/src/Form/CacheControlForm.php b/src/Form/CacheControlForm.php index 4309f87..6244cca 100644 --- a/src/Form/CacheControlForm.php +++ b/src/Form/CacheControlForm.php @@ -30,11 +30,11 @@ class CacheControlForm extends FormBase { /** * Constructs a new CacheControlForm. * - * @param \Drupal\akamai\AkamaiClient + * @param \Drupal\akamai\AkamaiClient $akamai_client * The akamai client. */ - public function __construct(AkamaiClient $akamaiClient) { - $this->akamaiClient = $akamaiClient; + public function __construct(AkamaiClient $akamai_client) { + $this->akamaiClient = $akamai_client; } /** @@ -149,7 +149,12 @@ class CacheControlForm extends FormBase { } if (!empty($invalid_paths)) { $paths = implode(",", $invalid_paths); - $message = $paths . \Drupal::translation()->formatPlural(count($invalid_paths), ' path is invalid and does not exist', ' paths are invalid and do not exist') . $this->t(' on the site. Please provide valid URLs for purging.'); + $message = $this->formatPlural( + count($invalid_paths), + '@paths path is invalid and does not exist on the site. Please provide valid URLs for purging.', + '@paths paths are invalid and do not exist on the site. Please provide valid URLs for purging.', + ['@paths' => $paths] + ); $form_state->setErrorByName('paths', $message); } } @@ -167,7 +172,9 @@ class CacheControlForm extends FormBase { $this->akamaiClient->setDomain($form_state->getValue('domain_override')); $response = $this->akamaiClient->purgeUrls($urls_to_clear); if ($response) { - drupal_set_message($this->t('Requested :action of the following URLs: :urls', [':action' => $action, ':urls' => implode(', ', $urls_to_clear)])); + drupal_set_message($this->t('Requested :action of the following URLs: :urls', + [':action' => $action, ':urls' => implode(', ', $urls_to_clear)]) + ); } else { drupal_set_message($this->t('There was an error clearing the cache. Check logs for further detail.'), 'error'); @@ -178,10 +185,7 @@ class CacheControlForm extends FormBase { * Shows a message to the user if not authenticated to the Akamai API. */ protected function showAuthenticationWarning() { - $url = Url::fromRoute('akamai.settings'); - $link_text = $this->t('Update settings now'); - $message = 'You are not authenticated to Akamai CCU v2. Until you authenticate, you will not be able to clear URLs from the Akamai cache. @link'; - $message = $this->t($message, ['@link' => $this->l($link_text, $url)]); + $message = $this->t('You are not authenticated to Akamai CCU v2. Until you authenticate, you will not be able to clear URLs from the Akamai cache. Update settings now.', [':url' => Url::fromRoute('akamai.settings')]); drupal_set_message($message, 'warning'); } diff --git a/src/Form/ConfigForm.php b/src/Form/ConfigForm.php index a59c30b..e04812d 100644 --- a/src/Form/ConfigForm.php +++ b/src/Form/ConfigForm.php @@ -37,21 +37,8 @@ class ConfigForm extends ConfigFormBase { public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('akamai.settings'); - $form = array(); - - // This doesn't do anything at present. Commenting out. // @todo decide whether we want a global killswitch here. -// $form['disable_fieldset'] = array( -// '#type' => 'fieldset', -// '#title' => $this->t('Disable Akamai Cache Clearing'), -// '#description' => $this->t('Set this field to disable cache clearing during imports, migrations, or other batch processes.'), -// ); -// -// $form['disable_fieldset']['disabled'] = array( -// '#type' => 'checkbox', -// '#title' => $this->t('Disable cache clearing'), -// '#default_value' => $config->get('disable'), -// ); + $form = array(); // Link to instructions on how to get Akamai credentials from Luna. $luna_url = 'https://developer.akamai.com/introduction/Prov_Creds.html'; @@ -166,7 +153,6 @@ class ConfigForm extends ConfigFormBase { $values = $form_state->getValues(); $this->config('akamai.settings') - //->set('disabled', $values['disabled']) ->set('rest_api_url', $values['rest_api_url']) ->set('client_token', $values['client_token']) ->set('client_secret', $values['client_secret']) diff --git a/src/Tests/AkamaiHomepageTest.php b/src/Tests/AkamaiHomepageTest.php index 33cde04..28c106f 100644 --- a/src/Tests/AkamaiHomepageTest.php +++ b/src/Tests/AkamaiHomepageTest.php @@ -67,8 +67,6 @@ class AkamaiHomepageTest extends WebTestBase { \Drupal::service('theme_handler')->install(['bartik']); $theme_settings = $this->config('system.theme'); foreach (['bartik'] as $theme) { - $this->drupalGet('admin/structure/block/list/' . $theme); - $this->assertTitle(t('Block layout') . ' | Drupal'); // Configure and save the block. $this->drupalPlaceBlock('akamai_cache_clear_block', array( 'region' => 'content',