Skip to content

Commit 9ed0382

Browse files
committed
new service to handle multi entity levels
1 parent b9d1b29 commit 9ed0382

19 files changed

+546
-0
lines changed

docs/sample/code/zcrm_oauthtokens.txt

478 Bytes
Binary file not shown.

docs/sample/code/zoho_oauth.log

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2021-03-24 09:14:01 INFO: Access Token has expired. Hence refreshing.

src/Client/RequestBuilder.php

+18
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@
1212
use Zoho\Desk\Exception\Exception;
1313
use Zoho\Desk\Exception\InvalidArgumentException;
1414
use Zoho\Desk\OAuth\ClientInterface;
15+
16+
use function array_keys;
17+
use function array_map;
1518
use function array_merge;
19+
use function array_values;
1620
use function curl_init;
1721
use function curl_setopt;
1822
use function http_build_query;
1923
use function is_array;
2024
use function is_numeric;
2125
use function json_encode;
2226
use function sprintf;
27+
use function str_replace;
28+
2329
use const CURLOPT_CUSTOMREQUEST;
2430
use const CURLOPT_HEADER;
2531
use const CURLOPT_HTTPHEADER;
@@ -61,13 +67,25 @@ public function __construct(ClientInterface $client, array $mandatoryData = [])
6167
$this->data = [];
6268
}
6369

70+
/**
71+
* @deprecated
72+
*/
6473
public function setEntityType(string $entityType): self
6574
{
6675
$this->data['entityType'] = $entityType;
6776

6877
return $this;
6978
}
7079

