You are here

BaseCcuClient.php in Akamai 7.3

Constains the Drupal\akamai\BaseCcuClient class.

Abstract implementation of CcuClientInterface.

Namespace

Drupal\akamai

File

src/BaseCcuClient.php
View source
<?php

/**
 * @file
 * Constains the Drupal\akamai\BaseCcuClient class.
 *
 * Abstract implementation of CcuClientInterface.
 */
namespace Drupal\akamai;

use Akamai\Open\EdgeGrid\Client as EdgeGridClient;
use InvalidArgumentException;
abstract class BaseCcuClient implements CcuClientInterface {

  /**
   * The string used when invalidating objects.
   */
  const OPERATION_INVALIDATE = 'invalidate';

  /**
   * The string used when removing objects.
   */
  const OPERATION_DELETE = 'remove';

  /**
   * An instance of an OPEN EdgeGrid Client.
   *
   * @var \Akamai\Open\EdgeGrid\Client
   */
  protected $client;

  /**
   * The network to use when issuing purge requests.
   *
   * @var string
   */
  protected $network;

  /**
   * The operation to use when issuing purge requests.
   *
   * @var string
   */
  protected $operation;

  /**
   * Implements CcuClientInterface::__construct().
   */
  public function __construct(EdgeGridClient $client) {
    $this->client = $client;
    $this->network = static::NETWORK_PRODUCTION;
    $this->operation = static::OPERATION_INVALIDATE;
  }

  /**
   * Implements CcuClientInterface::setNetwork().
   */
  public function setNetwork($network) {
    if ($network != static::NETWORK_PRODUCTION && $network != static::NETWORK_STAGING) {
      throw new InvalidArgumentException('Invalid network supplied.');
    }
    $this->network = $network;
    return $this;
  }

  /**
   * Implements CcuClientInterface::setOperation().
   */
  public function setOperation($operation) {
    if ($operation != static::OPERATION_INVALIDATE && $operation != static::OPERATION_DELETE) {
      throw new InvalidArgumentException('Invalid operation supplied.');
    }
    $this->operation = $operation;
    return $this;
  }

  /**
   * Implements CcuClientInterface::checkProgress().
   */
  public function checkProgress($progress_uri) {
    $response = $this->client
      ->get($progress_uri);
    return json_decode($response
      ->getBody());
  }

  /**
   * Implements CcuClientInterface::postPurgeRequest().
   */
  public function postPurgeRequest($hostname, array $paths) {
    if (empty($hostname)) {
      throw new InvalidArgumentException("Expected hostname to be a non-empty string.");
    }
    $uri = $this
      ->getPurgeApiEndpoint();
    $response = $this->client
      ->post($uri, [
      'body' => $this
        ->getPurgeBody($hostname, $paths),
      'headers' => [
        'Content-Type' => 'application/json',
      ],
    ]);
    return json_decode($response
      ->getBody());
  }

  /**
   * Implements CcuClientInterface::bodyIsBelowLimit().
   */
  public function bodyIsBelowLimit($hostname, array $paths) {
    $body = $this
      ->getPurgeBody($hostname, $paths);
    $bytes = mb_strlen($body, '8bit');
    return $bytes < static::MAX_BODY_SIZE;
  }

}

Classes

Namesort descending Description
BaseCcuClient