From 9ed038247163b3e9920df33c292810a7cefa2fe8 Mon Sep 17 00:00:00 2001 From: Thomas Klein Date: Sat, 11 Dec 2021 23:07:02 +0100 Subject: [PATCH 1/6] new service to handle multi entity levels --- docs/sample/code/zcrm_oauthtokens.txt | Bin 0 -> 478 bytes docs/sample/code/zoho_oauth.log | 1 + src/Client/RequestBuilder.php | 18 ++++ .../Operation/CreateOperationInterface.php | 1 + .../Operation/DeleteOperationInterface.php | 1 + .../Operation/ListOperationInterface.php | 1 + .../Operation/ReadOperationInterface.php | 1 + .../Operation/UpdateOperationInterface.php | 1 + src/Model/OperationPool.php | 3 + src/Service/CreateOperation.php | 76 ++++++++++++++ src/Service/CreateOperationInterface.php | 22 ++++ src/Service/DeleteOperation.php | 70 +++++++++++++ src/Service/DeleteOperationInterface.php | 21 ++++ src/Service/ListOperation.php | 99 ++++++++++++++++++ src/Service/ListOperationInterface.php | 23 ++++ src/Service/ReadOperation.php | 77 ++++++++++++++ src/Service/ReadOperationInterface.php | 22 ++++ src/Service/UpdateOperation.php | 87 +++++++++++++++ src/Service/UpdateOperationInterface.php | 22 ++++ 19 files changed, 546 insertions(+) create mode 100644 docs/sample/code/zcrm_oauthtokens.txt create mode 100644 docs/sample/code/zoho_oauth.log create mode 100644 src/Service/CreateOperation.php create mode 100644 src/Service/CreateOperationInterface.php create mode 100644 src/Service/DeleteOperation.php create mode 100644 src/Service/DeleteOperationInterface.php create mode 100644 src/Service/ListOperation.php create mode 100644 src/Service/ListOperationInterface.php create mode 100644 src/Service/ReadOperation.php create mode 100644 src/Service/ReadOperationInterface.php create mode 100644 src/Service/UpdateOperation.php create mode 100644 src/Service/UpdateOperationInterface.php diff --git a/docs/sample/code/zcrm_oauthtokens.txt b/docs/sample/code/zcrm_oauthtokens.txt new file mode 100644 index 0000000000000000000000000000000000000000..2e592501f9085949b4165058552b89ec5c687870 GIT binary patch literal 478 zcmb7uqbeh?2+xBrAfICVFXgHZd*Nlzq*DLP)j# zmM~e1Y@>oeS%T(XlCcEYw0uUBZvxGO2SrlPRMo~PW+~@<(8?w7tAu$?B7I-Wct4bW xR*whdata = []; } + /** + * @deprecated + */ public function setEntityType(string $entityType): self { $this->data['entityType'] = $entityType; @@ -68,6 +77,15 @@ public function setEntityType(string $entityType): self return $this; } + public function setPath(string $path, array $bind = []): self + { + $search = array_map(static function (string $variable): string { + return '{' . $variable . '}'; + }, array_keys($bind)); + + return $this->setEntityType(str_replace($search, array_values($bind), $path)); + } + public function setMethod(string $method): self { $this->data['method'] = $method; diff --git a/src/Model/Operation/CreateOperationInterface.php b/src/Model/Operation/CreateOperationInterface.php index 5bc093b..56d5867 100644 --- a/src/Model/Operation/CreateOperationInterface.php +++ b/src/Model/Operation/CreateOperationInterface.php @@ -12,6 +12,7 @@ /** * @api + * @deprecated */ interface CreateOperationInterface { diff --git a/src/Model/Operation/DeleteOperationInterface.php b/src/Model/Operation/DeleteOperationInterface.php index 509fe6a..bc5c7eb 100644 --- a/src/Model/Operation/DeleteOperationInterface.php +++ b/src/Model/Operation/DeleteOperationInterface.php @@ -11,6 +11,7 @@ /** * @api + * @deprecated */ interface DeleteOperationInterface { diff --git a/src/Model/Operation/ListOperationInterface.php b/src/Model/Operation/ListOperationInterface.php index 641c7b4..24b1d0b 100644 --- a/src/Model/Operation/ListOperationInterface.php +++ b/src/Model/Operation/ListOperationInterface.php @@ -13,6 +13,7 @@ /** * @api + * @deprecated */ interface ListOperationInterface { diff --git a/src/Model/Operation/ReadOperationInterface.php b/src/Model/Operation/ReadOperationInterface.php index db60fc4..55f38c4 100644 --- a/src/Model/Operation/ReadOperationInterface.php +++ b/src/Model/Operation/ReadOperationInterface.php @@ -12,6 +12,7 @@ /** * @api + * @deprecated */ interface ReadOperationInterface { diff --git a/src/Model/Operation/UpdateOperationInterface.php b/src/Model/Operation/UpdateOperationInterface.php index 6ad7f18..4c2b2af 100644 --- a/src/Model/Operation/UpdateOperationInterface.php +++ b/src/Model/Operation/UpdateOperationInterface.php @@ -12,6 +12,7 @@ /** * @api + * @deprecated */ interface UpdateOperationInterface { diff --git a/src/Model/OperationPool.php b/src/Model/OperationPool.php index 2545f65..421b62b 100644 --- a/src/Model/OperationPool.php +++ b/src/Model/OperationPool.php @@ -21,6 +21,9 @@ use function implode; use function md5; +/** + * @deprecated + */ final class OperationPool { private RequestBuilder $requestBuilder; diff --git a/src/Service/CreateOperation.php b/src/Service/CreateOperation.php new file mode 100644 index 0000000..81fca09 --- /dev/null +++ b/src/Service/CreateOperation.php @@ -0,0 +1,76 @@ +requestBuilder = $requestBuilder; + $this->dataObjectFactory = $dataObjectFactory; + $this->entityType = $entityType; + $this->path = $path; + $this->arguments = $arguments; + } + + public function create(DataObjectInterface $dataObject, array $bind = []): DataObjectInterface + { + try { + return $this->dataObjectFactory->create($this->entityType, $this->saveEntity($dataObject)->getResult()); + } catch (InvalidArgumentException $e) { + throw new CouldNotSaveException($e->getMessage(), $e->getCode(), $e); + } catch (InvalidRequestException $e) { + throw new CouldNotSaveException($e->getMessage(), $e->getCode(), $e); + } catch (Exception $e) { + throw new CouldNotSaveException('Could not create the entity.', $e->getCode(), $e); + } + } + + /** + * @throws Exception + * @throws InvalidArgumentException + * @throws InvalidRequestException + */ + private function saveEntity(DataObjectInterface $dataObject, array $bind = []): ResponseInterface + { + return $this->requestBuilder + ->setPath($this->path ?? $this->entityType, $bind) + ->setMethod(RequestBuilder::HTTP_POST) + ->setArguments($this->arguments) + ->setFields($dataObject->toArray()) + ->create() + ->execute(); + } +} diff --git a/src/Service/CreateOperationInterface.php b/src/Service/CreateOperationInterface.php new file mode 100644 index 0000000..adcf8f9 --- /dev/null +++ b/src/Service/CreateOperationInterface.php @@ -0,0 +1,22 @@ +requestBuilder = $requestBuilder; + $this->entityType = $entityType; + $this->path = $path; + $this->arguments = $arguments; + } + + public function delete(array $bind): void + { + try { + $this->requestBuilder + ->setPath($this->path ?? $this->entityType, $bind) + ->setMethod(RequestBuilder::HTTP_DELETE) + ->setArguments($this->path ? $this->arguments : array_merge([reset($bind)], $this->arguments)) + ->create() + ->execute(); + } catch (InvalidArgumentException $e) { + throw new CouldNotDeleteException($e->getMessage(), $e->getCode(), $e); + } catch (InvalidRequestException $e) { + throw new CouldNotDeleteException($e->getMessage(), $e->getCode(), $e); + } catch (Exception $e) { + $flatten = ''; + foreach ($bind as $key => $value) { + $flatten .= sprintf('%s: %s ', $key, $value); + } + throw new CouldNotDeleteException( + sprintf('Could not delete the entity with %s.', rtrim($flatten)), + $e->getCode(), + $e + ); + } + } +} diff --git a/src/Service/DeleteOperationInterface.php b/src/Service/DeleteOperationInterface.php new file mode 100644 index 0000000..efef99b --- /dev/null +++ b/src/Service/DeleteOperationInterface.php @@ -0,0 +1,21 @@ +requestBuilder = $requestBuilder; + $this->dataObjectFactory = $dataObjectFactory; + $this->entityType = $entityType; + $this->path = $path; + $this->arguments = $arguments; + } + + public function getList(ListCriteriaInterface $listCriteria, array $bind = []): array + { + $arguments = $listCriteria->getFilters() ? array_merge(['search'], $this->arguments) : $this->arguments; + + try { + $response = $this->fetchResult($arguments, $listCriteria->getQueryParams(), $bind); + } catch (InvalidArgumentException $e) { + throw new CouldNotReadException($e->getMessage(), $e->getCode(), $e); + } catch (InvalidRequestException $e) { + throw new CouldNotReadException($e->getMessage(), $e->getCode(), $e); + } catch (Exception $e) { + throw new CouldNotReadException('Could not fetch the entities.', $e->getCode(), $e); + } + + return $this->buildEntities($response); + } + + /** + * @return DataObjectInterface[] + */ + private function buildEntities(ResponseInterface $response): array + { + $entities = []; + $result = $response->getResult(); + if (isset($result['data']) && is_array($result['data'])) { + foreach ($result['data'] as $entity) { + $entities[] = $this->dataObjectFactory->create($this->entityType, $entity); + } + } + + return $entities; + } + + /** + * @throws Exception + * @throws InvalidArgumentException + * @throws InvalidRequestException + */ + private function fetchResult(array $arguments, array $params = [], array $bind = []): ResponseInterface + { + return $this->requestBuilder + ->setPath($this->path ?? $this->entityType, $bind) + ->setMethod(RequestBuilder::HTTP_GET) + ->setArguments($arguments) + ->setQueryParameters($params) + ->create() + ->execute(); + } +} diff --git a/src/Service/ListOperationInterface.php b/src/Service/ListOperationInterface.php new file mode 100644 index 0000000..04ce385 --- /dev/null +++ b/src/Service/ListOperationInterface.php @@ -0,0 +1,23 @@ +requestBuilder = $requestBuilder; + $this->dataObjectFactory = $dataObjectFactory; + $this->entityType = $entityType; + $this->path = $path; + $this->arguments = $arguments; + } + + public function get(array $bind): DataObjectInterface + { + try { + return $this->dataObjectFactory->create($this->entityType, $this->fetchEntity($bind)->getResult()); + } catch (InvalidArgumentException $e) { + throw new CouldNotReadException($e->getMessage(), $e->getCode(), $e); + } catch (InvalidRequestException $e) { + throw new CouldNotReadException($e->getMessage(), $e->getCode(), $e); + } catch (Exception $e) { + throw new CouldNotReadException('Could not fetch the entity.', $e->getCode(), $e); + } + } + + /** + * @throws Exception + * @throws InvalidArgumentException + * @throws InvalidRequestException + */ + private function fetchEntity(array $bind = []): ResponseInterface + { + return $this->requestBuilder + ->setPath($this->path ?? $this->entityType, $bind) + ->setMethod(RequestBuilder::HTTP_GET) + ->setArguments($this->path ? $this->arguments : array_merge([reset($bind)], $this->arguments)) + ->create() + ->execute(); + } +} diff --git a/src/Service/ReadOperationInterface.php b/src/Service/ReadOperationInterface.php new file mode 100644 index 0000000..00954f0 --- /dev/null +++ b/src/Service/ReadOperationInterface.php @@ -0,0 +1,22 @@ +requestBuilder = $requestBuilder; + $this->dataObjectFactory = $dataObjectFactory; + $this->entityType = $entityType; + $this->path = $path; + $this->arguments = $arguments; + } + + public function update(DataObjectInterface $dataObject, array $bind = []): DataObjectInterface + { + if (!$dataObject->getEntityId()) { + throw new CouldNotSaveException('Could not update an entity without ID.'); + } + + try { + return $this->dataObjectFactory->create($this->entityType, $this->saveEntity($dataObject, $bind)->getResult()); + } catch (InvalidArgumentException $e) { + throw new CouldNotSaveException($e->getMessage(), $e->getCode(), $e); + } catch (InvalidRequestException $e) { + throw new CouldNotSaveException($e->getMessage(), $e->getCode(), $e); + } catch (Exception $e) { + throw new CouldNotSaveException( + sprintf('Could not update the entity with ID "%u".', $dataObject->getEntityId()), + $e->getCode(), + $e + ); + } + } + + /** + * @throws Exception + * @throws InvalidArgumentException + * @throws InvalidRequestException + */ + private function saveEntity(DataObjectInterface $dataObject, array $bind = []): ResponseInterface + { + return $this->requestBuilder + ->setPath($this->path ?? $this->entityType, $bind) + ->setMethod(RequestBuilder::HTTP_PATCH) + ->setArguments($this->path ? $this->arguments : array_merge([reset($bind)], $this->arguments)) + ->setFields($dataObject->toArray()) + ->create() + ->execute(); + } +} diff --git a/src/Service/UpdateOperationInterface.php b/src/Service/UpdateOperationInterface.php new file mode 100644 index 0000000..bcd60b3 --- /dev/null +++ b/src/Service/UpdateOperationInterface.php @@ -0,0 +1,22 @@ + Date: Sun, 12 Dec 2021 10:29:34 +0100 Subject: [PATCH 2/6] add service factory + update facade + deprecated notice --- .gitignore | 1 + src/Client/RequestBuilder.php | 6 ++- src/Gateway.php | 21 ++++++-- src/Model/Operation/CreateOperation.php | 4 ++ .../Operation/CreateOperationInterface.php | 1 + src/Model/Operation/DeleteOperation.php | 4 ++ .../Operation/DeleteOperationInterface.php | 1 + src/Model/Operation/ListOperation.php | 4 ++ .../Operation/ListOperationInterface.php | 1 + src/Model/Operation/ReadOperation.php | 4 ++ .../Operation/ReadOperationInterface.php | 1 + src/Model/Operation/UpdateOperation.php | 4 ++ .../Operation/UpdateOperationInterface.php | 1 + src/Model/OperationPool.php | 2 + src/Service/ServiceFactory.php | 49 +++++++++++++++++++ 15 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 src/Service/ServiceFactory.php diff --git a/.gitignore b/.gitignore index c9dec9a..83c649a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/docs/sample/code/test.php /vendor/ .php_cs.cache composer.lock diff --git a/src/Client/RequestBuilder.php b/src/Client/RequestBuilder.php index 7b88407..559434e 100644 --- a/src/Client/RequestBuilder.php +++ b/src/Client/RequestBuilder.php @@ -83,7 +83,9 @@ public function setPath(string $path, array $bind = []): self return '{' . $variable . '}'; }, array_keys($bind)); - return $this->setEntityType(str_replace($search, array_values($bind), $path)); + $this->data['path'] = str_replace($search, array_values($bind), $path); + + return $this; } public function setMethod(string $method): self @@ -171,7 +173,7 @@ private function buildUrl(): string 'https://%s/%s/%s', $this->client->getApiBaseUrl(), $this->client->getApiVersion(), - $this->data['entityType'] + $this->data['path'] ?? $this->data['entityType'] ); if (isset($this->data['arguments']) && is_array($this->data['arguments'])) { diff --git a/src/Gateway.php b/src/Gateway.php index fbced11..827eba2 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -12,26 +12,30 @@ use Zoho\Desk\Model\DataObjectFactory; use Zoho\Desk\Model\OperationPool; use Zoho\Desk\OAuth\Client; +use Zoho\Desk\Service\ServiceFactory; /** * @api */ final class Gateway { + private Client $client; + + private RequestBuilder $requestBuilder; + private DataObjectFactory $dataObjectFactory; private OperationPool $operationPool; - private Client $client; - - private RequestBuilder $requestBuilder; + private ServiceFactory $serviceFactory; public function __construct(ConfigProviderInterface $configProvider, array $registeredEntityTypes = []) { - $this->dataObjectFactory = new DataObjectFactory($registeredEntityTypes); $this->client = new Client($configProvider); $this->requestBuilder = new RequestBuilder($this->client); + $this->dataObjectFactory = new DataObjectFactory($registeredEntityTypes); $this->operationPool = new OperationPool($this->requestBuilder, $this->dataObjectFactory); + $this->serviceFactory = new ServiceFactory($this->requestBuilder, $this->dataObjectFactory); } public function getDataObjectFactory(): DataObjectFactory @@ -39,11 +43,20 @@ public function getDataObjectFactory(): DataObjectFactory return $this->dataObjectFactory; } + /** + * @deprecated + * @see Gateway::getServiceFactory + */ public function getOperationPool(): OperationPool { return $this->operationPool; } + public function getServiceFactory(): ServiceFactory + { + return $this->serviceFactory; + } + public function getClient(): Client { return $this->client; diff --git a/src/Model/Operation/CreateOperation.php b/src/Model/Operation/CreateOperation.php index 132efc9..f5cbbaa 100644 --- a/src/Model/Operation/CreateOperation.php +++ b/src/Model/Operation/CreateOperation.php @@ -16,6 +16,10 @@ use Zoho\Desk\Model\DataObjectFactory; use Zoho\Desk\Model\DataObjectInterface; +/** + * @deprecated + * @see \Zoho\Desk\Service\CreateOperation + */ final class CreateOperation implements CreateOperationInterface { private RequestBuilder $requestBuilder; diff --git a/src/Model/Operation/CreateOperationInterface.php b/src/Model/Operation/CreateOperationInterface.php index 56d5867..cf3aa96 100644 --- a/src/Model/Operation/CreateOperationInterface.php +++ b/src/Model/Operation/CreateOperationInterface.php @@ -13,6 +13,7 @@ /** * @api * @deprecated + * @see \Zoho\Desk\Service\CreateOperationInterface */ interface CreateOperationInterface { diff --git a/src/Model/Operation/DeleteOperation.php b/src/Model/Operation/DeleteOperation.php index 37100fe..e5ea0c5 100644 --- a/src/Model/Operation/DeleteOperation.php +++ b/src/Model/Operation/DeleteOperation.php @@ -15,6 +15,10 @@ use function array_merge; use function sprintf; +/** + * @deprecated + * @see \Zoho\Desk\Service\DeleteOperation + */ final class DeleteOperation implements DeleteOperationInterface { private RequestBuilder $requestBuilder; diff --git a/src/Model/Operation/DeleteOperationInterface.php b/src/Model/Operation/DeleteOperationInterface.php index bc5c7eb..81adb89 100644 --- a/src/Model/Operation/DeleteOperationInterface.php +++ b/src/Model/Operation/DeleteOperationInterface.php @@ -12,6 +12,7 @@ /** * @api * @deprecated + * @see \Zoho\Desk\Service\DeleteOperationInterface */ interface DeleteOperationInterface { diff --git a/src/Model/Operation/ListOperation.php b/src/Model/Operation/ListOperation.php index 7b1f7d9..8520b6c 100644 --- a/src/Model/Operation/ListOperation.php +++ b/src/Model/Operation/ListOperation.php @@ -20,6 +20,10 @@ use function implode; use function is_array; +/** + * @deprecated + * @see \Zoho\Desk\Service\ListOperation + */ final class ListOperation implements ListOperationInterface { private RequestBuilder $requestBuilder; diff --git a/src/Model/Operation/ListOperationInterface.php b/src/Model/Operation/ListOperationInterface.php index 24b1d0b..e258cf2 100644 --- a/src/Model/Operation/ListOperationInterface.php +++ b/src/Model/Operation/ListOperationInterface.php @@ -14,6 +14,7 @@ /** * @api * @deprecated + * @see \Zoho\Desk\Service\ListOperationInterface */ interface ListOperationInterface { diff --git a/src/Model/Operation/ReadOperation.php b/src/Model/Operation/ReadOperation.php index 87a41c7..10deb49 100644 --- a/src/Model/Operation/ReadOperation.php +++ b/src/Model/Operation/ReadOperation.php @@ -17,6 +17,10 @@ use Zoho\Desk\Model\DataObjectInterface; use function array_merge; +/** + * @deprecated + * @see \Zoho\Desk\Service\ReadOperation + */ final class ReadOperation implements ReadOperationInterface { private RequestBuilder $requestBuilder; diff --git a/src/Model/Operation/ReadOperationInterface.php b/src/Model/Operation/ReadOperationInterface.php index 55f38c4..f8d319f 100644 --- a/src/Model/Operation/ReadOperationInterface.php +++ b/src/Model/Operation/ReadOperationInterface.php @@ -13,6 +13,7 @@ /** * @api * @deprecated + * @see \Zoho\Desk\Service\ReadOperationInterface */ interface ReadOperationInterface { diff --git a/src/Model/Operation/UpdateOperation.php b/src/Model/Operation/UpdateOperation.php index f0681e4..b388c49 100644 --- a/src/Model/Operation/UpdateOperation.php +++ b/src/Model/Operation/UpdateOperation.php @@ -18,6 +18,10 @@ use function array_merge; use function sprintf; +/** + * @deprecated + * @see \Zoho\Desk\Service\UpdateOperation + */ final class UpdateOperation implements UpdateOperationInterface { private RequestBuilder $requestBuilder; diff --git a/src/Model/Operation/UpdateOperationInterface.php b/src/Model/Operation/UpdateOperationInterface.php index 4c2b2af..31217aa 100644 --- a/src/Model/Operation/UpdateOperationInterface.php +++ b/src/Model/Operation/UpdateOperationInterface.php @@ -13,6 +13,7 @@ /** * @api * @deprecated + * @see \Zoho\Desk\Service\UpdateOperationInterface */ interface UpdateOperationInterface { diff --git a/src/Model/OperationPool.php b/src/Model/OperationPool.php index 421b62b..e108efe 100644 --- a/src/Model/OperationPool.php +++ b/src/Model/OperationPool.php @@ -18,11 +18,13 @@ use Zoho\Desk\Model\Operation\ReadOperationInterface; use Zoho\Desk\Model\Operation\UpdateOperation; use Zoho\Desk\Model\Operation\UpdateOperationInterface; + use function implode; use function md5; /** * @deprecated + * @see \Zoho\Desk\Service\ServiceFactory */ final class OperationPool { diff --git a/src/Service/ServiceFactory.php b/src/Service/ServiceFactory.php new file mode 100644 index 0000000..ab3c4f4 --- /dev/null +++ b/src/Service/ServiceFactory.php @@ -0,0 +1,49 @@ +requestBuilder = $requestBuilder; + $this->dataObjectFactory = $dataObjectFactory; + } + + public function createOperation(string $entityType, ?string $path, array $arguments = []): CreateOperationInterface + { + return new CreateOperation($this->requestBuilder, $this->dataObjectFactory, $entityType, $path, $arguments); + } + + public function readOperation(string $entityType, ?string $path, array $arguments = []): ReadOperationInterface + { + return new ReadOperation($this->requestBuilder, $this->dataObjectFactory, $entityType, $path, $arguments); + } + + public function updateOperation(string $entityType, ?string $path, array $arguments = []): UpdateOperationInterface + { + return new UpdateOperation($this->requestBuilder, $this->dataObjectFactory, $entityType, $path, $arguments); + } + + public function deleteOperation(string $entityType, ?string $path, array $arguments = []): DeleteOperationInterface + { + return new DeleteOperation($this->requestBuilder, $entityType, $path, $arguments); + } + + public function listOperation(string $entityType, ?string $path, array $arguments = []): ListOperationInterface + { + return new ListOperation($this->requestBuilder, $this->dataObjectFactory, $entityType, $path, $arguments); + } +} From 395add40b4a45f04b4ff2d1ef22fabe68a2b501b Mon Sep 17 00:00:00 2001 From: Thomas Klein Date: Mon, 13 Dec 2021 10:52:22 +0100 Subject: [PATCH 3/6] add nullable params --- src/Service/ServiceFactory.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Service/ServiceFactory.php b/src/Service/ServiceFactory.php index ab3c4f4..946ca2d 100644 --- a/src/Service/ServiceFactory.php +++ b/src/Service/ServiceFactory.php @@ -22,27 +22,27 @@ public function __construct(RequestBuilder $requestBuilder, DataObjectFactory $d $this->dataObjectFactory = $dataObjectFactory; } - public function createOperation(string $entityType, ?string $path, array $arguments = []): CreateOperationInterface + public function createOperation(string $entityType, ?string $path = null, array $arguments = []): CreateOperationInterface { return new CreateOperation($this->requestBuilder, $this->dataObjectFactory, $entityType, $path, $arguments); } - public function readOperation(string $entityType, ?string $path, array $arguments = []): ReadOperationInterface + public function readOperation(string $entityType, ?string $path = null, array $arguments = []): ReadOperationInterface { return new ReadOperation($this->requestBuilder, $this->dataObjectFactory, $entityType, $path, $arguments); } - public function updateOperation(string $entityType, ?string $path, array $arguments = []): UpdateOperationInterface + public function updateOperation(string $entityType, ?string $path = null, array $arguments = []): UpdateOperationInterface { return new UpdateOperation($this->requestBuilder, $this->dataObjectFactory, $entityType, $path, $arguments); } - public function deleteOperation(string $entityType, ?string $path, array $arguments = []): DeleteOperationInterface + public function deleteOperation(string $entityType, ?string $path = null, array $arguments = []): DeleteOperationInterface { return new DeleteOperation($this->requestBuilder, $entityType, $path, $arguments); } - public function listOperation(string $entityType, ?string $path, array $arguments = []): ListOperationInterface + public function listOperation(string $entityType, ?string $path = null, array $arguments = []): ListOperationInterface { return new ListOperation($this->requestBuilder, $this->dataObjectFactory, $entityType, $path, $arguments); } From 1025752ed223f8b4da28021a4612710851fa3cd3 Mon Sep 17 00:00:00 2001 From: Thomas Klein Date: Thu, 16 Dec 2021 11:26:42 +0100 Subject: [PATCH 4/6] update sample cod edoc --- docs/sample/code/index.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/docs/sample/code/index.php b/docs/sample/code/index.php index 59e6356..81f147f 100644 --- a/docs/sample/code/index.php +++ b/docs/sample/code/index.php @@ -34,6 +34,7 @@ $config = $configBuilder->create(); $gateway = new Gateway($config); +$serviceFactory = $gateway->getServiceFactory(); // Optional: if you need to register the token first // ZohoOAuth::initialize($config->get()); @@ -42,36 +43,41 @@ /** CRUD Operations **/ $ticketDataObject = $gateway->getDataObjectFactory()->create('tickets', /* Entity values */); +$threadDataObject = $gateway->getDataObjectFactory()->create('threads', /* Entity values */); try { - $ticketDataObject = $gateway->getOperationPool()->getCreateOperation('tickets')->create($ticketDataObject); + $ticketDataObject = $serviceFactory->createOperation('tickets')->create($ticketDataObject); + $threadDataObject = $serviceFactory->createOperation('threads', 'tickets/{ticket_id}/threads')->create($threadDataObject, ['ticket_id' => $ticketDataObject->getEntityId()]); } catch (CouldNotSaveException $e) { // Handle the exception... } try { - $ticketDataObject = $gateway->getOperationPool()->getReadOperation('tickets')->get(1234); + $ticketDataObject = $serviceFactory->readOperation('tickets')->get([1234]); + $threadDataObject = $serviceFactory->readOperation('threads', 'tickets/{ticket_id}/threads/{thread_id}')->get(['ticket_id' => 1234, 'thread_id' => 1234]); } catch (CouldNotReadException $e) { // Handle the exception... } try { $criteriaBuilder = new ListCriteriaBuilder(); - // $criteriaBuilder->setFields()->setFilters()... - $ticketList = $gateway->getOperationPool()->getListOperation('tickets')->getList($criteriaBuilder->create()); - $ticketList = $gateway->getOperationPool()->getListOperation('tickets')->getByIds([1,2,3]); + // $criteriaBuilder->setFields(...)->setFilters(...)... + $ticketList = $serviceFactory->listOperation('tickets')->getList($criteriaBuilder->create()); + $threadList = $serviceFactory->listOperation('threads', 'tickets/{ticket_id}/threads')->getList($criteriaBuilder->create(), ['ticket_id' => 1234]); } catch (CouldNotReadException $e) { // Handle the exception... } try { - $ticketDataObject = $gateway->getOperationPool()->getUpdateOperation('tickets')->update($ticketDataObject); + $ticketDataObject = $serviceFactory->updateOperation('tickets')->update($ticketDataObject); + $ticketDataObject = $serviceFactory->updateOperation('threads', 'tickets/{ticket_id}/threads/{thread_id}')->update($threadDataObject, ['ticket_id' => 1234, 'thread_id' => $threadDataObject->getEntityId()]); } catch (CouldNotSaveException $e) { // Handle the exception... } try { - $gateway->getOperationPool()->getDeleteOperation('tickets', ['resolution'])->delete(1234); + $serviceFactory->deleteOperation('tickets', null, ['resolution'])->delete([1234]); + $serviceFactory->deleteOperation('threads', 'tickets/{ticket_id}/threads/{thread_id}')->delete(['ticket_id' => 1234, 'thread_id' => 1234]); } catch (CouldNotDeleteException $e) { // Handle the exception... } From 4a25f55b219aa6a7f0974d1b56301d263c63d5cf Mon Sep 17 00:00:00 2001 From: Thomas Klein Date: Thu, 16 Dec 2021 14:36:18 +0100 Subject: [PATCH 5/6] Fix entityType mandatory value --- src/Client/RequestBuilder.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Client/RequestBuilder.php b/src/Client/RequestBuilder.php index 559434e..366b438 100644 --- a/src/Client/RequestBuilder.php +++ b/src/Client/RequestBuilder.php @@ -41,7 +41,7 @@ final class RequestBuilder public const HTTP_PATCH = 'PATCH'; public const HTTP_DELETE = 'DELETE'; - private const MANDATORY_FIELDS = ['entityType', 'method']; + private const MANDATORY_FIELDS = ['path', 'method']; private ClientInterface $client; @@ -72,7 +72,7 @@ public function __construct(ClientInterface $client, array $mandatoryData = []) */ public function setEntityType(string $entityType): self { - $this->data['entityType'] = $entityType; + $this->data['path'] = $entityType; return $this; } @@ -173,7 +173,7 @@ private function buildUrl(): string 'https://%s/%s/%s', $this->client->getApiBaseUrl(), $this->client->getApiVersion(), - $this->data['path'] ?? $this->data['entityType'] + $this->data['path'] ); if (isset($this->data['arguments']) && is_array($this->data['arguments'])) { From 6aa9336996f7610634c798bc3c00ef5089ca2fc9 Mon Sep 17 00:00:00 2001 From: Thomas Klein Date: Tue, 15 Mar 2022 18:46:29 +0100 Subject: [PATCH 6/6] Add query params to get operation --- src/Service/ReadOperation.php | 7 ++++--- src/Service/ReadOperationInterface.php | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Service/ReadOperation.php b/src/Service/ReadOperation.php index e36b0e6..0b9ef56 100644 --- a/src/Service/ReadOperation.php +++ b/src/Service/ReadOperation.php @@ -47,10 +47,10 @@ public function __construct( $this->arguments = $arguments; } - public function get(array $bind): DataObjectInterface + public function get(array $bind, array $query = []): DataObjectInterface { try { - return $this->dataObjectFactory->create($this->entityType, $this->fetchEntity($bind)->getResult()); + return $this->dataObjectFactory->create($this->entityType, $this->fetchEntity($bind, $query)->getResult()); } catch (InvalidArgumentException $e) { throw new CouldNotReadException($e->getMessage(), $e->getCode(), $e); } catch (InvalidRequestException $e) { @@ -65,12 +65,13 @@ public function get(array $bind): DataObjectInterface * @throws InvalidArgumentException * @throws InvalidRequestException */ - private function fetchEntity(array $bind = []): ResponseInterface + private function fetchEntity(array $bind = [], array $query = []): ResponseInterface { return $this->requestBuilder ->setPath($this->path ?? $this->entityType, $bind) ->setMethod(RequestBuilder::HTTP_GET) ->setArguments($this->path ? $this->arguments : array_merge([reset($bind)], $this->arguments)) + ->setQueryParameters($query) ->create() ->execute(); } diff --git a/src/Service/ReadOperationInterface.php b/src/Service/ReadOperationInterface.php index 00954f0..c448dc6 100644 --- a/src/Service/ReadOperationInterface.php +++ b/src/Service/ReadOperationInterface.php @@ -18,5 +18,5 @@ interface ReadOperationInterface /** * @throws CouldNotReadException */ - public function get(array $bind): DataObjectInterface; + public function get(array $bind, array $query = []): DataObjectInterface; }