Skip to content

Commit

Permalink
Implement the batch create and update endpoints for Channel Currency …
Browse files Browse the repository at this point in the history
…Assignments #53
  • Loading branch information
jswift committed Apr 13, 2021
1 parent 32a2852 commit 5d13790
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 8 deletions.
35 changes: 28 additions & 7 deletions src/BigCommerce/Api/Channels/ChannelCurrencyAssignmentsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@

namespace BigCommerce\ApiV3\Api\Channels;

use BigCommerce\ApiV3\Api\Generic\BatchCreateResource;
use BigCommerce\ApiV3\Api\Generic\BatchUpdateResource;
use BigCommerce\ApiV3\Api\Generic\CreateResource;
use BigCommerce\ApiV3\Api\Generic\DeleteResource;
use BigCommerce\ApiV3\Api\Generic\GetAllResources;
use BigCommerce\ApiV3\Api\Generic\UpdateResource;
use BigCommerce\ApiV3\Api\Generic\V3ApiBase;
use BigCommerce\ApiV3\ResourceModels\Channel\ChannelCurrencyAssignment;
use BigCommerce\ApiV3\ResponseModels\Channel\ChannelCurrencyAssignmentsResponse;
use BigCommerce\ApiV3\ResponseModels\PaginatedResponse;
use BigCommerce\ApiV3\ResponseModels\SingleResourceResponse;

