1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Sandro Keil (https://sandro-keil.de) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/sandrokeil/arangodb-php-client for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2018-2020 Sandro Keil |
7
|
|
|
* @license http://github.com/sandrokeil/arangodb-php-client/blob/master/LICENSE.md New BSD License |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace ArangoDb\Http; |
13
|
|
|
|
14
|
|
|
use ArangoDb\Exception\InvalidArgumentException; |
15
|
|
|
use ArangoDb\Exception\LogicException; |
16
|
|
|
use ArangoDb\Guard\Guard; |
17
|
|
|
use ArangoDb\Type\BatchType; |
18
|
|
|
use Countable; |
19
|
|
|
use Iterator; |
20
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
21
|
|
|
use Psr\Http\Message\ResponseInterface; |
22
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @implements Iterator<string, ResponseInterface> |
26
|
|
|
*/ |
27
|
|
|
final class BatchResult implements Countable, Iterator |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* responses |
31
|
|
|
* |
32
|
|
|
* @var ResponseInterface[] |
33
|
|
|
*/ |
34
|
|
|
private $responses = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ResponseFactoryInterface |
38
|
|
|
*/ |
39
|
|
|
private $responseFactory; |
40
|
|
|
|
41
|
4 |
|
private function __construct(ResponseFactoryInterface $responseFactory) |
42
|
|
|
{ |
43
|
4 |
|
$this->responseFactory = $responseFactory; |
44
|
4 |
|
} |
45
|
|
|
|
46
|
5 |
|
public static function fromResponse( |
47
|
|
|
ResponseInterface $batchResponse, |
48
|
|
|
ResponseFactoryInterface $responseFactory, |
49
|
|
|
StreamFactoryInterface $streamFactory |
50
|
|
|
): BatchResult { |
51
|
5 |
|
if ('multipart/form-data' !== ($batchResponse->getHeader('Content-Type')[0] ?? '')) { |
52
|
1 |
|
throw new InvalidArgumentException('Provided $batchResponse must have content type "multipart/form-data".'); |
53
|
|
|
} |
54
|
|
|
|
55
|
4 |
|
$batches = explode( |
56
|
4 |
|
'--' . BatchType::MIME_BOUNDARY . BatchType::EOL, |
57
|
4 |
|
trim($batchResponse->getBody()->getContents(), '--' . BatchType::MIME_BOUNDARY . '--') |
58
|
|
|
); |
59
|
|
|
|
60
|
4 |
|
$self = new self($responseFactory); |
61
|
|
|
|
62
|
4 |
|
foreach ($batches as $batch) { |
63
|
4 |
|
$data = HttpHelper::parseMessage($batch); |
64
|
4 |
|
[$httpCode, $headers, $body] = HttpHelper::parseMessage($data[2] ?? ''); |
65
|
|
|
|
66
|
4 |
|
$response = $self->responseFactory->createResponse($httpCode); |
67
|
|
|
|
68
|
4 |
|
foreach ($headers as $headerName => $header) { |
69
|
4 |
|
$response = $response->withAddedHeader($headerName, $header); |
70
|
|
|
} |
71
|
4 |
|
$response = $response->withBody($streamFactory->createStream($body)); |
72
|
|
|
|
73
|
4 |
|
if (isset($data[1]['Content-Id'][0])) { |
74
|
4 |
|
$self->responses[$data[1]['Content-Id'][0]] = $response; |
75
|
|
|
} else { |
76
|
|
|
$self->responses[] = $response; |
77
|
|
|
} |
78
|
|
|
} |
79
|
4 |
|
return $self; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
public function validateBatch(BatchType $batch): void |
83
|
|
|
{ |
84
|
1 |
|
$guards = $batch->guards(); |
85
|
|
|
|
86
|
1 |
|
if ($guards === null) { |
87
|
1 |
|
throw new LogicException('No guards are provided in Batch.'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->validate(... $guards); |
91
|
|
|
} |
92
|
|
|
|
93
|
3 |
|
public function validate(Guard ...$guards): void |
94
|
|
|
{ |
95
|
3 |
|
foreach ($guards as $guard) { |
96
|
3 |
|
if ($guard->contentId() === null) { |
97
|
1 |
|
foreach ($this->responses as $response) { |
98
|
1 |
|
$guard($response); |
99
|
|
|
} |
100
|
1 |
|
continue; |
101
|
|
|
} |
102
|
2 |
|
if (null !== ($response = $this->responses[$guard->contentId()] ?? null)) { |
103
|
2 |
|
$guard($response); |
104
|
|
|
} |
105
|
|
|
} |
106
|
3 |
|
} |
107
|
|
|
|
108
|
|
|
public function response(string $contentId): ?ResponseInterface |
109
|
|
|
{ |
110
|
|
|
return $this->responses[$contentId] ?? null; |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
public function responses(): array |
114
|
|
|
{ |
115
|
1 |
|
return $this->responses; |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
public function count(): int |
119
|
|
|
{ |
120
|
1 |
|
return count($this->responses); |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
public function current(): ResponseInterface |
124
|
|
|
{ |
125
|
1 |
|
return current($this->responses); |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
public function next(): void |
129
|
|
|
{ |
130
|
1 |
|
next($this->responses); |
131
|
1 |
|
} |
132
|
|
|
|
133
|
1 |
|
public function key() |
134
|
|
|
{ |
135
|
1 |
|
return key($this->responses); // @phpstan-ignore-line |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
public function valid(): bool |
139
|
|
|
{ |
140
|
1 |
|
return $this->key() !== null; // @phpstan-ignore-line |
141
|
|
|
} |
142
|
|
|
|
143
|
1 |
|
public function rewind(): void |
144
|
|
|
{ |
145
|
1 |
|
reset($this->responses); |
146
|
1 |
|
} |
147
|
|
|
} |
148
|
|
|
|