2017 © Pedro Peláez
 

library sylius-api-php

Sylius API PHP client library

image

lakion/sylius-api-php

Sylius API PHP client library

  • Tuesday, January 19, 2016
  • by michalmarcinkowski
  • Repository
  • 11 Watchers
  • 19 Stars
  • 944 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 10 Forks
  • 8 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Sylius API PHP client library

Build status... Code Quality, (*1)

PHP client for RESTful Sylius API!, (*2)

Usage

Initialize client with OAuth2 authentication and mapping, (*3)

use GuzzleHttp\Client;
use Sylius\Api\Client as SyliusClient;
use Sylius\Api\Map\ArrayUriMap;
use Sylius\Api\ApiResolver;
use CommerceGuys\Guzzle\Oauth2\GrantType\RefreshToken;
use CommerceGuys\Guzzle\Oauth2\GrantType\PasswordCredentials;
use CommerceGuys\Guzzle\Oauth2\Oauth2Subscriber;

$oauth2Client = new Client([
    'base_url' => 'http://demo.sylius.org',
]);

$config = [
    'token_url' => 'oauth/v2/token',
    'client_id' => '1_demo_client',
    'client_secret' => 'secret_demo_client',
    'username' => 'api@example.com',
    'password' => 'api'
];

$token = new PasswordCredentials($oauth2Client, $config);
$refreshToken = new RefreshToken($oauth2Client, $config);

$oauth2 = new Oauth2Subscriber($token, $refreshToken);

// create sylius client
$syliusClient = SyliusClient::createFromUrl('http://demo.sylius.org/api/', $oauth2);

// initialize uri mapping for custom uris, all other uris are autogenerated (plural name of the resource, in accordance with RESTful API resource naming best practices)
$uriMap = new ArrayUriMap(
    [
        'product-variants' => 'products/{productId}/variants',
        'taxons'           => 'taxonomies/{taxonomyId}/taxons',
        'items'            => 'orders/{orderId}/items',
        'coupons'          => 'promotions/{promotionId}/coupons',
        'provinces'        => 'countries/{countryId}/provinces'
    ]
);
// initializer api resolver
$apiResolver = new ApiResolver($syliusClient, $uriMap);

If you use Symfony2 framework, you can also register some services, initialize Sylius API Client and inject it to other services., (*4)


parameters: app.http_client.sylius.base_url: 'http://local.sylius.dev/app_dev.php/' app.http_client.sylius.api_url: 'http://local.sylius.dev/app_dev.php/api/' app.http_client.sylius.token_url: 'oauth/v2/token' app.sylius_client.id: 1_demo_client app.sylius_client.secret: secret_demo_client app.sylius_client.username: api@example.com app.sylius_client.password: api app.sylius_taxonomy.uri: taxonomies services: #Http client that is used to obtain OAuth2 token app.http_client.sylius_oauth: class: GuzzleHttp\Client arguments: - { base_url: %app.http_client.sylius.base_url% } #Password credentials that use http client to authenticate user with given data app.oauth2.grant_type.password_credentials: class: CommerceGuys\Guzzle\Oauth2\GrantType\PasswordCredentials arguments: - @app.http_client.sylius_oauth - { token_url: %app.http_client.sylius.token_url%, client_id: %app.sylius_client.id%, client_secret: %app.sylius_client.secret%, username: %app.sylius_client.username%, password: %app.sylius_client.password% } #Refresh token that use http client to refresh oauth2 token app.oauth2.grant_type.refresh_token: class: CommerceGuys\Guzzle\Oauth2\GrantType\RefreshToken arguments: - @app.http_client.sylius_oauth - { token_url: %app.http_client.sylius.token_url%, client_id: %app.sylius_client.id%, client_secret: %app.sylius_client.secret%, username: %app.sylius_client.username%, password: %app.sylius_client.password% } #Subscriber used by Sylius API Client to provide OAuth2 authentication app.oauth2.subscriber.sylius_api: class: CommerceGuys\Guzzle\Oauth2\Oauth2Subscriber arguments: [@app.oauth2.grant_type.password_credentials, @app.oauth2.grant_type.refresh_token] #Http client that is used to connect with Sylius API app.http_client.sylius_api: class: GuzzleHttp\Client arguments: - { base_url: '%app.http_client.sylius.api_url%', defaults: { auth: 'oauth2', subscribers: [@app.oauth2.subscriber.sylius_api] } } #Sylius API Client app.api_client.sylius: class: Sylius\Api\Client arguments: [@app.http_client.sylius_api] #Example API app.sylius_taxonomy.api: class: Sylius\Api\GenericApi arguments: [@app.api_client.sylius, %app.sylius_taxonomy.uri%] #Uri map with custom uris app.api_resolver.uri_map.sylius: class: Sylius\Api\Map\ArrayUriMap arguments: - { product-variants: 'products/{productId}/variants', taxons: 'taxonomies/{taxonomyId}/taxons', items: 'orders/{orderId}/items', coupons: 'promotions/{promotionId}/coupons', provinces: 'countries/{countryId}/provinces' } #Initialize Api resolver with uri map app.api_resolver.sylius: class: Sylius\Api\ApiResolver arguments: [@app.api_client.sylius, @app.api_resolver.uri_map.sylius]

