Composable Commerce PHP SDKPHP SDK
  • Namespace
  • Class
  • Tree

Namespaces

  • Commercetools
    • Commons
      • Helper
    • Core
      • Builder
        • Request
        • Update
      • Cache
      • Client
        • Adapter
        • OAuth
      • Error
      • Helper
        • Annotate
        • State
          • Renderer
        • Subscriber
          • Log
      • Model
        • ApiClient
        • Cart
        • CartDiscount
        • Category
        • Channel
        • Common
        • Customer
        • CustomerGroup
        • CustomField
        • CustomObject
        • DiscountCode
        • Extension
        • Inventory
        • Message
        • Order
        • OrderEdit
        • Payment
        • Product
          • Search
        • ProductDiscount
        • ProductSelection
        • ProductType
        • Project
        • Review
        • ShippingMethod
        • ShoppingList
        • State
        • Store
        • Subscription
        • TaxCategory
        • Type
        • Zone
      • Request
        • ApiClients
        • CartDiscounts
          • Command
        • Carts
          • Command
        • Categories
          • Command
        • Channels
          • Command
        • CustomerGroups
          • Command
        • Customers
          • Command
        • CustomField
          • Command
        • CustomObjects
        • DiscountCodes
          • Command
        • Extensions
          • Command
        • GraphQL
        • InStores
        • Inventory
          • Command
        • Me
          • Command
        • Messages
        • OrderEdits
          • Command
          • StagedOrder
            • Command
        • Orders
          • Command
        • Payments
          • Command
        • ProductDiscounts
          • Command
        • Products
          • Command
        • ProductSelections
          • Command
        • ProductTypes
          • Command
        • Project
          • Command
        • Query
        • Reviews
          • Command
        • ShippingMethods
          • Command
        • ShoppingLists
          • Command
        • States
          • Command
        • Stores
          • Command
        • Subscriptions
          • Command
        • TaxCategories
          • Command
        • Types
          • Command
        • Zones
          • Command
      • Response

Classes

  • AbstractHttpClient
  • Client
  • Config

Class Client

The client for communicating with Composable Commerce

Commercetools\Core\AbstractHttpClient
Extended by Commercetools\Core\Client implements Psr\Log\LoggerAwareInterface
Namespace: Commercetools\Core
Description:

Instantiation

$config = Config::fromArray(
 ['client_id' => '<client_id>', 'client_secret' => '<client_secret>', 'project' => '<project>']
);
$client = Client::ofConfig($config);

Execution

Synchronous

There are two main approaches for retrieving result objects

Client centric:

$response = $client->execute(ProductProjectionSearchRequest::of());
$products = $response->toObject();

Request centric:

$request = ProductProjectionSearchRequest::of();
$response = $request->executeWithClient($client);
$products = $request->mapFromResponse($response);

By using the request centric approach the IDE is capable of resolving the correct classes and give a maximum of support with available methods.

Asynchronous

The asynchronous execution will return a promise to fulfill the request.

$response = $client->executeAsync(ProductProjectionSearchRequest::of());

Batch

By filling the batch queue and starting the execution all requests will be executed in parallel.

$client->addBatchRequest(ProductProjectionSearchRequest::of())
    ->addBatchRequest(CartByIdGetRequest::ofId($cartId));
$responses = $client->executeBatch();

Instantiation options

Using a logger

The client uses the PSR-3 logger interface for logging requests and deprecation notices. To enable logging provide a PSR-3 compliant logger (e.g. Monolog).

$logger = new \Monolog\Logger('name');
$logger->pushHandler(new StreamHandler('./requests.log'));
$client = Client::ofConfigAndLogger($config, $logger);

Using a cache adapter

The client will automatically request an OAuth token and store the token in the provided cache.

It's also possible to use a different cache adapter. The SDK provides a Doctrine, a Redis and an APCu cache adapter. By default the SDK tries to instantiate the APCu or a PSR-6 filesystem cache adapter if there is no cache given. E.g. Redis:

$redis = new \Redis();
$redis->connect('localhost');
$client = Client::ofConfigAndCache($config, $redis);

Using cache and logger

$client = Client::ofConfigCacheAndLogger($config, $cache, $logger);

Using a custom cache adapter

