From 7a84b42f77ca8e303a36fc5346eec812a1b206fe Mon Sep 17 00:00:00 2001 From: arzzen Date: Mon, 17 Apr 2023 14:02:39 +0200 Subject: [PATCH] add createCatalog & getAllCatalogs --- src/Catalog/Methods.php | 59 +++++++++++++++++ src/Catalog/Partials/CreateCatalog.php | 74 ++++++++++++++++++++++ src/Catalog/Response/AllCatalogs.php | 77 +++++++++++++++++++++++ src/Catalog/Response/CreateCatalog.php | 77 +++++++++++++++++++++++ src/Interfaces/CreateCatalogInterface.php | 29 +++++++++ 5 files changed, 316 insertions(+) create mode 100644 src/Catalog/Partials/CreateCatalog.php create mode 100644 src/Catalog/Response/AllCatalogs.php create mode 100644 src/Catalog/Response/CreateCatalog.php create mode 100644 src/Interfaces/CreateCatalogInterface.php diff --git a/src/Catalog/Methods.php b/src/Catalog/Methods.php index 32cab05..e5309c7 100644 --- a/src/Catalog/Methods.php +++ b/src/Catalog/Methods.php @@ -10,10 +10,13 @@ use belenka\ExponeaApi\Exception\UnexpectedResponseSchemaException; use belenka\ExponeaApi\Interfaces\CatalogInterface; use belenka\ExponeaApi\Interfaces\CatalogItemInterface; +use belenka\ExponeaApi\Interfaces\CreateCatalogInterface; use belenka\ExponeaApi\Traits\ApiContainerTrait; use belenka\ExponeaApi\Catalog\Response\CatalogName; use belenka\ExponeaApi\Catalog\Response\CatalogItem; use belenka\ExponeaApi\Catalog\Response\CatalogItems; +use belenka\ExponeaApi\Catalog\Response\CreateCatalog; +use belenka\ExponeaApi\Catalog\Response\AllCatalogs; /** * Methods contained inside Catalog API @@ -28,6 +31,32 @@ protected function getMethodUri(string $method): string return '/data/v2/projects/' . $this->getClient()->getProjectToken() . $method; } + /** + * Get all catalogs + * + * Promise resolves to Response\AllCatalogs object + * @return PromiseInterface + */ + public function getAllCatalogs(): PromiseInterface + { + $request = new Request( + 'GET', + '/data/v2/projects/{projectToken}/catalogs' + ); + return $this->getClient()->call($request)->then(function (ResponseInterface $response) use ($request) { + try { + return new AllCatalogs(json_decode($response->getBody()->getContents(), true)); + } catch (MissingResponseFieldException $e) { + throw new UnexpectedResponseSchemaException( + $e->getMessage(), + $request, + $response, + $e + ); + } + }); + } + /** * Get catalog name * @@ -113,6 +142,36 @@ public function getCatalogItem(CatalogInterface $catalog): PromiseInterface }); } + /** + * Create catalog + * + * Promise resolves to Response\CreateCatalog object + * @param CreateCatalogInterface $catalog + * @return PromiseInterface + */ + public function createCatalog(CreateCatalogInterface $catalog): PromiseInterface + { + $request = new Request( + 'POST', + '/data/v2/projects/{projectToken}/catalogs', + [], + json_encode($catalog) ?: '{}' + ); + + return $this->getClient()->call($request)->then(function (ResponseInterface $response) use ($request) { + try { + return new CreateCatalog(json_decode($response->getBody()->getContents(), true)); + } catch (MissingResponseFieldException $e) { + throw new UnexpectedResponseSchemaException( + $e->getMessage(), + $request, + $response, + $e + ); + } + }); + } + /** * Create catalog item * diff --git a/src/Catalog/Partials/CreateCatalog.php b/src/Catalog/Partials/CreateCatalog.php new file mode 100644 index 0000000..6dbf7ab --- /dev/null +++ b/src/Catalog/Partials/CreateCatalog.php @@ -0,0 +1,74 @@ +setName($name); + $this->setIsProductCatalog($isProductCatalog); + $this->setFields($fields); + } + + public function setName(string $value): void + { + $this->name = $value; + } + + public function setIsProductCatalog($value): void + { + $this->isProductCatalog = $value; + } + + public function setFields(array $fields): void + { + $this->fields = $fields; + } + + public function getName(): int + { + return $this->name; + } + + public function isProductCatalog(): bool + { + return $this->isProductCatalog; + } + + public function getFields(): array + { + return $this->fields; + } + + public function jsonSerialize() + { + return [ + 'name' => $this->getName(), + 'is_product_catalog' => $this->isProductCatalog(), + 'fields' => $this->getFields() + ]; + } +} diff --git a/src/Catalog/Response/AllCatalogs.php b/src/Catalog/Response/AllCatalogs.php new file mode 100644 index 0000000..57f288c --- /dev/null +++ b/src/Catalog/Response/AllCatalogs.php @@ -0,0 +1,77 @@ +setData($data[self::FIELD_DATA]); + } else { + throw new MissingResponseFieldException(self::FIELD_DATA); + } + + if (isset($data[self::FIELD_SUCCESS])) { + $this->setSuccess($data[self::FIELD_SUCCESS]); + } else { + throw new MissingResponseFieldException(self::FIELD_SUCCESS); + } + } + + /** + * @param array $value + */ + public function setData(array $value): void + { + $this->data = $value; + } + + /** + * @return array + */ + public function getData(): array + { + return $this->data; + } + + /** + * @param boolean $value + */ + public function setSuccess(string $value): void + { + $this->success = $value; + } + + /** + * @return boolean + */ + public function getSuccess(): int + { + return $this->success; + } +} diff --git a/src/Catalog/Response/CreateCatalog.php b/src/Catalog/Response/CreateCatalog.php new file mode 100644 index 0000000..c5ce7af --- /dev/null +++ b/src/Catalog/Response/CreateCatalog.php @@ -0,0 +1,77 @@ +setId($data[self::FIELD_ID]); + } else { + throw new MissingResponseFieldException(self::FIELD_ID); + } + + if (isset($data[self::FIELD_SUCCESS])) { + $this->setSuccess($data[self::FIELD_SUCCESS]); + } else { + throw new MissingResponseFieldException(self::FIELD_SUCCESS); + } + } + + /** + * @param string $id + */ + public function setId(string $id): void + { + $this->id = $id; + } + + /** + * @return string + */ + public function getId(): string + { + return $this->id; + } + + /** + * @param boolean $value + */ + public function setSuccess(string $value): void + { + $this->success = $value; + } + + /** + * @return boolean + */ + public function getSuccess(): int + { + return $this->success; + } +} diff --git a/src/Interfaces/CreateCatalogInterface.php b/src/Interfaces/CreateCatalogInterface.php new file mode 100644 index 0000000..2cc4d5b --- /dev/null +++ b/src/Interfaces/CreateCatalogInterface.php @@ -0,0 +1,29 @@ +