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\EndpointObject\EndpointObjectInterface; |
19
|
|
|
use Tubee\Workflow\Factory as WorkflowFactory; |
20
|
|
|
|
21
|
|
|
class Balloon extends AbstractRest |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Kind. |
25
|
|
|
*/ |
26
|
|
|
public const KIND = 'BalloonEndpoint'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Init endpoint. |
30
|
|
|
*/ |
31
|
6 |
|
public function __construct(string $name, string $type, Client $client, CollectionInterface $collection, WorkflowFactory $workflow, LoggerInterface $logger, array $resource = []) |
32
|
|
|
{ |
33
|
6 |
|
$this->identifier = 'id'; |
34
|
6 |
|
$this->container = 'data'; |
35
|
6 |
|
parent::__construct($name, $type, $client, $collection, $workflow, $logger, $resource); |
36
|
6 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
3 |
|
public function transformQuery(?array $query = null) |
42
|
|
|
{ |
43
|
3 |
|
$result = null; |
44
|
3 |
|
if ($this->filter_all !== null) { |
45
|
1 |
|
$result = stripslashes($this->filter_all); |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
if (!empty($query)) { |
49
|
2 |
|
if ($this->filter_all === null) { |
50
|
1 |
|
$result = json_encode($query); |
51
|
|
|
} else { |
52
|
1 |
|
$result = '{"$and":['.stripslashes($this->filter_all).', '.json_encode($query).']}'; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
3 |
|
return $result; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function getAll(?array $query = null): Generator |
63
|
|
|
{ |
64
|
|
|
$this->logger->debug('find all balloon objects using ['.$this->client->getConfig('base_uri').']', [ |
65
|
|
|
'category' => get_class($this), |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
$options = $this->getRequestOptions(); |
69
|
|
|
$options['query'] = [ |
70
|
|
|
'query' => $this->transformQuery($query), |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
$i = 0; |
74
|
|
|
$response = $this->client->get('', $options); |
75
|
|
|
$data = $this->getResponse($response); |
76
|
|
|
|
77
|
|
|
foreach ($data as $object) { |
78
|
|
|
yield $this->build($object); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $i; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
3 |
|
public function getOne(array $object, ?array $attributes = []): EndpointObjectInterface |
88
|
|
|
{ |
89
|
3 |
|
$filter = $this->getFilterOne($object); |
90
|
3 |
|
$this->logger->debug('find rest resource with filter ['.$filter.'] in endpoint ['.$this->getIdentifier().']', [ |
91
|
3 |
|
'category' => get_class($this), |
92
|
|
|
]); |
93
|
|
|
|
94
|
3 |
|
$options = $this->getRequestOptions(); |
95
|
3 |
|
$options['query'] = [ |
96
|
3 |
|
'query' => stripslashes($filter), |
97
|
|
|
]; |
98
|
|
|
|
99
|
3 |
|
$result = $this->client->get('', $options); |
100
|
3 |
|
$data = $this->getResponse($result); |
101
|
|
|
|
102
|
3 |
|
if (count($data) > 1) { |
103
|
1 |
|
throw new Exception\ObjectMultipleFound('found more than one object with filter '.$filter); |
104
|
|
|
} |
105
|
2 |
|
if (count($data) === 0) { |
106
|
1 |
|
throw new Exception\ObjectNotFound('no object found with filter '.$filter); |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
return $this->build(array_shift($data)); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|