Passed
Pull Request — master (#7)
by Sandro
03:30
created

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