80+
public function setPath(string $path, array $bind = []): self
81+
{
82+
$search = array_map(static function (string $variable): string {
83+
return '{' . $variable . '}';
84+
}, array_keys($bind));
85+
86+
return $this->setEntityType(str_replace($search, array_values($bind), $path));
87+
}
88+
7189
public function setMethod(string $method): self
7290
{
7391
$this->data['method'] = $method;

src/Model/Operation/CreateOperationInterface.php

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
/**
1414
* @api
15+
* @deprecated
1516
*/
1617
interface CreateOperationInterface
1718
{

src/Model/Operation/DeleteOperationInterface.php

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
/**
1313
* @api
14+
* @deprecated
1415
*/
1516
interface DeleteOperationInterface
1617
{

src/Model/Operation/ListOperationInterface.php

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
/**
1515
* @api
16+
* @deprecated
1617
*/
1718
interface ListOperationInterface
1819
{

src/Model/Operation/ReadOperationInterface.php

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
/**
1414
* @api
15+
* @deprecated
1516
*/
1617
interface ReadOperationInterface
1718
{

src/Model/Operation/UpdateOperationInterface.php

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
/**
1414
* @api
15+
* @deprecated
1516
*/
1617
interface UpdateOperationInterface
1718
{

src/Model/OperationPool.php

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
use function implode;
2222
use function md5;
2323

24+
/**
25+
* @deprecated
26+
*/
2427
final class OperationPool
2528
{
2629
private RequestBuilder $requestBuilder;

src/Service/CreateOperation.php

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* Copyright © Thomas Klein, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Zoho\Desk\Service;
9+
10+
use Zoho\Desk\Client\RequestBuilder;
11+
use Zoho\Desk\Client\ResponseInterface;
12+
use Zoho\Desk\Exception\CouldNotSaveException;
13+
use Zoho\Desk\Exception\Exception;
14+
use Zoho\Desk\Exception\InvalidArgumentException;
15+
use Zoho\Desk\Exception\InvalidRequestException;
16+
use Zoho\Desk\Model\DataObjectFactory;
17+
use Zoho\Desk\Model\DataObjectInterface;
18+
19+
final class CreateOperation implements CreateOperationInterface
20+
{
21+
private RequestBuilder $requestBuilder;
22+
23+
private DataObjectFactory $dataObjectFactory;
24+
25+
private string $entityType;
26+
27+
private ?string $path;
28+
29+
/**
30+
* @var string[]
31+
*/
32+
private array $arguments;
33+
34+
public function __construct(
35+
RequestBuilder $requestBuilder,
36+
DataObjectFactory $dataObjectFactory,
37+
string $entityType,
38+
?string $path = null,
39+
array $arguments = []
40+
) {
41+
$this->requestBuilder = $requestBuilder;
42+
$this->dataObjectFactory = $dataObjectFactory;
43+
$this->entityType = $entityType;
44+
$this->path = $path;
45+
$this->arguments = $arguments;
46+
}
47+
48+
public function create(DataObjectInterface $dataObject, array $bind = []): DataObjectInterface
49+
{
50+
try {
51+
return $this->dataObjectFactory->create($this->entityType, $this->saveEntity($dataObject)->getResult());
52+
} catch (InvalidArgumentException $e) {
53+
throw new CouldNotSaveException($e->getMessage(), $e->getCode(), $e);
54+
} catch (InvalidRequestException $e) {
55+
throw new CouldNotSaveException($e->getMessage(), $e->getCode(), $e);
56+
} catch (Exception $e) {
57+
throw new CouldNotSaveException('Could not create the entity.', $e->getCode(), $e);
58+
}
59+
}
60+
61+
/**
62+
* @throws Exception
63+
* @throws InvalidArgumentException
64+
* @throws InvalidRequestException
65+
*/
66+
private function saveEntity(DataObjectInterface $dataObject, array $bind = []): ResponseInterface
67+
{
68+
return $this->requestBuilder
69+
->setPath($this->path ?? $this->entityType, $bind)
70+
->setMethod(RequestBuilder::HTTP_POST)
71+
->setArguments($this->arguments)
72+
->setFields($dataObject->toArray())
73+
->create()
74+
->execute();
75+
}
76+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © Thomas Klein, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Zoho\Desk\Service;
9+
10+
use Zoho\Desk\Exception\CouldNotSaveException;
11+
use Zoho\Desk\Model\DataObjectInterface;
12+
13+
/**
14+
* @api
15+
*/
16+
interface CreateOperationInterface
17+
{
18+
/**
19+
* @throws CouldNotSaveException
20+
*/
21+
public function create(DataObjectInterface $dataObject, array $bind = []): DataObjectInterface;
22+
}

src/Service/DeleteOperation.php

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright © Thomas Klein, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Zoho\Desk\Service;
9+
10+
use Zoho\Desk\Client\RequestBuilder;
11+
use Zoho\Desk\Exception\CouldNotDeleteException;
12+
use Zoho\Desk\Exception\Exception;
13+
use Zoho\Desk\Exception\InvalidArgumentException;
14+
use Zoho\Desk\Exception\InvalidRequestException;
15+
use function array_merge;
16+
use function reset;
17+
use function rtrim;
18+
use function sprintf;
19+
20+
final class DeleteOperation implements DeleteOperationInterface
21+
{
22+
private RequestBuilder $requestBuilder;
23+
24+
private string $entityType;
25+
26+
private ?string $path;
27+
28+
/**
29+
* @var string[]
30+
*/
31+
private array $arguments;
32+
33+
public function __construct(
34+
RequestBuilder $requestBuilder,
35+
string $entityType,
36+
?string $path = null,
37+
array $arguments = []
38+
) {
39+
$this->requestBuilder = $requestBuilder;
40+
$this->entityType = $entityType;
41+
$this->path = $path;
42+
$this->arguments = $arguments;
43+
}
44+
45+
public function delete(array $bind): void
46+
{
47+
try {
48+
$this->requestBuilder
49+
->setPath($this->path ?? $this->entityType, $bind)
50+
->setMethod(RequestBuilder::HTTP_DELETE)
51+
->setArguments($this->path ? $this->arguments : array_merge([reset($bind)], $this->arguments))
52+
->create()
53+
->execute();
54+
} catch (InvalidArgumentException $e) {
55+
throw new CouldNotDeleteException($e->getMessage(), $e->getCode(), $e);
56+
} catch (InvalidRequestException $e) {
57+
throw new CouldNotDeleteException($e->getMessage(), $e->getCode(), $e);
58+
} catch (Exception $e) {
59+
$flatten = '';
60+
foreach ($bind as $key => $value) {
61+
$flatten .= sprintf('%s: %s ', $key, $value);
62+
}
63+
throw new CouldNotDeleteException(
64+
sprintf('Could not delete the entity with %s.', rtrim($flatten)),
65+
$e->getCode(),
66+
$e
67+
);
68+
}
69+
}
70+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Copyright © Thomas Klein, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Zoho\Desk\Service;
9+
10+
use Zoho\Desk\Exception\CouldNotDeleteException;
11+
12+
/**
13+
* @api
14+
*/
15+
interface DeleteOperationInterface
16+
{
17+
/**
18+
* @throws CouldNotDeleteException
19+
*/
20+
public function delete(array $bind): void;
21+
}

src/Service/ListOperation.php

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* Copyright © Thomas Klein, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Zoho\Desk\Service;
9+
10+
use Zoho\Desk\Client\RequestBuilder;
11+
use Zoho\Desk\Client\ResponseInterface;
12+
use Zoho\Desk\Exception\CouldNotReadException;
13+
use Zoho\Desk\Exception\Exception;
14+
use Zoho\Desk\Exception\InvalidArgumentException;
15+
use Zoho\Desk\Exception\InvalidRequestException;
16+
use Zoho\Desk\Model\DataObjectFactory;
17+
use Zoho\Desk\Model\DataObjectInterface;
18+
use Zoho\Desk\Model\ListCriteriaInterface;
19+
use function array_merge;
20+
use function is_array;
21+
22+
final class ListOperation implements ListOperationInterface
23+
{
24+
private RequestBuilder $requestBuilder;
25+
26+
private DataObjectFactory $dataObjectFactory;
27+
28+
private string $entityType;
29+
30+
private ?string $path;
31+
32+
/**
33+
* @var string[]
34+
*/
35+
private array $arguments;
36+
37+
public function __construct(
38+
RequestBuilder $requestBuilder,
39+
DataObjectFactory $dataObjectFactory,
40+
string $entityType,
41+
?string $path = null,
42+
array $arguments = []
43+
) {
44+
$this->requestBuilder = $requestBuilder;
45+
$this->dataObjectFactory = $dataObjectFactory;
46+
$this->entityType = $entityType;
47+
$this->path = $path;
48+
$this->arguments = $arguments;
49+
}
50+
51+
public function getList(ListCriteriaInterface $listCriteria, array $bind = []): array
52+
{
53+
$arguments = $listCriteria->getFilters() ? array_merge(['search'], $this->arguments) : $this->arguments;
54+
55+
try {
56+
$response = $this->fetchResult($arguments, $listCriteria->getQueryParams(), $bind);
57+
} catch (InvalidArgumentException $e) {
58+
throw new CouldNotReadException($e->getMessage(), $e->getCode(), $e);
59+
} catch (InvalidRequestException $e) {
60+
throw new CouldNotReadException($e->getMessage(), $e->getCode(), $e);
61+
} catch (Exception $e) {
62+
throw new CouldNotReadException('Could not fetch the entities.', $e->getCode(), $e);
63+
}
64+
65+
return $this->buildEntities($response);
66+
}
67+
68+
/**
69+
* @return DataObjectInterface[]
70+
*/
71+
private function buildEntities(ResponseInterface $response): array
72+
{
73+
$entities = [];
74+
$result = $response->getResult();
75+
if (isset($result['data']) && is_array($result['data'])) {
76+
foreach ($result['data'] as $entity) {
77+
$entities[] = $this->dataObjectFactory->create($this->entityType, $entity);
78+
}
79+
}
80+
81+
return $entities;
82+
}
83+
84+
/**
85+
* @throws Exception
86+
* @throws InvalidArgumentException
87+
* @throws InvalidRequestException
88+
*/
89+
private function fetchResult(array $arguments, array $params = [], array $bind = []): ResponseInterface
90+
{
91+
return $this->requestBuilder
92+
->setPath($this->path ?? $this->entityType, $bind)
93+
->setMethod(RequestBuilder::HTTP_GET)
94+
->setArguments($arguments)
95+
->setQueryParameters($params)
96+
->create()
97+
->execute();
98+
}
99+
}

0 commit comments

Comments
 (0)