From f0bce589ebc9d14864673cba202c731ae15a2b06 Mon Sep 17 00:00:00 2001 From: KevinFrydman Date: Mon, 2 Oct 2023 16:01:22 +0200 Subject: [PATCH 1/3] Add UploadOrderDocument operation to SF SDK --- src/Api/Order/OrderOperation.php | 103 ++++++++++++++++-- src/Api/Order/UploadOrderDocument.php | 54 +++++++++ .../Order/UploadOrderDocumentCollection.php | 19 ++++ src/Hal/HalLink.php | 5 + 4 files changed, 169 insertions(+), 12 deletions(-) create mode 100644 src/Api/Order/UploadOrderDocument.php create mode 100644 src/Api/Order/UploadOrderDocumentCollection.php diff --git a/src/Api/Order/OrderOperation.php b/src/Api/Order/OrderOperation.php index ebf696d5..a206ebae 100644 --- a/src/Api/Order/OrderOperation.php +++ b/src/Api/Order/OrderOperation.php @@ -11,13 +11,14 @@ 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 @@ -30,6 +31,7 @@ class OrderOperation extends Operation\AbstractBulkOperation self::TYPE_REFUND, self::TYPE_ACKNOWLEDGE, self::TYPE_UNACKNOWLEDGE, + self::TYPE_UPLOAD_DOCUMENTS, ]; /** @@ -179,6 +181,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 * @@ -225,14 +243,75 @@ 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->getPath()]; + } + + $payload['order'][] = [ + 'reference' => $reference, + 'channelName' => $channelName, + 'documents' => $documents, + ]; + } + + return $client->createRequest( + 'POST', + $link->getUri(['operation' => self::TYPE_UPLOAD_DOCUMENTS]), + [ + 'Content-Type' => 'multipart/form-data', + ], + [ + 'multipart' => [ + $files, + [ + 'name' => 'body', + 'contents' => json_encode($payload), + ], + ], + ] + ); + } + /** * Add operation to queue * diff --git a/src/Api/Order/UploadOrderDocument.php b/src/Api/Order/UploadOrderDocument.php new file mode 100644 index 00000000..61f5bc28 --- /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 00000000..63eb477c --- /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/Hal/HalLink.php b/src/Hal/HalLink.php index 82e3002f..54529f2b 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; + } } From 4994cf1065e52612155b22340dbecce7f93f2ea5 Mon Sep 17 00:00:00 2001 From: KevinFrydman Date: Wed, 18 Oct 2023 14:41:49 +0200 Subject: [PATCH 2/3] sdk call ok --- src/Api/Order/OrderOperation.php | 33 +++++++++++++++++++++----------- src/Client/ClientOptions.php | 2 +- test_sdk.php | 22 +++++++++++++++++++++ 3 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 test_sdk.php diff --git a/src/Api/Order/OrderOperation.php b/src/Api/Order/OrderOperation.php index a206ebae..d4789b72 100644 --- a/src/Api/Order/OrderOperation.php +++ b/src/Api/Order/OrderOperation.php @@ -1,10 +1,12 @@ 'files[]', 'contents' => fopen($document->getPath(), 'rb'), ]; - $documents[] = ['type' => $document->getPath()]; + $documents[] = ['type' => $document->getType()]; } $payload['order'][] = [ @@ -294,22 +296,31 @@ private function createUploadDocumentRequest(Hal\HalLink $link, array $chunk) ]; } - return $client->createRequest( + $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', ], - [ - 'multipart' => [ - $files, - [ - 'name' => 'body', - 'contents' => json_encode($payload), - ], - ], - ] + Utils::streamFor($multipart) ); + + $modify['set_headers']['Content-Type'] = 'multipart/form-data; boundary=' + . $request->getBody()->getBoundary(); + + return Utils::modifyRequest($request, $modify); } /** diff --git a/src/Client/ClientOptions.php b/src/Client/ClientOptions.php index 30a5f942..47572fef 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/test_sdk.php b/test_sdk.php new file mode 100644 index 00000000..a644434d --- /dev/null +++ b/test_sdk.php @@ -0,0 +1,22 @@ +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); From 3bb9b966f186ebbc8a4fa34cd08d2f9c560e65ef Mon Sep 17 00:00:00 2001 From: KevinFrydman Date: Wed, 15 Nov 2023 16:37:58 +0100 Subject: [PATCH 3/3] t --- test_sdk.php | 1 + 1 file changed, 1 insertion(+) diff --git a/test_sdk.php b/test_sdk.php index a644434d..dfb15a2e 100644 --- a/test_sdk.php +++ b/test_sdk.php @@ -20,3 +20,4 @@ ->uploadDocuments('001-23144L16805-A', 'leroymerlin', $collection); $orderApi->execute($operation); +