Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 102 additions & 12 deletions src/Api/Order/OrderOperation.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
<?php
namespace ShoppingFeed\Sdk\Api\Order;

use GuzzleHttp\Psr7\MultipartStream;
use ShoppingFeed\Sdk\Api;
use ShoppingFeed\Sdk\Hal;
use ShoppingFeed\Sdk\Operation;
use ShoppingFeed\Sdk\Exception;
use GuzzleHttp\Psr7\Utils;

class OrderOperation extends Operation\AbstractBulkOperation
{
/**
* Operation types
*/
const TYPE_ACCEPT = 'accept';
const TYPE_CANCEL = 'cancel';
const TYPE_REFUSE = 'refuse';
const TYPE_SHIP = 'ship';
const TYPE_REFUND = 'refund';
const TYPE_ACKNOWLEDGE = 'acknowledge';
const TYPE_UNACKNOWLEDGE = 'unacknowledge';
const TYPE_ACCEPT = 'accept';
const TYPE_CANCEL = 'cancel';
const TYPE_REFUSE = 'refuse';
const TYPE_SHIP = 'ship';
const TYPE_REFUND = 'refund';
const TYPE_ACKNOWLEDGE = 'acknowledge';
const TYPE_UNACKNOWLEDGE = 'unacknowledge';
const TYPE_UPLOAD_DOCUMENTS = 'upload-documents';

/**
* @var array
Expand All @@ -30,6 +33,7 @@ class OrderOperation extends Operation\AbstractBulkOperation
self::TYPE_REFUND,
self::TYPE_ACKNOWLEDGE,
self::TYPE_UNACKNOWLEDGE,
self::TYPE_UPLOAD_DOCUMENTS,
];

/**
Expand Down Expand Up @@ -179,6 +183,22 @@ public function unacknowledge($reference, $channelName)
return $this;
}

public function uploadDocuments(
$reference,
$channelName,
UploadOrderDocumentCollection $collection
): self
{
$this->addOperation(
$reference,
$channelName,
self::TYPE_UPLOAD_DOCUMENTS,
['documents' => $collection->getDocuments()]
);

return $this;
}

/**
* Execute all declared operations
*
Expand Down Expand Up @@ -225,14 +245,84 @@ function (Hal\HalResource $batch) use ($resources) {
private function createRequestGenerator($type, Hal\HalLink $link, \ArrayAccess $requests)
{
return function (array $chunk) use ($type, $link, &$requests) {
$requests[] = $link->createRequest(
'POST',
['operation' => $type],
['order' => $chunk]
);
if ($type === self::TYPE_UPLOAD_DOCUMENTS) {
$requests[] = $this->createUploadDocumentRequest($link, $chunk);
} else {
$requests[] = $link->createRequest(
'POST',
['operation' => $type],
['order' => $chunk]
);
}
};
}

/**
* Create custom request generation callback for uploadDocument
*
* @param Hal\HalLink $link
* @param array $requests
*
* @return \Psr\Http\Message\RequestInterface
*/
private function createUploadDocumentRequest(Hal\HalLink $link, array $chunk)
{
$client = $link->getHalClient();

$files = [];
$payload = [
'order' => [],
];

foreach ($chunk as $operation) {
$reference = $operation['reference'];
$channelName = $operation['channelName'];

$documents = [];

foreach ($operation['documents'] as $document) {
/** @var UploadOrderDocument $document */
$files[] = [
'name' => 'files[]',
'contents' => fopen($document->getPath(), 'rb'),
];
$documents[] = ['type' => $document->getType()];
}

$payload['order'][] = [
'reference' => $reference,
'channelName' => $channelName,
'documents' => $documents,
];
}

$multipart = new MultipartStream(
array_merge(
$files,
[
[
'name' => 'body',
'contents' => json_encode($payload),
],
]
)
);

$request = $client->createRequest(
'POST',
$link->getUri(['operation' => self::TYPE_UPLOAD_DOCUMENTS]),
[
'Content-Type' => 'multipart/form-data',
],
Utils::streamFor($multipart)
);

$modify['set_headers']['Content-Type'] = 'multipart/form-data; boundary='
. $request->getBody()->getBoundary();

return Utils::modifyRequest($request, $modify);
}

/**
* Add operation to queue
*
Expand Down
54 changes: 54 additions & 0 deletions src/Api/Order/UploadOrderDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace ShoppingFeed\Sdk\Api\Order;

use ShoppingFeed\Sdk\Exception;

class UploadOrderDocument
{
public const INVOICE = 'invoice';

private const TYPES = [
self::INVOICE,
];

/**
* @var string
*/
private $path;

/**
* @var string
*/
private $type;

public function __construct(
string $path,
string $type
) {
if (! $this->isAllowedType($type)) {
throw new Exception\InvalidArgumentException(sprintf(
'Only %s types are accepted',
implode(', ', self::TYPES)
));
}

$this->path = $path;
$this->type = $type;
}

public function getPath(): string
{
return $this->path;
}

public function getType(): string
{
return $this->type;
}

private function isAllowedType(string $type): bool
{
return in_array($type, self::TYPES);
}
}
19 changes: 19 additions & 0 deletions src/Api/Order/UploadOrderDocumentCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace ShoppingFeed\Sdk\Api\Order;

class UploadOrderDocumentCollection
{
/** @var array */
private $documents = [];

public function addDocument(UploadOrderDocument $document): void
{
$this->documents[] = $document;
}

public function getDocuments(): array
{
return $this->documents;
}
}
2 changes: 1 addition & 1 deletion src/Client/ClientOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ClientOptions
/**
* @var bool
*/
private $baseUri = 'https://api.shopping-feed.com';
private $baseUri = 'http://api.shopping-feed.lan';

/**
* @var bool
Expand Down
5 changes: 5 additions & 0 deletions src/Hal/HalLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,9 @@ private function createExceptionCallback(callable $callback = null)
call_user_func($callback, $exception);
};
}

public function getHalClient()
{
return $this->client;
}
}
23 changes: 23 additions & 0 deletions test_sdk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace ShoppingFeed\Sdk;

use ShoppingFeed\Sdk\Api\Order\UploadOrderDocument;
use ShoppingFeed\Sdk\Api\Order\UploadOrderDocumentCollection;

$credential = new Credential\Password('login', 'password');
$session = Client\Client::createSession($credential);

$orderApi = $session->getMainStore()
->getOrderApi();

$uploadDocument = new UploadOrderDocument('/Users/kevinfrydman/Downloads/FA105411.pdf', UploadOrderDocument::INVOICE);
$collection = new UploadOrderDocumentCollection();
$collection->addDocument($uploadDocument);

$operation = new \ShoppingFeed\Sdk\Api\Order\OrderOperation();
$operation
->uploadDocuments('001-23144L16805-A', 'leroymerlin', $collection);

$orderApi->execute($operation);