/**
* Channel Currency Assignments API
*
* Example for finding all the currency assignments. Note that the parent id is effectively just a filter.
*
* ```php
* $api = new BigCommerce\ApiV3\Client($_ENV['hash'], $_ENV['CLIENT_ID'], $_ENV['ACCESS_TOKEN']);
*
* $allCurrencyAssignments = $api->channels()->currencyAssignments()->getAll()->getCurrencyAssignments();
* $channelCurrencyAssignments = $api->channel(1)->currencyAssignments()->getAll()->getCurrencyAssignments();
* ```
*/
class ChannelCurrencyAssignmentsApi extends V3ApiBase
{
Expand All @@ -24,24 +32,38 @@ class ChannelCurrencyAssignmentsApi extends V3ApiBase
use UpdateResource;
use CreateResource;
use BatchUpdateResource;

// batchcreate?
use BatchCreateResource;

private const CURRENCY_ASSIGNMENTS_ENDPOINT = 'channels/currency-assignments';
private const CURRENCY_ASSIGNMENT_ENDPOINT = 'channels/%d/currency-assignments';


public function create(): ChannelCurrencyAssignmentsResponse
public function create(ChannelCurrencyAssignment $currencyAssignment): ChannelCurrencyAssignmentsResponse
{
return new ChannelCurrencyAssignmentsResponse($this->createResource($currencyAssignment));
}

public function update(ChannelCurrencyAssignment $currencyAssignment): ChannelCurrencyAssignmentsResponse
{
return new ChannelCurrencyAssignmentsResponse($this->updateResource($currencyAssignment));
}

public function update(): ChannelCurrencyAssignmentsResponse
/**
* @param ChannelCurrencyAssignment[] $resources
* @return ChannelCurrencyAssignmentsResponse
*/
public function batchCreate(array $resources): PaginatedResponse
{
return ChannelCurrencyAssignmentsResponse::buildFromMultipleResponses($this->batchCreateResource($resources));
}

/**
* @param ChannelCurrencyAssignment[] $resources
* @return ChannelCurrencyAssignmentsResponse
*/
public function batchUpdate(array $resources): ChannelCurrencyAssignmentsResponse
{
// TODO: Implement batchUpdate() method.
return ChannelCurrencyAssignmentsResponse::buildFromMultipleResponses($this->batchUpdateResource($resources));
}

public function getAll(array $filters = [], int $page = 1, int $limit = 250): ChannelCurrencyAssignmentsResponse
Expand All @@ -64,7 +86,6 @@ public function multipleResourceUrl(): string
{
return $this->getParentResourceId() ? $this->singleResourceUrl() : $this->multipleResourcesEndpoint();
}

public function singleResourceUrl(): string
{
return sprintf(self::CURRENCY_ASSIGNMENT_ENDPOINT, $this->getParentResourceId());
Expand Down
40 changes: 40 additions & 0 deletions src/BigCommerce/Api/Generic/BatchCreateResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace BigCommerce\ApiV3\Api\Generic;

use BigCommerce\ApiV3\Client;
use BigCommerce\ApiV3\ResponseModels\PaginatedResponse;
use GuzzleHttp\RequestOptions;
use Psr\Http\Message\ResponseInterface;

trait BatchCreateResource
{
abstract public function batchCreate(array $resources): PaginatedResponse;
abstract protected function multipleResourcesEndpoint(): string;
abstract public function getClient(): Client;

protected function maxCreateBatchSize(): int
{
return 10;
}

/**
* @param array $resources
* @return ResponseInterface[]
*/
protected function batchCreateResource(array $resources): array
{
$chunks = array_chunk($resources, $this->maxCreateBatchSize());
$responses = [];
foreach ($chunks as $chunk) {
$responses[] = $this->getClient()->getRestClient()->post(
$this->multipleResourcesEndpoint(),
[
RequestOptions::JSON => $chunk,
]
);
}

return $responses;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
namespace BigCommerce\ApiV3\ResponseModels\Channel;

use BigCommerce\ApiV3\ResourceModels\Channel\ChannelCurrencyAssignment;
use BigCommerce\ApiV3\ResponseModels\PaginatedBatchableResponse;
use BigCommerce\ApiV3\ResponseModels\PaginatedResponse;

class ChannelCurrencyAssignmentsResponse extends PaginatedResponse
class ChannelCurrencyAssignmentsResponse extends PaginatedBatchableResponse
{
/**
* @return ChannelCurrencyAssignment[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace BigCommerce\Tests\Api\Channels;

use BigCommerce\ApiV3\ResourceModels\Channel\ChannelCurrencyAssignment;
use BigCommerce\Tests\BigCommerceApiTest;

class ChannelCurrencyAssignmentsApiTest extends BigCommerceApiTest
Expand All @@ -24,4 +25,64 @@ public function testCanGetAllChannelsCurrencyAssignments()
$this->assertEquals('channels/currency-assignments', $this->getLastRequestPath());
$this->assertEquals('AUD', $currencyAssignments[0]->default_currency);
}

/**
* @return ChannelCurrencyAssignment[]
*/
private function getCurrencyAssignments(): array
{
$currencyAssignments = [new ChannelCurrencyAssignment(), new ChannelCurrencyAssignment()];

$currencyAssignments[0]->channel_id = 1;
$currencyAssignments[0]->enabled_currencies = ['USD', 'AUD'];
$currencyAssignments[0]->default_currency = 'AUD';

$currencyAssignments[0]->channel_id = 2;
$currencyAssignments[0]->enabled_currencies = ['USD', 'AUD', 'NZD'];
$currencyAssignments[0]->default_currency = 'NZD';

return $currencyAssignments;
}

private function getCurrencyAssignment(): ChannelCurrencyAssignment
{
$currencyAssignment = new ChannelCurrencyAssignment();
$currencyAssignment->channel_id = 1;
$currencyAssignment->enabled_currencies = ['AUD', 'NZD', 'CAD'];
$currencyAssignment->default_currency = 'CAD';

return $currencyAssignment;
}

public function testCanCreateMultipleCurrencyAssignments()
{
$this->setReturnData('channel_currency_assignments.json');

$this->getApi()->channels()->currencyAssignments()->batchCreate($this->getCurrencyAssignments());
$this->assertEquals('channels/currency-assignments', $this->getLastRequestPath());
}

public function testCanUpdateMultipleCurrencyAssignments()
{
$this->setReturnData('channel_currency_assignments.json');

$this->getApi()->channels()->currencyAssignments()->batchUpdate($this->getCurrencyAssignments());
$this->assertEquals('channels/currency-assignments', $this->getLastRequestPath());
}

public function testCanCreateCurrencyAssignment()
{
$this->setReturnData('channel_currency_assignments.json');
$channelId = 1;
$this->getApi()->channel(1)->currencyAssignments()->create($this->getCurrencyAssignment());
$this->assertEquals("channels/$channelId/currency-assignments", $this->getLastRequestPath());
}

public function testCanUpdateCurrencyAssignment()
{
$this->setReturnData('channel_currency_assignments.json');
$channelId = 1;
$this->getApi()->channel(1)->currencyAssignments()->update($this->getCurrencyAssignment());
$this->assertEquals("channels/$channelId/currency-assignments", $this->getLastRequestPath());
}
}

0 comments on commit 5d13790

Please sign in to comment.