You are here

public static function AkamaiAuthentication::create in Akamai 8.3

AkamaiAuthentication factory method, following superclass patterns.

Parameters

\Drupal\Core\Config\ConfigFactoryInterface $config: A config factory, for getting client authentication details.

\Drupal\Core\Messenger\MessengerInterface $messenger: A messenger service.

\Drupal\akamai\KeyProviderInterface $key_provider: A akamai.key_provider service.

Return value

\Drupal\akamai\AkamaiAuthentication An authentication object.

2 calls to AkamaiAuthentication::create()
AkamaiAuthenticationTest::testSetupEdgeRc in tests/src/Unit/AkamaiAuthenticationTest.php
Tests that we can authorise when specifying edgerc file.
AkamaiClientBase::__construct in src/AkamaiClientBase.php
AkamaiClient constructor.

File

src/AkamaiAuthentication.php, line 33

Class

AkamaiAuthentication
Connects to the Akamai EdgeGrid.

Namespace

Drupal\akamai

Code

public static function create(ConfigFactoryInterface $config, MessengerInterface $messenger, KeyProviderInterface $key_provider) {

  // Following the pattern in the superclass.
  $auth = new static();
  $config = $config
    ->get('akamai.settings');
  $storage_method = $config
    ->get('storage_method');
  if ($storage_method == 'file') {
    $section = $config
      ->get('edgerc_section') ?: 'default';
    $path = $config
      ->get('edgerc_path') ?: NULL;
    try {
      $auth = static::createFromEdgeRcFile($section, $path);
    } catch (ConfigException $e) {
      $messenger
        ->addWarning($e
        ->getMessage());
    }
  }
  elseif ($storage_method == 'key' && $key_provider
    ->hasKeyRepository()) {
    $keys = [
      'access_token',
      'client_token',
      'client_secret',
    ];
    $key_values = [];
    $missing_values = FALSE;
    foreach ($keys as $key) {
      $key_values[$key] = $key_provider
        ->getKey($config
        ->get($key));
      if (!isset($key_values[$key])) {
        $messenger
          ->addWarning(t('Missing @key.', [
          '@key' => $key,
        ]));
        $missing_values = TRUE;
      }
    }
    if (!$missing_values) {
      $auth
        ->setHost($config
        ->get('rest_api_url'));

      // Set the auth credentials up.
      // @see Authentication::createFromEdgeRcFile()
      $auth
        ->setAuth($key_values['client_token'], $key_values['client_secret'], $key_values['access_token']);
    }
  }
  return $auth;
}