Passed
Pull Request — master (#7)
by Sandro
02:06
created

BatchResult::current()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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-2019 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;
13
14
use ArangoDb\Exception\InvalidArgumentException;
15
use ArangoDb\Exception\LogicException;
16
use ArangoDb\Guard\Guard;
17
use ArangoDb\Http\JsonStream;
18
use ArangoDb\Type\BatchType;
19
use Countable;
20
use Iterator;
21
use Psr\Http\Message\ResponseFactoryInterface;
22
use Psr\Http\Message\ResponseInterface;
23
24
final class BatchResult implements Countable, Iterator
25
{
26
    /**
27
     * responses
28
     *
29
     * @var ResponseInterface[]
30
     */
31
    private $responses = [];
32
33
    /**
34
     * @var ResponseFactoryInterface
35
     */
36
    private $responseFactory;
37
38 4
    private function __construct(ResponseFactoryInterface $responseFactory)
39
    {
40 4
        $this->responseFactory = $responseFactory;
41 4
    }
42
43 5
    public static function fromResponse(
44
        ResponseInterface $batchResponse,
45
        ResponseFactoryInterface $responseFactory
46
    ): BatchResult {
47 5
        if ('multipart/form-data' !== ($batchResponse->getHeader('Content-Type')[0] ?? '')) {
48 1
            throw new InvalidArgumentException('Provided $batchResponse must have content type "multipart/form-data".');
49
        }
50
51 4
        $batches = explode(
52 4
            '--' . BatchType::MIME_BOUNDARY . BatchType::EOL,
53 4
            trim($batchResponse->getBody()->getContents(), '--' . BatchType::MIME_BOUNDARY . '--')
54
        );
55
56 4
        $self = new self($responseFactory);
57
58 4
        foreach ($batches as $batch) {
59 4
            $data = HttpHelper::parseMessage($batch);
60 4
            [$httpCode, $headers, $body] = HttpHelper::parseMessage($data[2] ?? '');
61
62 4
            $response = $self->responseFactory->createResponse($httpCode);
63
64 4
            foreach ($headers as $headerName => $header) {
65 4
                $response = $response->withAddedHeader($headerName, $header);
66
            }
67 4
            $response = $response->withBody(new JsonStream($body));
68
69 4
            if (isset($data[1]['Content-Id'][0])) {
70 4
                $self->responses[$data[1]['Content-Id'][0]] = $response;
71
            } else {
72
                $self->responses[] = $response;
73
            }
74
        }
75 4
        return $self;
76
    }
77
78 2
    public function validateBatch(BatchType $batch): void
79
    {
80 2
        $guards = $batch->guards();
81
82 2
        if ($guards === null) {
83 1
            throw new LogicException('No guards are provided in Batch.');
84
        }
85
86 1
        $this->validate(... $guards);
87 1
    }
88
89 3
    public function validate(Guard ...$guards): void
90
    {
91 3
        foreach ($guards as $guard) {
92 3
            if ($guard->contentId() === null) {
93 1
                foreach ($this->responses as $response) {
94 1
                    $guard($response);
95
                }
96 1
                continue;
97
            }
98 2
            if (null !== ($response = $this->responses[$guard->contentId()] ?? null)) {
99 2
                $guard($response);
100
            }
101
        }
102 3
    }
103
104
    public function response(string $contentId): ?ResponseInterface
105
    {
106
        return $this->responses[$contentId] ?? null;
107
    }
108
109 1
    public function responses(): array
110
    {
111 1
        return $this->responses;
112
    }
113
114 1
    public function count(): int
115
    {
116 1
        return count($this->responses);
117
    }
118
119 1
    public function current(): ResponseInterface
120
    {
121 1
        return current($this->responses);
122
    }
123
124 1
    public function next(): void
125
    {
126 1
        next($this->responses);
127 1
    }
128
129
    /**
130
     * @return int|string|null
131
     */
132 1
    public function key()
133
    {
134 1
        return key($this->responses);
135
    }
136
137 1
    public function valid(): bool
138
    {
139 1
        return $this->key() !== null;
140
    }
141
142 1
    public function rewind(): void
143
    {
144 1
        reset($this->responses);
145 1
    }
146
}
147