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

TransactionalClient::addList()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
ccs 5
cts 6
cp 0.8333
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.0416
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\Type\Batch;
15
use ArangoDb\Type\GuardSupport;
16
use ArangoDb\Type\Transaction as TransactionType;
17
use ArangoDb\Type\Transactional;
18
use ArangoDb\Type\Type;
19
use Fig\Http\Message\StatusCodeInterface;
20
use function MongoDB\BSON\toPHP;
21
use Psr\Http\Client\ClientInterface;
22
use Psr\Http\Message\RequestFactoryInterface;
23
use Psr\Http\Message\RequestInterface;
24
use Psr\Http\Message\ResponseFactoryInterface;
25
use Psr\Http\Message\ResponseInterface;
26
use Psr\Http\Message\StreamFactoryInterface;
27
28
final class TransactionalClient implements ClientInterface
29
{
30
    /**
31
     * @var ClientInterface
32
     */
33
    private $client;
34
35
    /**
36
     * @var RequestFactoryInterface
37
     */
38
    private $requestFactory;
39
40
    /**
41
     * @var ResponseFactoryInterface
42
     */
43
    private $responseFactory;
44
45
    /**
46
     * Types
47
     *
48
     * @var Type[]
49
     */
50
    private $types = [];
51
52
    /**
53
     * Types
54
     *
55
     * @var Transactional[]
56
     */
57
    private $transactionalTypes = [];
58
59
    /**
60
     * @var StreamFactoryInterface
61
     */
62
    private $streamFactory;
63
64 9
    public function __construct(
65
        ClientInterface $client,
66
        RequestFactoryInterface $requestFactory,
67
        ResponseFactoryInterface $responseFactory,
68
        StreamFactoryInterface $streamFactory
69
    ) {
70 9
        $this->client = $client;
71 9
        $this->requestFactory = $requestFactory;
72 9
        $this->responseFactory = $responseFactory;
73 9
        $this->streamFactory = $streamFactory;
74 9
    }
75
76
    public function sendRequest(RequestInterface $request): ResponseInterface
77
    {
78
        return $this->client->sendRequest($request);
79
    }
80
81
    /**
82
     * Sends types and transactional types. Type responses and transaction response are validated via guards if provided
83
     * to a type. You can also manually validate the transaction response but not the non transaction response.
84
     *
85
     * @param array $params
86
     * @param bool $waitForSync
87
     * @return ResponseInterface
88
     * @throws \Psr\Http\Client\ClientExceptionInterface
89
     */
90 8
    public function send(array $params = [], bool $waitForSync = false): ResponseInterface
91
    {
92 8
        if (0 !== count($this->types)) {
93 2
            $batch = Batch::fromTypes(...$this->types);
94 2
            $responseBatch = $this->client->sendRequest($batch->toRequest($this->requestFactory, $this->streamFactory));
95
96 2
            if (null !== ($guards = $batch->guards())) {
0 ignored issues
show
introduced by
The condition null !== $guards = $batch->guards() is always true.
Loading history...
97 1
                BatchResult::fromResponse(
98 1
                    $responseBatch,
99 1
                    $this->responseFactory,
100 1
                    $this->streamFactory
101 1
                )->validate(...$guards);
102
            }
103
        }
104
105 8
        $actions = '';
106 8
        $collectionsWrite = [[]];
107 8
        $collectionsRead = [[]];
108 8
        $return = [];
109 8
        $guards = [];
110
111 8
        if (0 === count($this->transactionalTypes)) {
112 1
            return $this->responseFactory->createResponse(StatusCodeInterface::STATUS_OK);
113
        }
114
115 7
        foreach ($this->transactionalTypes as $key => $type) {
116 7
            $collectionsWrite[] = $type->collectionsWrite();
117 7
            $collectionsRead[] = $type->collectionsRead();
118
119 7
            if ($type instanceof GuardSupport
120 7
                && ($guard = $type->guard()) !== null
121
            ) {
122 1
                $guards[] = $guard;
123 1
                $key = $guard->contentId();
124
            }
125 7
            $actions .= str_replace('var rId', 'var rId' . $key, $type->toJs());
126 7
            $return[] = 'rId' . $key;
127
        }
128 7
        $collectionsWrite = array_merge(...$collectionsWrite);
129 7
        $collectionsRead = array_merge(...$collectionsRead);
130
131 7
        $response = $this->client->sendRequest(
132 7
            TransactionType::with(
133 7
                sprintf(
134 7
                    'function () {var db = require("@arangodb").db;%s return {%s}}',
135
                    $actions,
136 7
                    implode(',', $return)
137
                ),
138 7
                array_unique($collectionsWrite),
139
                $params,
140 7
                array_unique($collectionsRead),
141
                $waitForSync
142 7
            )->toRequest($this->requestFactory, $this->streamFactory)
143
        );
144
145 7
        if (0 !== count($guards)) {
146
            \array_walk($guards, static function ($guard) use ($response) {
147 1
                $guard($response);
148 1
            });
149
        }
150
151 7
        $this->types = [];
152 7
        $this->transactionalTypes = [];
153
154 7
        return $response;
155
    }
156
157
    /**
158
     * Add type
159
     *
160
     * @param Type $type
161
     */
162 4
    public function add(Type $type): void
163
    {
164 4
        if ($type instanceof Transactional) {
165 4
            $this->transactionalTypes[] = $type;
166 4
            return;
167
        }
168 3
        $this->types[] = $type;
169 3
    }
170
171
    /**
172
     * Adds multiple types
173
     *
174
     * @param Type ...$types
175
     */
176 4
    public function addList(Type ...$types): void
177
    {
178 4
        foreach ($types as $type) {
179 4
            if ($type instanceof Transactional) {
180 4
                $this->transactionalTypes[] = $type;
181 4
                continue;
182
            }
183
            $this->types[] = $type;
184
        }
185 4
    }
186
187
    /**
188
     * Counts non transactional types
189
     *
190
     * @return int
191
     */
192 3
    public function countTypes(): int
193
    {
194 3
        return count($this->types);
195
    }
196
197
    /**
198
     * Counts transactional types
199
     *
200
     * @return int
201
     */
202 3
    public function countTransactionalTypes(): int
203
    {
204 3
        return count($this->transactionalTypes);
205
    }
206
207
    /**
208
     * Resets all types and transactional types
209
     */
210 1
    public function reset(): void
211
    {
212 1
        $this->types = [];
213 1
        $this->transactionalTypes = [];
214 1
    }
215
}
216