View source
<?php
namespace Drupal\akamai\Plugin\Client;
use Drupal\Core\Form\FormStateInterface;
use Drupal\akamai\AkamaiClientBase;
use GuzzleHttp\Exception\RequestException;
class AkamaiClientV3 extends AkamaiClientBase {
protected $apiBaseUrl = '/ccu/v3/';
protected $action = 'delete';
protected $type = 'url';
protected $usesQueue = FALSE;
protected function purgeRequest(array $objects) {
try {
$response = $this->client
->request('POST', "{$this->apiBaseUrl}{$this->action}/{$this->type}/{$this->domain}", [
'json' => $this
->createPurgeBody($objects),
]);
return $response;
} catch (RequestException $e) {
$this->logger
->error($this
->formatExceptionMessage($e));
return FALSE;
}
}
public function createPurgeBody(array $objects) {
$body = [
'objects' => $objects,
];
if ($this->type == 'url') {
$purge_urls_with_hostname = $this->configFactory
->get('akamai.settings')
->get('purge_urls_with_hostname');
if ($purge_urls_with_hostname) {
$body['hostname'] = $this->baseUrl;
}
}
return (object) $body;
}
public function validActions() {
return [
'delete',
'invalidate',
];
}
public function setType($type) {
$valid_types = [
'cpcode',
'tag',
'url',
];
if (in_array($type, $valid_types)) {
$this->type = $type;
}
else {
throw new \InvalidArgumentException('Type must be one of: ' . implode(', ', $valid_types));
}
return $this;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$config = $this->configFactory
->get('akamai.settings');
$default_action = key(array_filter($config
->get('action_v3')));
$form['action'] = [
'#type' => 'select',
'#title' => $this
->t('Clearing Action Type Default'),
'#default_value' => in_array($default_action, $this
->validActions()) ? $default_action : 'delete',
'#options' => [
'delete' => $this
->t('Delete'),
'invalidate' => $this
->t('Invalidate'),
],
'#description' => $this
->t('The default clearing action. The options are <em>delete</em> (which deletes the item from the Akamai cache) and <em>invalidate</em> (which leaves the item in the cache, but invalidates it so that the origin will be hit on the next request).'),
'#required' => TRUE,
];
$form['purge_urls_with_hostname'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Purge URLs With Common Hostname'),
'#default_value' => $config
->get('purge_urls_with_hostname'),
'#description' => $this
->t('Sends Base Path as "hostname" Fast Purge API request data member when purging URLs'),
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$actions = array_fill_keys($this
->validActions(), FALSE);
$actions[$form_state
->getValue([
'v3',
'action',
])] = TRUE;
$this->configFactory
->getEditable('akamai.settings')
->set('action_v3', $actions)
->set('purge_urls_with_hostname', $form_state
->getValue([
'v3',
'purge_urls_with_hostname',
]))
->save();
}
public static function getSupportedVersions() {
$versions = [];
foreach (static::supportedTypes() as $type) {
$versions[] = Unicode::strtolower($type);
}
return $versions;
}
}