From a1c1db006d035ffea22a37db2b807fdcd3fb0653 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Planchat?= Date: Wed, 10 Feb 2021 13:18:42 +0100 Subject: [PATCH] Added a fluent interface in the SyliusClientBuilder Fixed some PHPSpec tests --- spec/Api/AuthenticationApiSpec.php | 28 +-- spec/Client/ResourceClientSpec.php | 34 ++-- .../UnprocessableEntityHttpExceptionSpec.php | 40 ++-- spec/Routing/UriGeneratorSpec.php | 2 +- spec/SyliusClientSpec.php | 178 +++++++++++++++++- src/SyliusClient.php | 176 ++++++++--------- src/SyliusClientBuilder.php | 108 +++++------ src/SyliusClientBuilderInterface.php | 10 +- 8 files changed, 351 insertions(+), 225 deletions(-) diff --git a/spec/Api/AuthenticationApiSpec.php b/spec/Api/AuthenticationApiSpec.php index 523632d..d23b080 100644 --- a/spec/Api/AuthenticationApiSpec.php +++ b/spec/Api/AuthenticationApiSpec.php @@ -41,13 +41,13 @@ public function it_authenticates_with_the_password_grant_type( $response->getBody()->willReturn($body); $responseContent = <<<'JSON' - { - "access_token": "foo", - "expires_in": 3600, - "token_type": "bearer", - "scope": null, - "refresh_token": "bar" - } + { + "access_token": "foo", + "expires_in": 3600, + "token_type": "bearer", + "scope": null, + "refresh_token": "bar" + } JSON; $body->getContents()->willReturn($responseContent); @@ -78,13 +78,13 @@ public function it_authenticates_with_the_refresh_token_type( $response->getBody()->willReturn($body); $responseContent = <<<'JSON' - { - "access_token": "foo", - "expires_in": 3600, - "token_type": "bearer", - "scope": null, - "refresh_token": "baz" - } + { + "access_token": "foo", + "expires_in": 3600, + "token_type": "bearer", + "scope": null, + "refresh_token": "baz" + } JSON; $body->getContents()->willReturn($responseContent); diff --git a/spec/Client/ResourceClientSpec.php b/spec/Client/ResourceClientSpec.php index dbe78a4..a738907 100644 --- a/spec/Client/ResourceClientSpec.php +++ b/spec/Client/ResourceClientSpec.php @@ -35,17 +35,16 @@ public function it_is_initializable() public function it_gets_resource($httpClient, $uriGenerator, ResponseInterface $response, StreamInterface $responseBody) { $uri = 'http://diglin.com/api/rest/v1/categories/winter_collection'; - $resource = -<<<'JSON' - { - "code": "winter_collection", - "parent": null, - "labels": { - "en_US": "Winter collection", - "fr_FR": "Collection hiver" - } - } - JSON; + $resource = <<<'JSON' + { + "code": "winter_collection", + "parent": null, + "labels": { + "en_US": "Winter collection", + "fr_FR": "Collection hiver" + } + } + JSON; $uriGenerator ->generate('api/rest/v1/categories/%s', ['winter_collection'], []) @@ -231,13 +230,12 @@ public function it_upserts_a_list_of_resources_from_an_array( ->willReturn($uri) ; - $body = -<<<'JSON' - {"code":"category_1"} - {"code":"category_2"} - {"code":"category_3"} - {"code":"category_4"} - JSON; + $body = <<<'JSON' + {"code":"category_1"} + {"code":"category_2"} + {"code":"category_3"} + {"code":"category_4"} + JSON; $httpClient ->sendRequest('PATCH', $uri, ['Content-Type' => 'application/vnd.akeneo.collection+json'], $body) diff --git a/spec/Exception/UnprocessableEntityHttpExceptionSpec.php b/spec/Exception/UnprocessableEntityHttpExceptionSpec.php index 6e283cf..df4c618 100644 --- a/spec/Exception/UnprocessableEntityHttpExceptionSpec.php +++ b/spec/Exception/UnprocessableEntityHttpExceptionSpec.php @@ -25,22 +25,21 @@ public function it_exposes_the_response_errors($response, StreamInterface $body) $response->getStatusCode()->willReturn(422); $response->getBody()->willReturn($body); $body->rewind()->shouldBeCalled(); - $body->getContents()->willReturn( - <<<'JSON' - { - "code": "422", - "message": "The response message", - "errors": [ - { - "property": "labels", - "message": "The first error" - }, - { - "property": "labels", - "message": "The second error" - } - ] - } + $body->getContents()->willReturn(<<<'JSON' + { + "code": "422", + "message": "The response message", + "errors": [ + { + "property": "labels", + "message": "The first error" + }, + { + "property": "labels", + "message": "The second error" + } + ] + } JSON ); @@ -61,11 +60,10 @@ public function it_returns_an_empty_array_when_the_response_has_no_errors($respo $response->getStatusCode()->willReturn(422); $response->getBody()->willReturn($body); $body->rewind()->shouldBeCalled(); - $body->getContents()->willReturn( - <<<'JSON' - { - "code": "422", - } + $body->getContents()->willReturn(<<<'JSON' + { + "code": "422", + } JSON ); diff --git a/spec/Routing/UriGeneratorSpec.php b/spec/Routing/UriGeneratorSpec.php index 10cc977..2a6a5d7 100644 --- a/spec/Routing/UriGeneratorSpec.php +++ b/spec/Routing/UriGeneratorSpec.php @@ -6,7 +6,7 @@ class UriGeneratorSpec extends ObjectBehavior { - public const BASE_URI = 'http://akeneo-pim.local/'; + public const BASE_URI = 'http://sylius.local/'; public function let() { diff --git a/spec/SyliusClientSpec.php b/spec/SyliusClientSpec.php index 7679f8a..d84d5f6 100644 --- a/spec/SyliusClientSpec.php +++ b/spec/SyliusClientSpec.php @@ -2,7 +2,7 @@ namespace spec\Diglin\Sylius\ApiClient; -use Diglin\Sylius\ApiClient\Api\ChannelsApiInterface; +use Diglin\Sylius\ApiClient\Api; use Diglin\Sylius\ApiClient\Security\Authentication; use Diglin\Sylius\ApiClient\SyliusClient; use Diglin\Sylius\ApiClient\SyliusClientInterface; @@ -12,11 +12,59 @@ class SyliusClientSpec extends ObjectBehavior { public function let( Authentication $authentication, - ChannelsApiInterface $channelApi + Api\CartsApiInterface $cartsApi, + Api\ChannelsApiInterface $channelsApi, + Api\CountriesApiInterface $countriesApi, + Api\CurrenciesApiInterface $currenciesApi, + Api\CustomersApiInterface $customersApi, + Api\ExchangeRatesApiInterface $exchangeRatesApi, + Api\LocalesApiInterface $localesApi, + Api\OrdersApiInterface $ordersApi, + Api\PaymentMethodsApiInterface $paymentMethodApi, + Api\PaymentsApiInterface $paymentsApi, + Api\ProductsApiInterface $productsApi, + Api\ProductAttributesApiInterface $productAttributesApi, + Api\ProductAssociationTypesApiInterface $productAssociationTypesApi, + Api\ProductOptionsApiInterface $productOptionsApi, + Api\ProductReviewsApiInterface $productReviewsApi, + Api\ProductVariantsApiInterface $productVariantsApi, + Api\PromotionsApiInterface $promotionsApi, + Api\PromotionCouponsApiInterface $promotionCouponsApi, + Api\ShipmentsApiInterface $shipmentsApi, + Api\ShippingCategoriesApiInterface $shippingCategoriesApi, + Api\TaxCategoriesApiInterface $taxCategoriesApi, + Api\TaxRatesApiInterface $taxRatesApi, + Api\TaxonsApiInterface $taxonsApi, + Api\UsersApiInterface $usersApi, + Api\ZonesApiInterface $zonesApi ) { $this->beConstructedWith( $authentication, - $channelApi + $cartsApi, + $channelsApi, + $countriesApi, + $currenciesApi, + $customersApi, + $exchangeRatesApi, + $localesApi, + $ordersApi, + $paymentMethodApi, + $paymentsApi, + $productsApi, + $productAttributesApi, + $productAssociationTypesApi, + $productOptionsApi, + $productReviewsApi, + $productVariantsApi, + $promotionsApi, + $promotionCouponsApi, + $shipmentsApi, + $shippingCategoriesApi, + $taxCategoriesApi, + $taxRatesApi, + $taxonsApi, + $usersApi, + $zonesApi ); } @@ -40,8 +88,128 @@ public function it_gets_refresh_token($authentication) $this->getRefreshToken()->shouldReturn('bar'); } - public function it_gets_channel_api($channelApi) + public function it_gets_cart_api() { - $this->getChannelsApi()->shouldReturn($channelApi); + $this->getCartsApi()->shouldReturnAnInstanceOf(Api\CartsApiInterface::class); + } + + public function it_gets_channel_api() + { + $this->getChannelsApi()->shouldReturnAnInstanceOf(Api\ChannelsApiInterface::class); + } + + public function it_gets_country_api() + { + $this->getCountriesApi()->shouldReturnAnInstanceOf(Api\CountriesApiInterface::class); + } + + public function if_gets_currencies_api() + { + $this->getCurrenciesApi()->shouldReturnAnInstanceOf(Api\CurrenciesApiInterface::class); + } + + public function if_gets_customers_api() + { + $this->getCustomersApi()->shouldReturnAnInstanceOf(Api\CustomersApiInterface::class); + } + + public function if_gets_exchangeRates_api() + { + $this->getExchangeRatesApi()->shouldReturnAnInstanceOf(Api\ExchangeRatesApiInterface::class); + } + + public function if_gets_locales_api() + { + $this->getLocalesApi()->shouldReturnAnInstanceOf(Api\LocalesApiInterface::class); + } + + public function if_gets_orders_api() + { + $this->getOrdersApi()->shouldReturnAnInstanceOf(Api\OrdersApiInterface::class); + } + + public function if_gets_paymentMethods_api() + { + $this->getPaymentMethodsApi()->shouldReturnAnInstanceOf(Api\PaymentMethodsApiInterface::class); + } + + public function if_gets_payments_api() + { + $this->getPaymentsApi()->shouldReturnAnInstanceOf(Api\PaymentsApiInterface::class); + } + + public function if_gets_products_api() + { + $this->getProductsApi()->shouldReturnAnInstanceOf(Api\ProductsApiInterface::class); + } + + public function if_gets_productAttributes_api() + { + $this->getProductAttributesApi()->shouldReturnAnInstanceOf(Api\ProductAttributesApiInterface::class); + } + + public function if_gets_productAssociationTypes_api() + { + $this->getProductAssociationTypesApi()->shouldReturnAnInstanceOf(Api\ProductAssociationTypesApiInterface::class); + } + + public function if_gets_productOptions_api() + { + $this->getProductOptionsApi()->shouldReturnAnInstanceOf(Api\ProductOptionsApiInterface::class); + } + + public function if_gets_productReviews_api() + { + $this->getProductReviewsApi()->shouldReturnAnInstanceOf(Api\ProductReviewsApiInterface::class); + } + + public function if_gets_productVariants_api() + { + $this->getProductVariantsApi()->shouldReturnAnInstanceOf(Api\ProductVariantsApiInterface::class); + } + + public function if_gets_promotions_api() + { + $this->getPromotionsApi()->shouldReturnAnInstanceOf(Api\PromotionsApiInterface::class); + } + + public function if_gets_promotionCoupons_api() + { + $this->getPromotionCouponsApi()->shouldReturnAnInstanceOf(Api\PromotionCouponsApiInterface::class); + } + + public function if_gets_shipments_api() + { + $this->getShipmentsApi()->shouldReturnAnInstanceOf(Api\ShipmentsApiInterface::class); + } + + public function if_gets_shippingCategories_api() + { + $this->getShippingCategoriesApi()->shouldReturnAnInstanceOf(Api\ShippingCategoriesApiInterface::class); + } + + public function if_gets_taxCategories_api() + { + $this->getTaxCategoriesApi()->shouldReturnAnInstanceOf(Api\TaxCategoriesApiInterface::class); + } + + public function if_gets_taxRates_api() + { + $this->getTaxRatesApi()->shouldReturnAnInstanceOf(Api\TaxRatesApiInterface::class); + } + + public function if_gets_taxons_api() + { + $this->getTaxonsApi()->shouldReturnAnInstanceOf(Api\TaxonsApiInterface::class); + } + + public function if_gets_users_api() + { + $this->getUsersApi()->shouldReturnAnInstanceOf(Api\UsersApiInterface::class); + } + + public function if_gets_zones_api() + { + $this->getZonesApi()->shouldReturnAnInstanceOf(Api\ZonesApiInterface::class); } } diff --git a/src/SyliusClient.php b/src/SyliusClient.php index 7958989..aba994d 100644 --- a/src/SyliusClient.php +++ b/src/SyliusClient.php @@ -11,31 +11,7 @@ namespace Diglin\Sylius\ApiClient; -use Diglin\Sylius\ApiClient\Api\CartsApiInterface; -use Diglin\Sylius\ApiClient\Api\ChannelsApiInterface; -use Diglin\Sylius\ApiClient\Api\CountriesApiInterface; -use Diglin\Sylius\ApiClient\Api\CurrenciesApiInterface; -use Diglin\Sylius\ApiClient\Api\CustomersApiInterface; -use Diglin\Sylius\ApiClient\Api\ExchangeRatesApiInterface; -use Diglin\Sylius\ApiClient\Api\LocalesApiInterface; -use Diglin\Sylius\ApiClient\Api\OrdersApiInterface; -use Diglin\Sylius\ApiClient\Api\PaymentMethodsApiInterface; -use Diglin\Sylius\ApiClient\Api\PaymentsApiInterface; -use Diglin\Sylius\ApiClient\Api\ProductAssociationTypesApiInterface; -use Diglin\Sylius\ApiClient\Api\ProductAttributesApiInterface; -use Diglin\Sylius\ApiClient\Api\ProductOptionsApiInterface; -use Diglin\Sylius\ApiClient\Api\ProductReviewsApiInterface; -use Diglin\Sylius\ApiClient\Api\ProductsApiInterface; -use Diglin\Sylius\ApiClient\Api\ProductVariantsApiInterface; -use Diglin\Sylius\ApiClient\Api\PromotionCouponsApiInterface; -use Diglin\Sylius\ApiClient\Api\PromotionsApiInterface; -use Diglin\Sylius\ApiClient\Api\ShipmentsApiInterface; -use Diglin\Sylius\ApiClient\Api\ShippingCategoriesApi; -use Diglin\Sylius\ApiClient\Api\TaxCategoriesApiInterface; -use Diglin\Sylius\ApiClient\Api\TaxonsApiInterface; -use Diglin\Sylius\ApiClient\Api\TaxRatesApiInterface; -use Diglin\Sylius\ApiClient\Api\UsersApiInterface; -use Diglin\Sylius\ApiClient\Api\ZonesApiInterface; +use Diglin\Sylius\ApiClient\Api; use Diglin\Sylius\ApiClient\Security\Authentication; class SyliusClient implements SyliusClientInterface @@ -43,84 +19,84 @@ class SyliusClient implements SyliusClientInterface /** @var Authentication */ private $authentication; - /** @var ChannelsApiInterface */ + /** @var Api\ChannelsApiInterface */ private $channelsApi; - /** @var UsersApiInterface */ + /** @var Api\UsersApiInterface */ private $usersApi; - /** @var CustomersApiInterface */ + /** @var Api\CustomersApiInterface */ private $customersApi; - /** @var ProductsApiInterface */ + /** @var Api\ProductsApiInterface */ private $productsApi; - /** @var CartsApiInterface */ + /** @var Api\CartsApiInterface */ private $cartsApi; - /** @var OrdersApiInterface */ + /** @var Api\OrdersApiInterface */ private $ordersApi; - /** @var PaymentsApiInterface */ + /** @var Api\PaymentsApiInterface */ private $paymentsApi; - /** @var ShipmentsApiInterface */ + /** @var Api\ShipmentsApiInterface */ private $shipmentsApi; - /** @var ShippingCategoriesApi */ + /** @var Api\ShippingCategoriesApiInterface */ private $shippingCategoriesApi; - /** @var LocalesApiInterface */ + /** @var Api\LocalesApiInterface */ private $localesApi; - /** @var CurrenciesApiInterface */ + /** @var Api\CurrenciesApiInterface */ private $currenciesApi; - /** @var CountriesApiInterface */ + /** @var Api\CountriesApiInterface */ private $countriesApi; - /** @var ExchangeRatesApiInterface */ + /** @var Api\ExchangeRatesApiInterface */ private $exchangeRatesApi; - /** @var PaymentMethodsApiInterface */ + /** @var Api\PaymentMethodsApiInterface */ private $paymentMethodsApi; - /** @var ProductAttributesApiInterface */ + /** @var Api\ProductAttributesApiInterface */ private $productAttributesApi; - /** @var ProductAssociationTypesApiInterface */ + /** @var Api\ProductAssociationTypesApiInterface */ private $productAssociationTypesApi; - /** @var ProductOptionsApiInterface */ + /** @var Api\ProductOptionsApiInterface */ private $productOptionsApi; - /** @var ProductReviewsApiInterface */ + /** @var Api\ProductReviewsApiInterface */ private $productReviewsApi; - /** @var ProductVariantsApiInterface */ + /** @var Api\ProductVariantsApiInterface */ private $productVariantsApi; - /** @var PromotionsApiInterface */ + /** @var Api\PromotionsApiInterface */ private $promotionsApi; - /** @var PromotionCouponsApiInterface */ + /** @var Api\PromotionCouponsApiInterface */ private $promotionCouponsApi; - /** @var TaxCategoriesApiInterface */ + /** @var Api\TaxCategoriesApiInterface */ private $taxCategoriesApi; - /** @var TaxRatesApiInterface */ + /** @var Api\TaxRatesApiInterface */ private $taxRatesApi; - /** @var TaxonsApiInterface */ + /** @var Api\TaxonsApiInterface */ private $taxonsApi; - /** @var ZonesApiInterface */ + /** @var Api\ZonesApiInterface */ private $zonesApi; public function __construct( Authentication $authentication, - CartsApiInterface $cartsApi, - ChannelsApiInterface $channelsApi, - CountriesApiInterface $countriesApi, - CurrenciesApiInterface $currenciesApi, - CustomersApiInterface $customersApi, - ExchangeRatesApiInterface $exchangeRatesApi, - LocalesApiInterface $localesApi, - OrdersApiInterface $ordersApi, - PaymentMethodsApiInterface $paymentMethodApi, - PaymentsApiInterface $paymentsApi, - ProductsApiInterface $productsApi, - ProductAttributesApiInterface $productAttributesApi, - ProductAssociationTypesApiInterface $productAssociationTypesApi, - ProductOptionsApiInterface $productOptionsApi, - ProductReviewsApiInterface $productReviewsApi, - ProductVariantsApiInterface $productVariantsApi, - PromotionsApiInterface $promotionsApi, - PromotionCouponsApiInterface $promotionCouponsApi, - ShipmentsApiInterface $shipmentsApi, - ShippingCategoriesApi $shippingCategoriesApi, - TaxCategoriesApiInterface $taxCategoriesApi, - TaxRatesApiInterface $taxRatesApi, - TaxonsApiInterface $taxonsApi, - UsersApiInterface $usersApi, - ZonesApiInterface $zonesApi + Api\CartsApiInterface $cartsApi, + Api\ChannelsApiInterface $channelsApi, + Api\CountriesApiInterface $countriesApi, + Api\CurrenciesApiInterface $currenciesApi, + Api\CustomersApiInterface $customersApi, + Api\ExchangeRatesApiInterface $exchangeRatesApi, + Api\LocalesApiInterface $localesApi, + Api\OrdersApiInterface $ordersApi, + Api\PaymentMethodsApiInterface $paymentMethodApi, + Api\PaymentsApiInterface $paymentsApi, + Api\ProductsApiInterface $productsApi, + Api\ProductAttributesApiInterface $productAttributesApi, + Api\ProductAssociationTypesApiInterface $productAssociationTypesApi, + Api\ProductOptionsApiInterface $productOptionsApi, + Api\ProductReviewsApiInterface $productReviewsApi, + Api\ProductVariantsApiInterface $productVariantsApi, + Api\PromotionsApiInterface $promotionsApi, + Api\PromotionCouponsApiInterface $promotionCouponsApi, + Api\ShipmentsApiInterface $shipmentsApi, + Api\ShippingCategoriesApiInterface $shippingCategoriesApi, + Api\TaxCategoriesApiInterface $taxCategoriesApi, + Api\TaxRatesApiInterface $taxRatesApi, + Api\TaxonsApiInterface $taxonsApi, + Api\UsersApiInterface $usersApi, + Api\ZonesApiInterface $zonesApi ) { $this->authentication = $authentication; $this->channelsApi = $channelsApi; @@ -150,122 +126,122 @@ public function __construct( $this->zonesApi = $zonesApi; } - public function getZonesApi(): ZonesApiInterface + public function getZonesApi(): Api\ZonesApiInterface { return $this->zonesApi; } - public function getTaxonsApi(): TaxonsApiInterface + public function getTaxonsApi(): Api\TaxonsApiInterface { return $this->taxonsApi; } - public function getTaxRatesApi(): TaxRatesApiInterface + public function getTaxRatesApi(): Api\TaxRatesApiInterface { return $this->taxRatesApi; } - public function getTaxCategoriesApi(): TaxCategoriesApiInterface + public function getTaxCategoriesApi(): Api\TaxCategoriesApiInterface { return $this->taxCategoriesApi; } - public function getPromotionCouponsApi(): PromotionCouponsApiInterface + public function getPromotionCouponsApi(): Api\PromotionCouponsApiInterface { return $this->promotionCouponsApi; } - public function getPromotionsApi(): PromotionsApiInterface + public function getPromotionsApi(): Api\PromotionsApiInterface { return $this->promotionsApi; } - public function getProductVariantsApi(): ProductVariantsApiInterface + public function getProductVariantsApi(): Api\ProductVariantsApiInterface { return $this->productVariantsApi; } - public function getProductReviewsApi(): ProductReviewsApiInterface + public function getProductReviewsApi(): Api\ProductReviewsApiInterface { return $this->productReviewsApi; } - public function getProductOptionsApi(): ProductOptionsApiInterface + public function getProductOptionsApi(): Api\ProductOptionsApiInterface { return $this->productOptionsApi; } - public function getProductAssociationTypesApi(): ProductAssociationTypesApiInterface + public function getProductAssociationTypesApi(): Api\ProductAssociationTypesApiInterface { return $this->productAssociationTypesApi; } - public function getProductAttributesApi(): ProductAttributesApiInterface + public function getProductAttributesApi(): Api\ProductAttributesApiInterface { return $this->productAttributesApi; } - public function getShippingCategoriesApi(): ShippingCategoriesApi + public function getShippingCategoriesApi(): Api\ShippingCategoriesApi { return $this->shippingCategoriesApi; } - public function getLocalesApi(): LocalesApiInterface + public function getLocalesApi(): Api\LocalesApiInterface { return $this->localesApi; } - public function getCurrenciesApi(): CurrenciesApiInterface + public function getCurrenciesApi(): Api\CurrenciesApiInterface { return $this->currenciesApi; } - public function getCountriesApi(): CountriesApiInterface + public function getCountriesApi(): Api\CountriesApiInterface { return $this->countriesApi; } - public function getExchangeRatesApi(): ExchangeRatesApiInterface + public function getExchangeRatesApi(): Api\ExchangeRatesApiInterface { return $this->exchangeRatesApi; } - public function getPaymentMethodsApi(): PaymentMethodsApiInterface + public function getPaymentMethodsApi(): Api\PaymentMethodsApiInterface { return $this->paymentMethodsApi; } - public function getUsersApi(): UsersApiInterface + public function getUsersApi(): Api\UsersApiInterface { return $this->usersApi; } - public function getCustomersApi(): CustomersApiInterface + public function getCustomersApi(): Api\CustomersApiInterface { return $this->customersApi; } - public function getProductsApi(): ProductsApiInterface + public function getProductsApi(): Api\ProductsApiInterface { return $this->productsApi; } - public function getCartsApi(): CartsApiInterface + public function getCartsApi(): Api\CartsApiInterface { return $this->cartsApi; } - public function getOrdersApi(): OrdersApiInterface + public function getOrdersApi(): Api\OrdersApiInterface { return $this->ordersApi; } - public function getPaymentsApi(): PaymentsApiInterface + public function getPaymentsApi(): Api\PaymentsApiInterface { return $this->paymentsApi; } - public function getShipmentsApi(): ShipmentsApiInterface + public function getShipmentsApi(): Api\ShipmentsApiInterface { return $this->shipmentsApi; } @@ -280,7 +256,7 @@ public function getRefreshToken(): ?string return $this->authentication->getRefreshToken(); } - public function getChannelsApi(): ChannelsApiInterface + public function getChannelsApi(): Api\ChannelsApiInterface { return $this->channelsApi; } diff --git a/src/SyliusClientBuilder.php b/src/SyliusClientBuilder.php index 07c70a4..6577b11 100644 --- a/src/SyliusClientBuilder.php +++ b/src/SyliusClientBuilder.php @@ -11,33 +11,7 @@ namespace Diglin\Sylius\ApiClient; -use Diglin\Sylius\ApiClient\Api\ApiAwareInterface; -use Diglin\Sylius\ApiClient\Api\AuthenticationApi; -use Diglin\Sylius\ApiClient\Api\CartsApi; -use Diglin\Sylius\ApiClient\Api\ChannelsApi; -use Diglin\Sylius\ApiClient\Api\CountriesApi; -use Diglin\Sylius\ApiClient\Api\CurrenciesApi; -use Diglin\Sylius\ApiClient\Api\CustomersApi; -use Diglin\Sylius\ApiClient\Api\ExchangeRatesApi; -use Diglin\Sylius\ApiClient\Api\LocalesApi; -use Diglin\Sylius\ApiClient\Api\OrdersApi; -use Diglin\Sylius\ApiClient\Api\PaymentMethodsApi; -use Diglin\Sylius\ApiClient\Api\PaymentsApi; -use Diglin\Sylius\ApiClient\Api\ProductAssociationTypesApi; -use Diglin\Sylius\ApiClient\Api\ProductAttributesApi; -use Diglin\Sylius\ApiClient\Api\ProductOptionsApi; -use Diglin\Sylius\ApiClient\Api\ProductReviewsApi; -use Diglin\Sylius\ApiClient\Api\ProductsApi; -use Diglin\Sylius\ApiClient\Api\ProductVariantsApi; -use Diglin\Sylius\ApiClient\Api\PromotionCouponsApi; -use Diglin\Sylius\ApiClient\Api\PromotionsApi; -use Diglin\Sylius\ApiClient\Api\ShipmentsApi; -use Diglin\Sylius\ApiClient\Api\ShippingCategoriesApi; -use Diglin\Sylius\ApiClient\Api\TaxCategoriesApi; -use Diglin\Sylius\ApiClient\Api\TaxonsApi; -use Diglin\Sylius\ApiClient\Api\TaxRatesApi; -use Diglin\Sylius\ApiClient\Api\UsersApi; -use Diglin\Sylius\ApiClient\Api\ZonesApi; +use Diglin\Sylius\ApiClient\Api; use Diglin\Sylius\ApiClient\Client\AuthenticatedHttpClient; use Diglin\Sylius\ApiClient\Client\HttpClient; use Diglin\Sylius\ApiClient\Client\ResourceClient; @@ -83,13 +57,13 @@ class SyliusClientBuilder implements SyliusClientBuilderInterface /** @var FileSystemInterface */ protected $fileSystem; - /** @var ApiAwareInterface[] */ + /** @var Api\ApiAwareInterface[] */ protected $apiRegistry = []; /** @var array */ protected $defaultHeaders = []; - public function __construct(?ApiAwareInterface ...$apis) + public function __construct(?Api\ApiAwareInterface ...$apis) { foreach ($apis as $api) { $this->addApi($api); @@ -104,7 +78,7 @@ protected function setUp(Authentication $authentication) $uriGenerator = new UriGenerator($this->baseUri); $httpClient = new HttpClient($this->getHttpClient(), $this->getRequestFactory(), $this->getStreamFactory(), $this->defaultHeaders); - $authenticationApi = new AuthenticationApi($httpClient, $uriGenerator); + $authenticationApi = new Api\AuthenticationApi($httpClient, $uriGenerator); $authenticatedHttpClient = new AuthenticatedHttpClient($httpClient, $authenticationApi, $authentication); $multipartStreamBuilderFactory = new MultipartStreamBuilderFactory($this->getStreamFactory()); $upsertListResponseFactory = new UpsertResourceListResponseFactory(); @@ -123,43 +97,55 @@ protected function setUp(Authentication $authentication) return [$resourceClient, $pageFactory, $cursorFactory, $fileSystem]; } - public function addApi(ApiAwareInterface $api) + public function addApi(Api\ApiAwareInterface $api): self { $this->apiRegistry[(new \ReflectionClass($api))->getShortName()] = $api; + + return $this; } - public function setBaseUri(string $baseUri) + public function setBaseUri(string $baseUri): SyliusClientBuilderInterface { $this->baseUri = $baseUri; + + return $this; } - public function setDefaultHeaders(array $headers) + public function setDefaultHeaders(array $headers): SyliusClientBuilderInterface { $this->defaultHeaders = $headers; + + return $this; } /** * Allows to directly set a client instead of using HttpClientDiscovery::find(). */ - public function setHttpClient(Client $httpClient) + public function setHttpClient(Client $httpClient): SyliusClientBuilderInterface { $this->httpClient = $httpClient; + + return $this; } /** * Allows to directly set a request factory instead of using MessageFactoryDiscovery::find(). */ - public function setRequestFactory(RequestFactory $requestFactory) + public function setRequestFactory(RequestFactory $requestFactory): SyliusClientBuilderInterface { $this->requestFactory = $requestFactory; + + return $this; } /** * Allows to directly set a stream factory instead of using StreamFactoryDiscovery::find(). */ - public function setStreamFactory(StreamFactory $streamFactory) + public function setStreamFactory(StreamFactory $streamFactory): SyliusClientBuilderInterface { $this->streamFactory = $streamFactory; + + return $this; } /** @@ -207,31 +193,31 @@ private function buildAuthenticatedClient(Authentication $authentication): Syliu $client = new SyliusClientDecorator( new SyliusClient( $authentication, - new CartsApi($resourceClient, $pageFactory, $cursorFactory), - new ChannelsApi($resourceClient, $pageFactory, $cursorFactory), - new CountriesApi($resourceClient, $pageFactory, $cursorFactory), - new CurrenciesApi($resourceClient, $pageFactory, $cursorFactory), - new CustomersApi($resourceClient, $pageFactory, $cursorFactory), - new ExchangeRatesApi($resourceClient, $pageFactory, $cursorFactory), - new LocalesApi($resourceClient, $pageFactory, $cursorFactory), - new OrdersApi($resourceClient, $pageFactory, $cursorFactory), - new PaymentMethodsApi($resourceClient, $pageFactory, $cursorFactory), - new PaymentsApi($resourceClient, $pageFactory, $cursorFactory), - new ProductsApi($resourceClient, $pageFactory, $cursorFactory), - new ProductAttributesApi($resourceClient, $pageFactory, $cursorFactory), - new ProductAssociationTypesApi($resourceClient, $pageFactory, $cursorFactory), - new ProductOptionsApi($resourceClient, $pageFactory, $cursorFactory), - new ProductReviewsApi($resourceClient, $pageFactory, $cursorFactory), - new ProductVariantsApi($resourceClient, $pageFactory, $cursorFactory), - new PromotionsApi($resourceClient, $pageFactory, $cursorFactory), - new PromotionCouponsApi($resourceClient, $pageFactory, $cursorFactory), - new ShipmentsApi($resourceClient, $pageFactory, $cursorFactory), - new ShippingCategoriesApi($resourceClient, $pageFactory, $cursorFactory), - new TaxCategoriesApi($resourceClient, $pageFactory, $cursorFactory), - new TaxRatesApi($resourceClient, $pageFactory, $cursorFactory), - new TaxonsApi($resourceClient, $pageFactory, $cursorFactory), - new UsersApi($resourceClient, $pageFactory, $cursorFactory), - new ZonesApi($resourceClient, $pageFactory, $cursorFactory) + new Api\CartsApi($resourceClient, $pageFactory, $cursorFactory), + new Api\ChannelsApi($resourceClient, $pageFactory, $cursorFactory), + new Api\CountriesApi($resourceClient, $pageFactory, $cursorFactory), + new Api\CurrenciesApi($resourceClient, $pageFactory, $cursorFactory), + new Api\CustomersApi($resourceClient, $pageFactory, $cursorFactory), + new Api\ExchangeRatesApi($resourceClient, $pageFactory, $cursorFactory), + new Api\LocalesApi($resourceClient, $pageFactory, $cursorFactory), + new Api\OrdersApi($resourceClient, $pageFactory, $cursorFactory), + new Api\PaymentMethodsApi($resourceClient, $pageFactory, $cursorFactory), + new Api\PaymentsApi($resourceClient, $pageFactory, $cursorFactory), + new Api\ProductsApi($resourceClient, $pageFactory, $cursorFactory), + new Api\ProductAttributesApi($resourceClient, $pageFactory, $cursorFactory), + new Api\ProductAssociationTypesApi($resourceClient, $pageFactory, $cursorFactory), + new Api\ProductOptionsApi($resourceClient, $pageFactory, $cursorFactory), + new Api\ProductReviewsApi($resourceClient, $pageFactory, $cursorFactory), + new Api\ProductVariantsApi($resourceClient, $pageFactory, $cursorFactory), + new Api\PromotionsApi($resourceClient, $pageFactory, $cursorFactory), + new Api\PromotionCouponsApi($resourceClient, $pageFactory, $cursorFactory), + new Api\ShipmentsApi($resourceClient, $pageFactory, $cursorFactory), + new Api\ShippingCategoriesApi($resourceClient, $pageFactory, $cursorFactory), + new Api\TaxCategoriesApi($resourceClient, $pageFactory, $cursorFactory), + new Api\TaxRatesApi($resourceClient, $pageFactory, $cursorFactory), + new Api\TaxonsApi($resourceClient, $pageFactory, $cursorFactory), + new Api\UsersApi($resourceClient, $pageFactory, $cursorFactory), + new Api\ZonesApi($resourceClient, $pageFactory, $cursorFactory) ) ); diff --git a/src/SyliusClientBuilderInterface.php b/src/SyliusClientBuilderInterface.php index 39b2b2c..ce62942 100644 --- a/src/SyliusClientBuilderInterface.php +++ b/src/SyliusClientBuilderInterface.php @@ -15,15 +15,15 @@ interface SyliusClientBuilderInterface { - public function setBaseUri(string $baseUri); + public function setBaseUri(string $baseUri): self; - public function setDefaultHeaders(array $headers); + public function setDefaultHeaders(array $headers): self; - public function setHttpClient(Client $httpClient); + public function setHttpClient(Client $httpClient): self; - public function setRequestFactory(RequestFactory $requestFactory); + public function setRequestFactory(RequestFactory $requestFactory): self; - public function setStreamFactory(StreamFactory $streamFactory); + public function setStreamFactory(StreamFactory $streamFactory): self; public function buildAuthenticatedByPassword( string $clientId,