Skip to content

Commit

Permalink
new service to handle multi entity levels
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Dec 11, 2021
1 parent b9d1b29 commit 9ed0382
Show file tree
Hide file tree
Showing 19 changed files with 546 additions and 0 deletions.
Binary file added docs/sample/code/zcrm_oauthtokens.txt
Binary file not shown.
1 change: 1 addition & 0 deletions docs/sample/code/zoho_oauth.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2021-03-24 09:14:01 INFO: Access Token has expired. Hence refreshing.
18 changes: 18 additions & 0 deletions src/Client/RequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@
use Zoho\Desk\Exception\Exception;
use Zoho\Desk\Exception\InvalidArgumentException;
use Zoho\Desk\OAuth\ClientInterface;

use function array_keys;
use function array_map;
use function array_merge;
use function array_values;
use function curl_init;
use function curl_setopt;
use function http_build_query;
use function is_array;
use function is_numeric;
use function json_encode;
use function sprintf;
use function str_replace;

use const CURLOPT_CUSTOMREQUEST;
use const CURLOPT_HEADER;
use const CURLOPT_HTTPHEADER;
Expand Down Expand Up @@ -61,13 +67,25 @@ public function __construct(ClientInterface $client, array $mandatoryData = [])
$this->data = [];
}

/**
* @deprecated
*/
public function setEntityType(string $entityType): self
{
$this->data['entityType'] = $entityType;

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;
Expand Down
1 change: 1 addition & 0 deletions src/Model/Operation/CreateOperationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

/**
* @api
* @deprecated
*/
interface CreateOperationInterface
{
Expand Down
1 change: 1 addition & 0 deletions src/Model/Operation/DeleteOperationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

/**
* @api
* @deprecated
*/
interface DeleteOperationInterface
{
Expand Down
1 change: 1 addition & 0 deletions src/Model/Operation/ListOperationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

/**
* @api
* @deprecated
*/
interface ListOperationInterface
{
Expand Down
1 change: 1 addition & 0 deletions src/Model/Operation/ReadOperationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

/**
* @api
* @deprecated
*/
interface ReadOperationInterface
{
Expand Down
1 change: 1 addition & 0 deletions src/Model/Operation/UpdateOperationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

/**
* @api
* @deprecated
*/
interface UpdateOperationInterface
{
Expand Down
3 changes: 3 additions & 0 deletions src/Model/OperationPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
use function implode;
use function md5;

/**
* @deprecated
*/
final class OperationPool
{
private RequestBuilder $requestBuilder;
Expand Down
76 changes: 76 additions & 0 deletions src/Service/CreateOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* Copyright © Thomas Klein, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Zoho\Desk\Service;

use Zoho\Desk\Client\RequestBuilder;
use Zoho\Desk\Client\ResponseInterface;
use Zoho\Desk\Exception\CouldNotSaveException;
use Zoho\Desk\Exception\Exception;
use Zoho\Desk\Exception\InvalidArgumentException;
use Zoho\Desk\Exception\InvalidRequestException;
use Zoho\Desk\Model\DataObjectFactory;
use Zoho\Desk\Model\DataObjectInterface;

final class CreateOperation implements CreateOperationInterface
{
private RequestBuilder $requestBuilder;

private DataObjectFactory $dataObjectFactory;

private string $entityType;

private ?string $path;

/**
* @var string[]
*/
private array $arguments;

public function __construct(
RequestBuilder $requestBuilder,
DataObjectFactory $dataObjectFactory,
string $entityType,
?string $path = null,
array $arguments = []
) {
$this->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();
}
}
22 changes: 22 additions & 0 deletions src/Service/CreateOperationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Copyright © Thomas Klein, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Zoho\Desk\Service;

use Zoho\Desk\Exception\CouldNotSaveException;
use Zoho\Desk\Model\DataObjectInterface;

/**
* @api
*/
interface CreateOperationInterface
{
/**
* @throws CouldNotSaveException
*/
public function create(DataObjectInterface $dataObject, array $bind = []): DataObjectInterface;
}
70 changes: 70 additions & 0 deletions src/Service/DeleteOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Copyright © Thomas Klein, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Zoho\Desk\Service;

use Zoho\Desk\Client\RequestBuilder;
use Zoho\Desk\Exception\CouldNotDeleteException;
use Zoho\Desk\Exception\Exception;
use Zoho\Desk\Exception\InvalidArgumentException;
use Zoho\Desk\Exception\InvalidRequestException;
use function array_merge;
use function reset;
use function rtrim;
use function sprintf;

final class DeleteOperation implements DeleteOperationInterface
{
private RequestBuilder $requestBuilder;

private string $entityType;

private ?string $path;

/**
* @var string[]
*/
private array $arguments;

public function __construct(
RequestBuilder $requestBuilder,
string $entityType,
?string $path = null,
array $arguments = []
) {
$this->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
);
}
}
}
21 changes: 21 additions & 0 deletions src/Service/DeleteOperationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Copyright © Thomas Klein, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Zoho\Desk\Service;

use Zoho\Desk\Exception\CouldNotDeleteException;

/**
* @api
*/
interface DeleteOperationInterface
{
/**
* @throws CouldNotDeleteException
*/
public function delete(array $bind): void;
}
99 changes: 99 additions & 0 deletions src/Service/ListOperation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
* Copyright © Thomas Klein, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Zoho\Desk\Service;

use Zoho\Desk\Client\RequestBuilder;
use Zoho\Desk\Client\ResponseInterface;
use Zoho\Desk\Exception\CouldNotReadException;
use Zoho\Desk\Exception\Exception;
use Zoho\Desk\Exception\InvalidArgumentException;
use Zoho\Desk\Exception\InvalidRequestException;
use Zoho\Desk\Model\DataObjectFactory;
use Zoho\Desk\Model\DataObjectInterface;
use Zoho\Desk\Model\ListCriteriaInterface;
use function array_merge;
use function is_array;

final class ListOperation implements ListOperationInterface
{
private RequestBuilder $requestBuilder;

private DataObjectFactory $dataObjectFactory;

private string $entityType;

private ?string $path;

/**
* @var string[]
*/
private array $arguments;

public function __construct(
RequestBuilder $requestBuilder,
DataObjectFactory $dataObjectFactory,
string $entityType,
?string $path = null,
array $arguments = []
) {
$this->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();
}
}
Loading

0 comments on commit 9ed0382

Please sign in to comment.