Get API for resource, (*5)

#Obtain api using api resolver
$taxonomiesApi = $apiResolver->getApi('taxonomies');
#or register it as a service and obtain it from container
$taxonomiesApi = $container->get('app.sylius_taxonomy.api');
$taxonsApi = $apiResolver->getApi('taxons');

Create resource, (*6)

// right now because of translations you have to do it this way (will be changed in the nearest future to simply allow for ['name' => 'My taxonomy'])
$taxonomy = $taxonomiesApi->create(['translations' => ['en' => ['name' => 'My taxonomy']]]);
// for custom uris you have to specify uri parameters ('taxonomyId')
$taxon = $taxonsApi->create(['translations' => ['en' => ['name' => 'My taxon']]], ['taxonomyId' => $taxonomy['id']);

Update resource, (*7)

$taxonomy = $taxonomiesApi->update($taxonomyId, ['translations' => ['en' => ['name' => 'My taxonomy updated']]]);
// for custom uris you have to specify uri parameters ('taxonomyId')
$taxon = $taxonsApi->update($taxonId, $taxonomy['translations' => ['en' => ['name' => 'My taxon' updated]]], ['taxonomyId' => $taxonomyId]);

Get resource, (*8)

$taxonomy = $taxonomiesApi->get($taxonomyId);
// for custom uris you have to specify uri parameters ('taxonomyId')
$taxon = $taxonsApi->get($taxonId, ['taxonomyId' => $taxonomyId]);

Get all resources, (*9)

  • Get paginated resources
// you can getPaginated resources (it returns array)
$taxonomies = $taxonomiesApi->getPaginated($page, $limitPerPage);
$taxons = $taxonsApi->getPaginated($page, $limitPerPage, ['taxonomyId' => $taxonomyId]);
  • Create paginator for resource
// you can create taxonomies paginator
$taxonomiesPaginator = $taxonomiesApi->createPaginator($limitPerPage);
// for custom uris you have to specify uri parameters ('taxonomyId'), rest is the same
$taxonsPaginator = $taxonsApi->createPaginator($limitPerPage, ['taxonomyId' => $taxonomyId]);
// returns array of elements for current page
$taxonomies = $taxonomiesPaginator->getCurrentPageResults();
// ... (do something with current page taxonomies)
// and go through all results
while ($taxonomiesPaginator->hasNextPage()) {
    $taxonomiesPaginator->nextPage();
    $taxonomies = $taxonomiesPaginator->getCurrentPageResults();
    // ... (do something with current page taxonomies)
}
  • Get all resources
// getAll method gets all resources, so you have to be careful about the memory usage
$taxonomies = $taxonomiesApi->getAll();
// for custom uris you have to specify uri parameters ('taxonomyId')
$taxons = $taxonsApi->getAll(['taxonomyId' => $taxonomyId]);

Delete resource, (*10)

// returns whether the deletion was successful or not
// for custom uris you have to specify uri parameters ('taxonomyId')
$trueOrFalse = $taxonsApi->delete($taxonId, ['taxonomyId' => $taxonomyId]);
$trueOrFalse = $taxonomiesApi->delete($taxonomyId);

Bug tracking

This component uses GitHub issues. If you have found bug, please create an issue., (*11)

MIT License

License can be found here., (*12)

Authors

The library was originally created by:, (*13)

See the list of contributors., (*14)

The Versions

19/01 2016

dev-master

9999999-dev

Sylius API PHP client library

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michal Marcinkowski

15/01 2016

dev-michalmarcinkowski-patch-1

dev-michalmarcinkowski-patch-1

Sylius API PHP client library

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michal Marcinkowski

15/01 2016

dev-guzzle-6

dev-guzzle-6

Sylius API PHP client library

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michal Marcinkowski

16/11 2015

dev-enhance-api-adapter

dev-enhance-api-adapter

Sylius API PHP client library

  Sources   Download

MIT

The Requires

 

The Development Requires

by Michal Marcinkowski