Skip to content

Commit

Permalink
add createCatalog & getAllCatalogs
Browse files Browse the repository at this point in the history
  • Loading branch information
arzzen authored Apr 17, 2023
1 parent 1561b59 commit 7a84b42
Show file tree
Hide file tree
Showing 5 changed files with 316 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/Catalog/Methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down
74 changes: 74 additions & 0 deletions src/Catalog/Partials/CreateCatalog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace belenka\ExponeaApi\Catalog\Partials;

use JsonSerializable;
use belenka\ExponeaApi\Interfaces\CreateCatalogInterface;

/**
* Representation of create catalog object
* @package belenka\ExponeaApi\Catalog\Partials
*/
class CreateCatalog implements CreateCatalogInterface, JsonSerializable
{
/**
* @var string|null
*/
protected $name;

/**
* @var boolean|null
*/
protected $isProductCatalog;

/**
* @var array
*/
protected $fields = [];

public function __construct(string $name, $isProductCatalog = false, $fields = [])
{
$this->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()
];
}
}
77 changes: 77 additions & 0 deletions src/Catalog/Response/AllCatalogs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace belenka\ExponeaApi\Catalog\Response;

use belenka\ExponeaApi\Exception\Internal\MissingResponseFieldException;

/**
* Result of getAllCatalogs() call
* @package belenka\ExponeaApi\Response
*/
class AllCatalogs
{
/**
* @var array
*/
protected $data;

/**
* @var boolean
*/
protected $success;

const FIELD_DATA = 'data';
const FIELD_SUCCESS = 'success';

/**
* AllCatalog constructor.
* @param array $data
* @throws MissingResponseFieldException
*/
public function __construct(array $data)
{
if (isset($data[self::FIELD_DATA])) {
$this->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;
}
}
77 changes: 77 additions & 0 deletions src/Catalog/Response/CreateCatalog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace belenka\ExponeaApi\Catalog\Response;

use belenka\ExponeaApi\Exception\Internal\MissingResponseFieldException;

/**
* Result of createCatalog() call
* @package belenka\ExponeaApi\Response
*/
class CreateCatalog
{
/**
* @var string
*/
protected $id;

/**
* @var boolean
*/
protected $success;

const FIELD_ID = 'id';
const FIELD_SUCCESS = 'success';

/**
* CreateCatalog constructor.
* @param array $data
* @throws MissingResponseFieldException
*/
public function __construct(array $data)
{
if (isset($data[self::FIELD_ID])) {
$this->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;
}
}
29 changes: 29 additions & 0 deletions src/Interfaces/CreateCatalogInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace belenka\ExponeaApi\Interfaces;

/**
* Interface describing create catalog for which action should be made
* @package belenka\ExponeaApi\Interfaces
*/
interface CreateCatalogInterface
{
/**
* Get catalog Name
* @return string|null
*/
public function getName();

/**
* Is product catalog
* @return boolean|null
*/
public function isProductCatalog();

/**
* Get catalog fields
* @return array|null
*/
public function getFields();

}

0 comments on commit 7a84b42

Please sign in to comment.