class <CacheClass>Adapter implements \Commercetools\Core\Cache\CacheAdapterInterface {
    protected $cache;
    public function __construct(<CacheClass> $cache) {
        $this->cache = $cache;
    }
}

$client->getAdapterFactory()->registerCallback(function ($cache) {
    if ($cache instanceof <CacheClass>) {
        return new <CacheClass>Adapter($cache);
    }
    return null;
});

Located at Core/Client.php

Methods summary

public
# __construct( array|Commercetools\Core\Config $config, $cache = null, Psr\Log\LoggerInterface $logger = null )

Parameters

$config
$cache
$logger

Overrides

Commercetools\Core\AbstractHttpClient::__construct
public Commercetools\Core\Client\OAuth\Manager
# getOauthManager( )

Returns

Commercetools\Core\Client\OAuth\Manager
public Commercetools\Core\Client
# setLogger( Psr\Log\LoggerInterface $logger = null )

Parameters

$logger

Returns

Commercetools\Core\Client
$this
public Commercetools\Core\Client\Adapter\AdapterInterface
# getHttpClient( array $options = [] )

Parameters

$options

Returns

Commercetools\Core\Client\Adapter\AdapterInterface

Overrides

Commercetools\Core\AbstractHttpClient::getHttpClient
public Commercetools\Core\Response\ApiResponseInterface
# execute( Commercetools\Core\Request\ClientRequestInterface $request, array $headers = null, array $clientOptions = [] )

Executes an API request synchronously

Executes an API request synchronously

Parameters

$request
$headers
$clientOptions

Returns

Commercetools\Core\Response\ApiResponseInterface

Throws

Commercetools\Core\Error\ApiException
Commercetools\Core\Error\InvalidTokenException
public Commercetools\Core\Response\ApiResponseInterface|Commercetools\Core\Response\ApiPromiseGetInterface
# executeAsync( Commercetools\Core\Request\ClientRequestInterface $request, array $headers = null, array $clientOptions = [] )

Executes an API request asynchronously

Executes an API request asynchronously

Parameters

$request
$headers
$clientOptions
$clientOptions

Returns

Commercetools\Core\Response\ApiResponseInterface|Commercetools\Core\Response\ApiPromiseGetInterface
public Commercetools\Core\Response\ApiResponseInterface[]
# executeBatch( array $headers = null, array $clientOptions = [] )

Executes API requests in batch

Executes API requests in batch

Parameters

$headers
$clientOptions

Returns

Commercetools\Core\Response\ApiResponseInterface[]

Throws

Commercetools\Core\Error\ApiException
public Commercetools\Core\Client
# addBatchRequest( Commercetools\Core\Request\ClientRequestInterface $request )

Adds a request to the batch execution queue

Adds a request to the batch execution queue

Parameters

$request

Returns

Commercetools\Core\Client
$this
public static Commercetools\Core\Client
# ofConfig( Commercetools\Core\Config $config )

Instantiates a client with the given config

Instantiates a client with the given config

Parameters

$config

Returns

Commercetools\Core\Client
public static Commercetools\Core\Client
# ofConfigAndCache( Commercetools\Core\Config $config, $cache )

Instantiates a client with the given config and cache adapter

Instantiates a client with the given config and cache adapter

Parameters

$config
$cache

Returns

Commercetools\Core\Client
public static Commercetools\Core\Client
# ofConfigAndLogger( Commercetools\Core\Config $config, Psr\Log\LoggerInterface $logger )

Instantiates a client with the given config and a PSR-3 compliant logger

Instantiates a client with the given config and a PSR-3 compliant logger

Parameters

$config
$logger

Returns

Commercetools\Core\Client
public static Commercetools\Core\Client
# ofConfigCacheAndLogger( Commercetools\Core\Config $config, $cache, Psr\Log\LoggerInterface $logger )

Instantiates a client with the given config, a cache adapter and a PSR-3 compliant logger

Instantiates a client with the given config, a cache adapter and a PSR-3 compliant logger

Parameters

$config
$cache
$logger

Returns

Commercetools\Core\Client

Methods inherited from Commercetools\Core\AbstractHttpClient

getAdapterFactory(), getConfig(), setConfig()

Constants summary

string DEPRECATION_HEADER
# 'X-DEPRECATION-NOTICE'

Constants inherited from Commercetools\Core\AbstractHttpClient

VERSION

PHP SDK API documentation generated by ApiGen