diff --git a/src/Api/Order/OrderOperation.php b/src/Api/Order/OrderOperation.php index ebf696d..d4789b7 100644 --- a/src/Api/Order/OrderOperation.php +++ b/src/Api/Order/OrderOperation.php @@ -1,23 +1,26 @@ addOperation( + $reference, + $channelName, + self::TYPE_UPLOAD_DOCUMENTS, + ['documents' => $collection->getDocuments()] + ); + + return $this; + } + /** * Execute all declared operations * @@ -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 * diff --git a/src/Api/Order/UploadOrderDocument.php b/src/Api/Order/UploadOrderDocument.php new file mode 100644 index 0000000..61f5bc2 --- /dev/null +++ b/src/Api/Order/UploadOrderDocument.php @@ -0,0 +1,54 @@ +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); + } +} diff --git a/src/Api/Order/UploadOrderDocumentCollection.php b/src/Api/Order/UploadOrderDocumentCollection.php new file mode 100644 index 0000000..63eb477 --- /dev/null +++ b/src/Api/Order/UploadOrderDocumentCollection.php @@ -0,0 +1,19 @@ +documents[] = $document; + } + + public function getDocuments(): array + { + return $this->documents; + } +} diff --git a/src/Client/ClientOptions.php b/src/Client/ClientOptions.php index 30a5f94..47572fe 100644 --- a/src/Client/ClientOptions.php +++ b/src/Client/ClientOptions.php @@ -9,7 +9,7 @@ class ClientOptions /** * @var bool */ - private $baseUri = 'https://api.shopping-feed.com'; + private $baseUri = 'http://api.shopping-feed.lan'; /** * @var bool diff --git a/src/Hal/HalLink.php b/src/Hal/HalLink.php index 82e3002..54529f2 100644 --- a/src/Hal/HalLink.php +++ b/src/Hal/HalLink.php @@ -302,4 +302,9 @@ private function createExceptionCallback(callable $callback = null) call_user_func($callback, $exception); }; } + + public function getHalClient() + { + return $this->client; + } } diff --git a/test_sdk.php b/test_sdk.php new file mode 100644 index 0000000..dfb15a2 --- /dev/null +++ b/test_sdk.php @@ -0,0 +1,23 @@ +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); +