You are here

class Amazon in Amazon Product Advertisement API 8.2

Provides methods that interfaces with the Amazon Product Advertising API.

@package Drupal\amazon

Hierarchy

Expanded class hierarchy of Amazon

3 files declare their use of Amazon
AmazonFieldFormatter.php in modules/amazon_field/src/Plugin/Field/FieldFormatter/AmazonFieldFormatter.php
FilterAmazon.php in modules/amazon_filter/src/Plugin/Filter/FilterAmazon.php
Contains \Drupal\amazon_filter\Plugin\Filter\FilterAmazon.
SettingsForm.php in src/Form/SettingsForm.php
Contains Drupal\amazon\Form\SettingsForm.
3 string references to 'Amazon'
amazon.info.yml in ./amazon.info.yml
amazon.info.yml
amazon_field.info.yml in modules/amazon_field/amazon_field.info.yml
modules/amazon_field/amazon_field.info.yml
amazon_filter.info.yml in modules/amazon_filter/amazon_filter.info.yml
modules/amazon_filter/amazon_filter.info.yml

File

src/Amazon.php, line 19
Contains Drupal\amazon\Amazon

Namespace

Drupal\amazon
View source
class Amazon {

  /**
   * The server environment variables for (optionally) specifying the access
   * key and secret.
   */
  const AMAZON_ACCESS_KEY = 'AMAZON_ACCESS_KEY';
  const AMAZON_ACCESS_SECRET = 'AMAZON_ACCESS_SECRET';

  /**
   * @var \ApaiIO\ApaiIO
   *   The Amazon API object.
   */
  protected $apaiIO;

  /**
   * Provides an Amazon object for calling the Amazon API.
   *
   * @param string $associatesId
   *   The Amazon Associates ID (a.k.a. tag).
   * @param string $accessKey
   *   (optional) Access key to use for all API requests. If not specified, the
   *   access key is determined from other system variables.
   * @param string $accessSecret
   *   (optional) Access secret to use for all API requests. If not specified,
   *   the access key is determined from other system variables.
   * @param string $locale
   *   (optional) Which locale to run queries against. Valid values include: de,
   *   com, co.uk, ca, fr, co.jp, it, cn, es, in.
   */
  public function __construct($associatesId, $accessKey = '', $accessSecret = '', $locale = 'com') {
    if (empty($accessKey)) {
      $accessKey = self::getAccessKey();
      if (!$accessKey) {
        throw new \InvalidArgumentException('Configuration missing: Amazon access key.');
      }
    }
    if (empty($accessSecret)) {
      $accessSecret = self::getAccessSecret();
      if (!$accessSecret) {
        throw new \InvalidArgumentException('Configuration missing: Amazon access secret.');
      }
    }
    $conf = new GenericConfiguration();
    $conf
      ->setCountry($locale)
      ->setAccessKey($accessKey)
      ->setSecretKey($accessSecret)
      ->setAssociateTag($associatesId)
      ->setResponseTransformer('\\Drupal\\amazon\\LookupXmlToItemsArray');
    $this->apaiIO = new ApaiIO($conf);
  }

  /**
   * Returns the secret key needed for API calls.
   *
   * @return string|bool
   *   String on success, FALSE otherwise.
   */
  public static function getAccessSecret() {

    // Use credentials from environment variables, if available.
    $secret = getenv(self::AMAZON_ACCESS_SECRET);
    if ($secret) {
      return $secret;
    }

    // If not, use Drupal config variables. (Automatically handles overrides
    // in settings.php.)
    $secret = \Drupal::config('amazon.settings')
      ->get('access_secret');
    if ($secret) {
      return $secret;
    }
    return FALSE;
  }

  /**
   * Returns the access key needed for API calls.
   *
   * @return string|bool
   *   String on success, FALSE otherwise.
   */
  public static function getAccessKey() {

    // Use credentials from environment variables, if available.
    $key = getenv(self::AMAZON_ACCESS_KEY);
    if ($key) {
      return $key;
    }

    // If not, use Drupal config variables. (Automatically handles overrides
    // in settings.php.)
    $key = \Drupal::config('amazon.settings')
      ->get('access_key');
    if ($key) {
      return $key;
    }
    return FALSE;
  }

  /**
   * Gets information about an item, or array of items, from Amazon.
   *
   * @param array|string $items
   *   A string containing a single ASIN, or an array of ASINs, to look up.
   *
   * @return array
   *   An array of SimpleXMLElement objects representing the response from
   *   Amazon.
   */
  public function lookup($items) {
    if (empty($items)) {
      throw new \InvalidArgumentException('Calling lookup without anything to lookup!');
    }
    if (!is_array($items)) {
      $items = [
        $items,
      ];
    }
    $results = [];

    // Cannot ask for info from more than 10 items in a single call.
    foreach (array_chunk($items, 10) as $asins) {
      $lookup = new Lookup();
      $lookup
        ->setItemIds($asins);
      $lookup
        ->setResponseGroup([
        'Small',
        'Images',
      ]);
      $results = array_merge($results, $this->apaiIO
        ->runOperation($lookup));
    }
    return $results;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Amazon::$apaiIO protected property The Amazon API object.
Amazon::AMAZON_ACCESS_KEY constant The server environment variables for (optionally) specifying the access key and secret.
Amazon::AMAZON_ACCESS_SECRET constant
Amazon::getAccessKey public static function Returns the access key needed for API calls.
Amazon::getAccessSecret public static function Returns the secret key needed for API calls.
Amazon::lookup public function Gets information about an item, or array of items, from Amazon.
Amazon::__construct public function Provides an Amazon object for calling the Amazon API.