commerce_amws-8.x-1.x-dev/src/Api/ClientBase.php
src/Api/ClientBase.php
<?php
namespace Drupal\commerce_amws\Api;
use Drupal\commerce_amws\Entity\StoreInterface;
use SellingPartnerApi\Configuration;
use SellingPartnerApi\Endpoint;
/**
* A base class for all Selling Partner API clients.
*/
abstract class ClientBase {
/**
* The AMWS store in the context of which all requests will be made.
*
* @var \Drupal\commerce_amws\Entity\StoreInterface
*/
protected $amwsStore;
/**
* The Selling Partner API client.
*
* @var mixed
*/
protected $client;
/**
* Returns the name of the Selling Partner API client.
*
* @return string
* The fully qualified class name.
*/
abstract protected function clientClassName();
/**
* Constructs a new OrderStorage object.
*
* @param \Drupal\commerce_amws\Entity\StoreInterface $amws_store
* The Amazon MWS store that the order list object will be created for.
*/
public function __construct(StoreInterface $amws_store) {
$this->amwsStore = $amws_store;
}
/**
* Returns the instantiated Selling Partner API class.
*
* @return mixed
* The client object.
*/
protected function client() {
if ($this->client) {
return $this->client;
}
$class = $this->clientClassName();
return new $class($this->prepareStoreConfig());
}
/**
* Prepares the configuration for initializing the Selling Partner API client.
*
* @return array
* The configuration array.
*/
protected function prepareStoreConfig() {
return new Configuration([
'lwaClientId' => $this->amwsStore->getLwaClientId(),
'lwaClientSecret' => $this->amwsStore->getLwaClientSecret(),
'lwaRefreshToken' => $this->amwsStore->getLwaRefreshToken(),
'awsAccessKeyId' => $this->amwsStore->getAwsAccessKeyId(),
'awsSecretAccessKey' => $this->amwsStore->getAwsSecretAccessKey(),
'endpoint' => Endpoint::getByMarketplaceId(
$this->amwsStore->getMarketplaceId()
),
]);
}
}
