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
478 Bytes
Binary file not shown.

docs/sample/code/zoho_oauth.log

Lines changed: 1 addition & 0 deletions
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

Lines changed: 18 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 76 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)