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\Model\Operation; |
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 implode; |
21
|
|
|
use function is_array; |
22
|
|
|
|
23
|
|
|
final class ListOperation implements ListOperationInterface |
24
|
|
|
{ |
25
|
|
|
private RequestBuilder $requestBuilder; |
26
|
|
|
|
27
|
|
|
private DataObjectFactory $dataObjectFactory; |
28
|
|
|
|
29
|
|
|
private string $entityType; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string[] |
33
|
|
|
*/ |
34
|
|
|
private array $arguments; |
35
|
|
|
|
36
|
|
|
public function __construct( |
37
|
|
|
RequestBuilder $requestBuilder, |
38
|
|
|
DataObjectFactory $dataObjectFactory, |
39
|
|
|
string $entityType, |
40
|
|
|
array $arguments = [] |
41
|
|
|
) { |
42
|
|
|
$this->requestBuilder = $requestBuilder; |
43
|
|
|
$this->dataObjectFactory = $dataObjectFactory; |
44
|
|
|
$this->entityType = $entityType; |
45
|
|
|
$this->arguments = $arguments; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getByIds(array $entityIds): array |
49
|
|
|
{ |
50
|
|
|
try { |
51
|
|
|
$response = $this->fetchResult( |
52
|
|
|
array_merge([$this->entityType . 'ByIds'], $this->arguments), |
53
|
|
|
['ids' => implode(',', $entityIds)] |
54
|
|
|
); |
55
|
|
|
} catch (InvalidArgumentException $e) { |
56
|
|
|
throw new CouldNotReadException($e->getMessage(), $e->getCode(), $e); |
57
|
|
|
} catch (InvalidRequestException $e) { |
58
|
|
|
throw new CouldNotReadException($e->getMessage(), $e->getCode(), $e); |
59
|
|
|
} catch (Exception $e) { |
60
|
|
|
throw new CouldNotReadException('Could not fetch the entity.', $e->getCode(), $e); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->buildEntities($response); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getList(ListCriteriaInterface $listCriteria): array |
67
|
|
|
{ |
68
|
|
|
$arguments = $listCriteria->getFilters() ? array_merge(['search'], $this->arguments) : $this->arguments; |
69
|
|
|
|
70
|
|
|
try { |
71
|
|
|
$response = $this->fetchResult($arguments, $listCriteria->getQueryParams()); |
72
|
|
|
} catch (InvalidArgumentException $e) { |
73
|
|
|
throw new CouldNotReadException($e->getMessage(), $e->getCode(), $e); |
74
|
|
|
} catch (InvalidRequestException $e) { |
75
|
|
|
throw new CouldNotReadException($e->getMessage(), $e->getCode(), $e); |
76
|
|
|
} catch (Exception $e) { |
77
|
|
|
throw new CouldNotReadException('Could not fetch the entity.', $e->getCode(), $e); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->buildEntities($response); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param ResponseInterface $response |
85
|
|
|
* @return DataObjectInterface[] |
86
|
|
|
*/ |
87
|
|
|
private function buildEntities(ResponseInterface $response): array |
88
|
|
|
{ |
89
|
|
|
$entities = []; |
90
|
|
|
$result = $response->getResult(); |
91
|
|
|
if (isset($result['data']) && is_array($result['data'])) { |
92
|
|
|
foreach ($result['data'] as $entity) { |
93
|
|
|
$entities[] = $this->dataObjectFactory->create($this->entityType, $entity); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $entities; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string[] $arguments |
102
|
|
|
* @param string[] $params |
103
|
|
|
* @return ResponseInterface |
104
|
|
|
* @throws Exception |
105
|
|
|
* @throws InvalidArgumentException |
106
|
|
|
* @throws InvalidRequestException |
107
|
|
|
*/ |
108
|
|
|
private function fetchResult(array $arguments, array $params = []): ResponseInterface |
109
|
|
|
{ |
110
|
|
|
return $this->requestBuilder |
111
|
|
|
->setEntityType($this->entityType) |
112
|
|
|
->setMethod(RequestBuilder::HTTP_GET) |
113
|
|
|
->setArguments($arguments) |
114
|
|
|
->setQueryParameters($params) |
115
|
|
|
->create() |
116
|
|
|
->execute(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|