Passed
Pull Request — master (#8)
by lee
03:23
created

BatchResult   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 88.24%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
eloc 38
c 1
b 0
f 0
dl 0
loc 107
ccs 45
cts 51
cp 0.8824
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A validateBatch() 0 9 2
A validate() 0 11 5
A key() 0 3 1
A __construct() 0 2 1
A count() 0 3 1
A valid() 0 3 1
A current() 0 3 1
A responses() 0 3 1
A fromResponse() 0 28 4
A rewind() 0 3 1
A response() 0 3 1
A next() 0 3 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\Response;
18
use ArangoDb\Type\BatchType;
19
use Countable;
20
use Iterator;
21
use Psr\Http\Message\ResponseInterface;
22
23
final class BatchResult implements Countable, Iterator
24
{
25
    /**
26
     * responses
27
     *
28
     * @var ResponseInterface[]
29
     */
30
    private $responses = [];
31
32 3
    private function __construct()
33
    {
34 3
    }
35
36 4
    public static function fromResponse(ResponseInterface $batchResponse): BatchResult
37
    {
38 4
        if ('multipart/form-data' !== $batchResponse->getHeader('Content-Type')[0] ?? '') {
39 1
            throw new InvalidArgumentException('Provided $batchResponse must have content type "multipart/form-data".');
40
        }
41
42 3
        $batches = explode(
43 3
            '--' . BatchType::MIME_BOUNDARY . BatchType::EOL,
44 3
            trim($batchResponse->getBody()->getContents(), '--' . BatchType::MIME_BOUNDARY . '--')
45
        );
46
47 3
        $self = new self();
48
49 3
        foreach ($batches as $batch) {
50 3
            $data = HttpHelper::parseMessage($batch);
51 3
            [$httpCode, $headers, $body] = HttpHelper::parseMessage($data[2] ?? '');
52
53 3
            $response = new Response($httpCode, $headers);
54 3
            $response->getBody()->write($body);
55 3
            $response->getBody()->rewind();
56
57 3
            if (isset($data[1]['Content-Id'][0])) {
58 3
                $self->responses[$data[1]['Content-Id'][0]] = $response;
59
            } else {
60
                $self->responses[] = $response;
61
            }
62
        }
63 3
        return $self;
64
    }
65
66 1
    public function validateBatch(BatchType $batch): void
67
    {
68 1
        $guards = $batch->guards();
69
70 1
        if ($guards === null) {
71
            throw new LogicException('No guards are provided in Batch.');
72
        }
73
74 1
        $this->validate(... $guards);
75 1
    }
76
77 3
    public function validate(Guard ...$guards): void
78
    {
79 3
        foreach ($guards as $guard) {
80 3
            if ($guard->contentId() === null) {
81 1
                foreach ($this->responses as $response) {
82 1
                    $guard($response);
83
                }
84 1
                continue;
85
            }
86 2
            if (null !== ($response = $this->responses[$guard->contentId()] ?? null)) {
87 2
                $guard($response);
88
            }
89
        }
90 3
    }
91
92
    public function response(string $contentId): ?ResponseInterface
93
    {
94
        return $this->responses[$contentId] ?? null;
95
    }
96
97
    public function responses(): array
98
    {
99
        return $this->responses;
100
    }
101
102 1
    public function count(): int
103
    {
104 1
        return count($this->responses);
105
    }
106
107 1
    public function current()
108
    {
109 1
        return current($this->responses);
110
    }
111
112 1
    public function next()
113
    {
114 1
        next($this->responses);
115 1
    }
116
117 1
    public function key()
118
    {
119 1
        return key($this->responses);
120
    }
121
122 1
    public function valid()
123
    {
124 1
        return $this->key() !== null;
125
    }
126
127 1
    public function rewind()
128
    {
129 1
        reset($this->responses);
130 1
    }
131
}
132