You are here

class ClientFactory in Media: Acquia DAM 8

Class ClientFactory.

Factory class for Client.

Hierarchy

Expanded class hierarchy of ClientFactory

5 files declare their use of ClientFactory
AcquiadamClientFactoryTest.php in tests/src/Unit/AcquiadamClientFactoryTest.php
AcquiadamConfig.php in src/Form/AcquiadamConfig.php
AcquiadamConfigFormTest.php in tests/src/Unit/AcquiadamConfigFormTest.php
AcquiadamKernelTestBase.php in tests/src/Kernel/AcquiadamKernelTestBase.php
AcquiadamServiceTest.php in tests/src/Unit/AcquiadamServiceTest.php
1 string reference to 'ClientFactory'
media_acquiadam.services.yml in ./media_acquiadam.services.yml
media_acquiadam.services.yml
1 service uses ClientFactory
media_acquiadam.client_factory in ./media_acquiadam.services.yml
Drupal\media_acquiadam\ClientFactory

File

src/ClientFactory.php, line 17

Namespace

Drupal\media_acquiadam
View source
class ClientFactory implements ContainerInjectionInterface {

  /**
   * A config object to retrieve Acquia DAM auth information from.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * A fully-configured Guzzle client to pass to the dam client.
   *
   * @var \GuzzleHttp\ClientInterface
   */
  protected $guzzleClient;

  /**
   * A user data object to retrieve API keys from.
   *
   * @var \Drupal\user\UserDataInterface
   */
  protected $userData;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * ClientFactory constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   A config object to retrieve Acquia DAM auth information from.
   * @param \GuzzleHttp\ClientInterface $guzzleClient
   *   A fully configured Guzzle client to pass to the dam client.
   * @param \Drupal\user\UserDataInterface $userData
   *   A userdata object to retreive user-specific creds from.
   * @param \Drupal\Core\Session\AccountProxyInterface $currentUser
   *   The currently authenticated user.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ClientInterface $guzzleClient, UserDataInterface $userData, AccountProxyInterface $currentUser) {
    $this->config = $config_factory
      ->get('media_acquiadam.settings');
    $this->guzzleClient = $guzzleClient;
    $this->userData = $userData;
    $this->currentUser = $currentUser;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('http_client'), $container
      ->get('user.data'), $container
      ->get('current_user'));
  }

  /**
   * Creates a new DAM client object.
   *
   * @param string $credentials
   *   The switch for which credentials the client object
   *   should be configured with.
   *
   * @return \Drupal\media_acquiadam\Client
   *   A configured DAM HTTP client object.
   */
  public function get($credentials = 'background') {
    $client = $this
      ->getWithCredentials($this->config
      ->get('username'), $this->config
      ->get('password'), $this->config
      ->get('client_id'), $this->config
      ->get('secret'));

    // Set the user's credentials in the client if necessary.
    if ($credentials == 'current') {
      $access_token = $this->userData
        ->get('media_acquiadam', $this->currentUser
        ->id(), 'acquiadam_access_token');
      $access_token_expiration = $this->userData
        ->get('media_acquiadam', $this->currentUser
        ->id(), 'acquiadam_access_token_expiration');
      $refresh_token = $this->userData
        ->get('media_acquiadam', $this->currentUser
        ->id(), 'acquiadam_refresh_token');
      $client
        ->setToken($access_token, $access_token_expiration, $refresh_token);
    }
    return $client;
  }

  /**
   * Gets a base DAM Client object using the specified credentials.
   *
   * @param string $username
   *   The username to authenticate with.
   * @param string $password
   *   The password to authenticate with.
   * @param string $client_id
   *   The client ID to authenticate with.
   * @param string $secret
   *   The secret to authenticate with.
   *
   * @return \Drupal\media_acquiadam\Client
   *   The Acquia DAM client.
   */
  public function getWithCredentials($username, $password, $client_id, $secret) {
    return new Client($this->guzzleClient, $username, $password, $client_id, $secret);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ClientFactory::$config protected property A config object to retrieve Acquia DAM auth information from.
ClientFactory::$currentUser protected property The current user.
ClientFactory::$guzzleClient protected property A fully-configured Guzzle client to pass to the dam client.
ClientFactory::$userData protected property A user data object to retrieve API keys from.
ClientFactory::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
ClientFactory::get public function Creates a new DAM client object.
ClientFactory::getWithCredentials public function Gets a base DAM Client object using the specified credentials.
ClientFactory::__construct public function ClientFactory constructor.