|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* tubee.io |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com) |
|
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Tubee\Endpoint; |
|
13
|
|
|
|
|
14
|
|
|
use Generator; |
|
15
|
|
|
use GuzzleHttp\Client; |
|
16
|
|
|
use Psr\Log\LoggerInterface; |
|
17
|
|
|
use Tubee\Collection\CollectionInterface; |
|
18
|
|
|
use Tubee\Endpoint\OdataRest\QueryTransformer; |
|
19
|
|
|
use Tubee\EndpointObject\EndpointObjectInterface; |
|
20
|
|
|
use Tubee\Workflow\Factory as WorkflowFactory; |
|
21
|
|
|
|
|
22
|
|
|
class OdataRest extends AbstractRest |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Kind. |
|
26
|
|
|
*/ |
|
27
|
|
|
public const KIND = 'OdataRestEndpoint'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Init endpoint. |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(string $name, string $type, Client $client, CollectionInterface $collection, WorkflowFactory $workflow, LoggerInterface $logger, array $resource = []) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->identifier = 'id'; |
|
35
|
|
|
$this->container = 'value'; |
|
36
|
|
|
parent::__construct($name, $type, $client, $collection, $workflow, $logger, $resource); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function transformQuery(?array $query = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$result = null; |
|
45
|
|
|
|
|
46
|
|
|
if ($this->filter_all !== null) { |
|
47
|
|
|
$result = $this->filter_all; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if (!empty($query)) { |
|
51
|
|
|
if ($this->filter_all === null) { |
|
52
|
|
|
$result = QueryTransformer::transform($query); |
|
53
|
|
|
} else { |
|
54
|
|
|
$result = $this->filter_all.' and '.QueryTransformer::transform($query); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $result; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getAll(?array $query = null): Generator |
|
65
|
|
|
{ |
|
66
|
|
|
$this->logger->debug('find all balloon objects using ['.$this->client->getConfig('base_uri').']', [ |
|
67
|
|
|
'category' => get_class($this), |
|
68
|
|
|
]); |
|
69
|
|
|
|
|
70
|
|
|
$options = $this->getRequestOptions(); |
|
71
|
|
|
$query = $this->transformQuery($query); |
|
72
|
|
|
|
|
73
|
|
|
if ($query !== null) { |
|
74
|
|
|
$options['query']['$filter'] = $query; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$i = 0; |
|
78
|
|
|
$response = $this->client->get('', $options); |
|
79
|
|
|
$data = $this->getResponse($response); |
|
80
|
|
|
|
|
81
|
|
|
foreach ($data as $object) { |
|
82
|
|
|
yield $this->build($object); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $i; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getOne(array $object, ?array $attributes = []): EndpointObjectInterface |
|
92
|
|
|
{ |
|
93
|
|
|
$filter = $this->getFilterOne($object); |
|
94
|
|
|
$this->logger->debug('find rest resource with filter ['.$filter.'] in endpoint ['.$this->getIdentifier().']', [ |
|
95
|
|
|
'category' => get_class($this), |
|
96
|
|
|
]); |
|
97
|
|
|
|
|
98
|
|
|
$options = $this->getRequestOptions(); |
|
99
|
|
|
$options['query']['$filter'] = $filter; |
|
100
|
|
|
$options['query']['$select'] = join(',', $attributes); |
|
101
|
|
|
$result = $this->client->get('', $options); |
|
102
|
|
|
$data = $this->getResponse($result); |
|
103
|
|
|
|
|
104
|
|
|
if (count($data) > 1) { |
|
105
|
|
|
throw new Exception\ObjectMultipleFound('found more than one object with filter '.$filter); |
|
106
|
|
|
} |
|
107
|
|
|
if (count($data) === 0) { |
|
108
|
|
|
throw new Exception\ObjectNotFound('no object found with filter '.$filter); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $this->build(array_shift($data)); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|