-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new service to handle multi entity levels
- Loading branch information
1 parent
b9d1b29
commit 9ed0382
Showing
19 changed files
with
546 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
|
||
/** | ||
* @api | ||
* @deprecated | ||
*/ | ||
interface CreateOperationInterface | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
|
||
/** | ||
* @api | ||
* @deprecated | ||
*/ | ||
interface DeleteOperationInterface | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
|
||
/** | ||
* @api | ||
* @deprecated | ||
*/ | ||
interface ListOperationInterface | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
|
||
/** | ||
* @api | ||
* @deprecated | ||
*/ | ||
interface ReadOperationInterface | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
|
||
/** | ||
* @api | ||
* @deprecated | ||
*/ | ||
interface UpdateOperationInterface | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.