Completed
Pull Request — master (#143)
by Alexander
07:24
created

DataSyncClient::getDatabaseUrl()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
ccs 17
cts 17
cp 1
rs 8.439
cc 6
eloc 14
nc 16
nop 3
crap 6
1
<?php
2
/**
3
 * Yandex PHP Library
4
 *
5
 * @copyright NIX Solutions Ltd.
6
 * @link      https://github.com/nixsolutions/yandex-php-library
7
 */
8
9
/**
10
 * @namespace
11
 */
12
namespace Yandex\DataSync;
13
14
use Psr\Http\Message\UriInterface;
15
use Yandex\Common\AbstractServiceClient;
16
use GuzzleHttp\Psr7\Response;
17
use GuzzleHttp\Exception\ClientException;
18
use Yandex\Common\Exception\ForbiddenException;
19
use Yandex\Common\Exception\IncorrectDataFormatException;
20
use Yandex\DataSync\Exception\IncorrectRevisionNumberException;
21
use Yandex\Common\Exception\InvalidArgumentException;
22
use Yandex\DataSync\Exception\MaxDatabasesCountException;
23
use Yandex\Common\Exception\NotFoundException;
24
use Yandex\DataSync\Exception\RevisionOnServerOverCurrentException;
25
use Yandex\DataSync\Exception\RevisionTooOldException;
26
use Yandex\Common\Exception\TooManyRequestsException;
27
use Yandex\Common\Exception\UnauthorizedException;
28
use Yandex\Common\Exception\UnavailableResourceException;
29
use Yandex\DataSync\Exception\DataSyncException;
30
use Yandex\DataSync\Models\Database;
31
use Yandex\DataSync\Responses\DatabaseDeltasResponse;
32
use Yandex\DataSync\Responses\DatabaseSnapshotResponse;
33
use Yandex\DataSync\Responses\DatabasesResponse;
34
35
/**
36
 * Class DataSyncClient
37
 *
38
 * @category Yandex
39
 * @package  DataSync
40
 *
41
 * @author   Alexander Khaylo <[email protected]>
42
 * @created  01.03.15 12:07
43
 */
44
class DataSyncClient extends AbstractServiceClient
45
{
46
    /**
47
     * Requested version of API
48
     *
49
     * @var string
50
     */
51
    private $version = 'v1';
52
53
    /**
54
     * API domain
55
     *
56
     * @var string
57
     */
58
    protected $serviceDomain = 'cloud-api.yandex.net';
59
60
    /**
61
     * DB app context.
62
     */
63
    const CONTEXT_APP = 'app';
64
65
    /**
66
     * DB user context.
67
     */
68
    const CONTEXT_USER = 'user';
69
70
    /**
71
     * @var string
72
     */
73
    private $context;
74
75
    /**
76
     * @var string
77
     */
78
    private $databaseId;
79
80
    /**
81
     * @return string
82
     */
83 1
    public function getDatabaseId()
84
    {
85 1
        return $this->databaseId;
86
    }
87
88
    /**
89
     * @param string $databaseId
90
     */
91 26
    public function setDatabaseId($databaseId)
92
    {
93 26
        $this->databaseId = $databaseId;
94 26
    }
95
96
    /**
97
     * @return string
98
     */
99 7
    public function getContext()
100
    {
101 7
        return $this->context;
102
    }
103
104
    /**
105
     * @param string $context
106
     *
107
     * @throws InvalidArgumentException
108
     */
109 32
    public function setContext($context)
110
    {
111 32
        if ($context === self::CONTEXT_APP || $context === self::CONTEXT_USER) {
112 31
            $this->context = $context;
113 31
        } else {
114 1
            throw new InvalidArgumentException('Incorrect context');
115
        }
116 31
    }
117
118
    /**
119
     * @param string $token access token
120
     * @param null   $context
121
     * @param null   $databaseId
122
     */
123 36
    public function __construct($token = '', $context = null, $databaseId = null)
124
    {
125 36
        $this->setAccessToken($token);
126
127 36
        if ($context) {
128 5
            $this->setContext($context);
129 4
        }
130
131 35
        if ($databaseId) {
132 1
            $this->setDatabaseId($databaseId);
133 1
        }
134 35
    }
135
136
    /**
137
     * @param null|string $context
138
     * @param array       $fields
139
     * @param null        $limit
140
     * @param null        $offset
141
     *
142
     * @return string
143
     * @throws InvalidArgumentException
144
     */
145 3
    public function getDatabasesUrl($context = null, $fields = [], $limit = null, $offset = null)
146
    {
147 3
        if ($context) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $context of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
148 2
            $this->setContext($context);
149 2
        }
150
151 3
        if (!$this->context) {
152 1
            throw new InvalidArgumentException('Empty context');
153
        }
154
155 2
        $params = '?';
156 2
        if ($limit) {
157 1
            $params .= 'limit=' . $limit . '&';
158 1
        }
159
160 2
        if ($offset) {
161 1
            $params .= 'offset=' . $offset . '&';
162 1
        }
163
164 2
        if ($fields) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
165 1
            $params .= 'fields=' . implode(',', $fields) . '&';
166 1
        }
167 2
        $params = rtrim($params, "&");
168
169 2
        return $this->serviceScheme . '://' . $this->serviceDomain . '/' . $this->version . '/data/'
170 2
        . $this->context . '/databases/' . $params;
171
    }
172
173
    /**
174
     * @param null|string $databaseId
175
     * @param null|string $context
176
     * @param array       $fields
177
     *
178
     * @return string
179
     * @throws InvalidArgumentException
180
     */
181 21
    public function getDatabaseUrl($databaseId = null, $context = null, $fields = [])
182
    {
183 21
        if ($context) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $context of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
184 19
            $this->setContext($context);
185 19
        }
186 21
        if ($databaseId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $databaseId of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
187 19
            $this->setDatabaseId($databaseId);
188 19
        }
189
190 21
        if (!$this->context) {
191 1
            throw new InvalidArgumentException('Empty context');
192
        }
193
194 20
        if (!$this->databaseId) {
195 1
            throw new InvalidArgumentException('Empty database id');
196
        }
197
198 19
        $params = '';
199 19
        if ($fields) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
200 1
            $params = '?fields=' . implode(',', $fields);
201 1
        }
202
203 19
        return $this->serviceScheme . '://' . $this->serviceDomain . '/' . $this->version . '/data/'
204 19
        . $this->context . '/databases/' . $this->databaseId . '/' . $params;
205
    }
206
207
    /**
208
     * @param null|string $databaseId
209
     * @param null|string $context
210
     * @param null        $collectionId
211
     * @param array       $fields
212
     *
213
     * @return string
214
     * @throws InvalidArgumentException
215
     */
216 4
    public function getDatabaseSnapshotUrl($databaseId = null, $context = null, $collectionId = null, $fields = [])
217
    {
218 4
        if ($context) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $context of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
219 2
            $this->setContext($context);
220 2
        }
221 4
        if ($databaseId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $databaseId of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
222 2
            $this->setDatabaseId($databaseId);
223 2
        }
224 4
        if (!$this->context) {
225 1
            throw new InvalidArgumentException('Empty context');
226
        }
227
228 3
        if (!$this->databaseId) {
229 1
            throw new InvalidArgumentException('Empty database id');
230
        }
231
232 2
        $params = '?';
233 2
        if ($collectionId) {
234 1
            $params .= 'collection_id=' . $collectionId . '&';
235 1
        }
236
237 2
        if ($fields) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
238 1
            $params .= 'fields=' . implode(',', $fields) . '&';
239 1
        }
240 2
        $params = rtrim($params, "&");
241
242 2
        return $this->serviceScheme . '://' . $this->serviceDomain . '/' . $this->version . '/data/'
243 2
        . $this->context . '/databases/' . $this->databaseId . '/snapshot/' . $params;
244
    }
245
246
    /**
247
     * @param null|string $databaseId
248
     * @param null|string $context
249
     * @param array       $fields
250
     * @param int         $baseRevision
251
     * @param int         $limit
252
     *
253
     * @return string
254
     * @throws InvalidArgumentException
255
     */
256 6
    public function getDatabaseDeltasUrl(
257
        $databaseId = null,
258
        $context = null,
259
        $fields = [],
260
        $baseRevision = null,
261
        $limit = null
262
    ) {
263 6
        if ($context) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $context of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
264 4
            $this->setContext($context);
265 4
        }
266 6
        if ($databaseId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $databaseId of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
267 4
            $this->setDatabaseId($databaseId);
268 4
        }
269 6
        if (!$this->context) {
270 1
            throw new InvalidArgumentException('Empty context');
271
        }
272 5
        if (!$this->databaseId) {
273 1
            throw new InvalidArgumentException('Empty database id');
274
        }
275 4
        $params = '';
276 4
        if ($baseRevision !== null) {
277 2
            $params .= 'base_revision=' . $baseRevision . '&';
278 2
        }
279 4
        if ($limit > 0) {
280 2
            $params .= 'limit=' . $limit . '&';
281 2
        }
282 4
        if ($fields) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
283 2
            $params .= 'fields=' . implode(',', $fields) . '&';
284 2
        }
285 4
        if ($params) {
286 3
            $params = '/?' . $params;
287 3
            $params = rtrim($params, "&");
288 3
        }
289
290 4
        return $this->serviceScheme . '://' . $this->serviceDomain . '/' . $this->version . '/data/'
291 4
        . $this->context . '/databases/' . $this->databaseId . '/deltas' . $params;
292
    }
293
294
    /**
295
     * Sends a request
296
     *
297
     * @param string              $method  HTTP method
298
     * @param UriInterface|string $uri     URI object or string.
299
     * @param array               $options Request options to apply.
300
     *
301
     * @return Response|\Psr\Http\Message\ResponseInterface
302
     * @throws DataSyncException
303
     * @throws ForbiddenException
304
     * @throws IncorrectDataFormatException
305
     * @throws IncorrectRevisionNumberException
306
     * @throws InvalidArgumentException
307
     * @throws MaxDatabasesCountException
308
     * @throws NotFoundException
309
     * @throws RevisionOnServerOverCurrentException
310
     * @throws RevisionTooOldException
311
     * @throws TooManyRequestsException
312
     * @throws UnauthorizedException
313
     * @throws UnavailableResourceException
314
     */
315 24
    protected function sendRequest($method, $uri, array $options = [])
316
    {
317
        try {
318 14
            $response = $this->getClient()->request($method, $uri, $options);
319 14
        } catch (ClientException $ex) {
320 13
            $result  = $ex->getResponse();
321 13
            $code    = $result->getStatusCode();
322 13
            $message = $result->getReasonPhrase();
323
324
            switch ($code) {
325 13
                case 400:
326 1
                    throw new InvalidArgumentException($message);
327 12
                case 401:
328 1
                    throw new UnauthorizedException($message);
329 24
                case 403:
330 1
                    throw new ForbiddenException($message);
331 10
                case 404:
332 1
                    throw new NotFoundException($message);
333 9
                case 406:
334 1
                    throw new IncorrectDataFormatException($message);
335 8
                case 409:
336 1
                    throw new RevisionOnServerOverCurrentException($message);
337 7
                case 410:
338 1
                    throw new RevisionTooOldException($message);
339 6
                case 412:
340 1
                    throw new IncorrectRevisionNumberException($message);
341 5
                case 415:
342 1
                    throw new IncorrectDataFormatException($message);
343 4
                case 423:
344 1
                    throw new UnavailableResourceException($message);
345 3
                case 429:
346 1
                    throw new TooManyRequestsException($message);
347 2
                case 507:
348 1
                    throw new MaxDatabasesCountException($message);
349
            }
350
351 1
            throw new DataSyncException(
352 1
                'Service responded with error code: "' . $code . '" and message: "' . $message . '"',
353
                $code
354 1
            );
355
        }
356
357 1
        return $response;
358
    }
359
360
    /**
361
     * @param null|string $context
362
     * @param array       $fields
363
     * @param null        $limit
364
     * @param null        $offset
365
     *
366
     * @return DatabasesResponse
367
     * @throws DataSyncException
368
     * @throws ForbiddenException
369
     * @throws IncorrectDataFormatException
370
     * @throws InvalidArgumentException
371
     * @throws MaxDatabasesCountException
372
     * @throws NotFoundException
373
     * @throws TooManyRequestsException
374
     * @throws UnauthorizedException
375
     * @throws UnavailableResourceException
376
     */
377 2
    public function getDatabases($context = null, $fields = [], $limit = null, $offset = null)
378
    {
379 2
        $response            = $this->sendRequest('GET', $this->getDatabasesUrl($context, $fields, $limit, $offset));
380 2
        $decodedResponseBody = $this->getDecodedBody($response->getBody());
381 2
        $databasesResponse   = new DatabasesResponse($decodedResponseBody);
382 2
        if ($databasesResponse->getItems()) {
383 1
            $databases = $databasesResponse->getItems()->getAll();
384 1
            foreach ($databases as $database) {
385 1
                $database->setContext($this->getContext());
386 1
            }
387 1
        }
388
389 2
        return $databasesResponse;
390
    }
391
392
    /**
393
     * @param null|string $databaseId
394
     * @param null|string $context
395
     * @param array       $fields
396
     *
397
     * @return Database
398
     * @throws DataSyncException
399
     * @throws ForbiddenException
400
     * @throws IncorrectDataFormatException
401
     * @throws InvalidArgumentException
402
     * @throws MaxDatabasesCountException
403
     * @throws NotFoundException
404
     * @throws TooManyRequestsException
405
     * @throws UnauthorizedException
406
     * @throws UnavailableResourceException
407
     */
408 1 View Code Duplication
    public function createDatabase($databaseId = null, $context = null, $fields = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
409
    {
410 1
        $response            = $this->sendRequest('PUT', $this->getDatabaseUrl($databaseId, $context, $fields));
411 1
        $decodedResponseBody = $this->getDecodedBody($response->getBody());
412 1
        $database            = new Database($decodedResponseBody);
413 1
        $database->setContext($this->getContext());
414 1
        return $database;
415
    }
416
417
    /**
418
     * @param null|string $databaseId
419
     * @param null|string $context
420
     * @param array       $fields
421
     *
422
     * @return Database
423
     * @throws DataSyncException
424
     * @throws ForbiddenException
425
     * @throws IncorrectDataFormatException
426
     * @throws InvalidArgumentException
427
     * @throws MaxDatabasesCountException
428
     * @throws NotFoundException
429
     * @throws TooManyRequestsException
430
     * @throws UnauthorizedException
431
     * @throws UnavailableResourceException
432
     */
433 16 View Code Duplication
    public function getDatabase($databaseId = null, $context = null, $fields = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
434
    {
435 16
        $response            = $this->sendRequest('GET', $this->getDatabaseUrl($databaseId, $context, $fields));
436 3
        $decodedResponseBody = $this->getDecodedBody($response->getBody());
437 3
        $database            = new Database($decodedResponseBody);
438 3
        $database->setContext($this->getContext());
439 3
        return $database;
440
    }
441
442
    /**
443
     * @param null|string $databaseId
444
     * @param null|string $context
445
     *
446
     * @return bool
447
     *
448
     * @throws DataSyncException
449
     * @throws ForbiddenException
450
     * @throws IncorrectDataFormatException
451
     * @throws InvalidArgumentException
452
     * @throws MaxDatabasesCountException
453
     * @throws NotFoundException
454
     * @throws TooManyRequestsException
455
     * @throws UnauthorizedException
456
     * @throws UnavailableResourceException
457
     */
458 1
    public function deleteDatabase($databaseId = null, $context = null)
459
    {
460 1
        $response = $this->sendRequest('DELETE', $this->getDatabaseUrl($databaseId, $context));
461 1
        return $response->getStatusCode() === 204;
462
    }
463
464
    /**
465
     * @param string $title
466
     * @param null   $databaseId
467
     * @param null   $context
468
     * @param array  $fields
469
     *
470
     * @return Database
471
     * @throws DataSyncException
472
     * @throws ForbiddenException
473
     * @throws IncorrectDataFormatException
474
     * @throws InvalidArgumentException
475
     * @throws MaxDatabasesCountException
476
     * @throws NotFoundException
477
     * @throws TooManyRequestsException
478
     * @throws UnauthorizedException
479
     * @throws UnavailableResourceException
480
     */
481 1
    public function updateDatabaseTitle($title, $databaseId = null, $context = null, $fields = [])
482
    {
483 1
        $response            = $this->sendRequest(
484 1
            'PATCH',
485 1
            $this->getDatabaseUrl($databaseId, $context, $fields),
486
            [
487 1
                'json' => ['title' => $title]
488 1
            ]
489 1
        );
490 1
        $decodedResponseBody = $this->getDecodedBody($response->getBody());
491 1
        $database            = new Database($decodedResponseBody);
492 1
        $database->setContext($this->getContext());
493 1
        return $database;
494
    }
495
496
    /**
497
     * @param null|string $databaseId
498
     * @param null|string $context
499
     * @param null        $collectionId
500
     * @param array       $fields
501
     *
502
     * @return DatabaseSnapshotResponse
503
     * @throws DataSyncException
504
     * @throws ForbiddenException
505
     * @throws IncorrectDataFormatException
506
     * @throws InvalidArgumentException
507
     * @throws MaxDatabasesCountException
508
     * @throws NotFoundException
509
     * @throws TooManyRequestsException
510
     * @throws UnauthorizedException
511
     * @throws UnavailableResourceException
512
     */
513 2 View Code Duplication
    public function getDatabaseSnapshot($databaseId = null, $context = null, $collectionId = null, $fields = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
514
    {
515 2
        $response            = $this->sendRequest(
516 2
            'GET',
517 2
            $this->getDatabaseSnapshotUrl($databaseId, $context, $collectionId, $fields)
518 2
        );
519 2
        $decodedResponseBody = $this->getDecodedBody($response->getBody());
520 2
        $result              = new DatabaseSnapshotResponse($decodedResponseBody);
521 2
        return $result;
522
    }
523
524
    /**
525
     * @param array       $data
526
     * @param int         $revision
527
     * @param null|string $databaseId
528
     * @param null|string $context
529
     * @param array       $fields
530
     *
531
     * @return array
532
     * @throws DataSyncException
533
     * @throws ForbiddenException
534
     * @throws IncorrectDataFormatException
535
     * @throws IncorrectRevisionNumberException
536
     * @throws InvalidArgumentException
537
     * @throws MaxDatabasesCountException
538
     * @throws NotFoundException
539
     * @throws RevisionOnServerOverCurrentException
540
     * @throws RevisionTooOldException
541
     * @throws TooManyRequestsException
542
     * @throws UnauthorizedException
543
     * @throws UnavailableResourceException
544
     *
545
     * @see https://tech.yandex.ru/datasync/http/doc/tasks/add-changes-docpage/
546
     */
547 2
    public function saveDelta($data, $revision = 0, $databaseId = null, $context = null, $fields = [])
548
    {
549
        $options             = [
550
            'headers' => [
551 2
                'If-Match' => $revision,
552 2
            ],
553
            'json'    => $data
554 2
        ];
555 2
        $response            = $this->sendRequest(
556 2
            'POST',
557 2
            $this->getDatabaseDeltasUrl($databaseId, $context, $fields),
558
            $options
559 2
        );
560 2
        $decodedResponseBody = $this->getDecodedBody($response->getBody());
561 2
        if ($response->getHeader('ETag')
562 2
            && is_array($response->getHeader('ETag'))
563 2
            && count($response->getHeader('ETag')) > 0
564 2
        ) {
565 1
            $decodedResponseBody['revision'] = $response->getHeader('ETag')[0];
566 1
        }
567
568 2
        return $decodedResponseBody;
569
    }
570
571
    /**
572
     * @param int         $baseRevision
573
     * @param null|string $databaseId
574
     * @param null|string $context
575
     * @param array       $fields
576
     * @param null|int    $limit
577
     *
578
     * @return DatabaseDeltasResponse
579
     * @throws DataSyncException
580
     * @throws ForbiddenException
581
     * @throws IncorrectDataFormatException
582
     * @throws IncorrectRevisionNumberException
583
     * @throws InvalidArgumentException
584
     * @throws MaxDatabasesCountException
585
     * @throws NotFoundException
586
     * @throws RevisionOnServerOverCurrentException
587
     * @throws RevisionTooOldException
588
     * @throws TooManyRequestsException
589
     * @throws UnauthorizedException
590
     * @throws UnavailableResourceException
591
     */
592 2 View Code Duplication
    public function getDelta($baseRevision = 0, $databaseId = null, $context = null, $fields = [], $limit = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
593
    {
594 2
        $response            = $this->sendRequest(
595 2
            'GET',
596 2
            $this->getDatabaseDeltasUrl($databaseId, $context, $fields, $baseRevision, $limit)
597 2
        );
598 2
        $decodedResponseBody = $this->getDecodedBody($response->getBody());
599 2
        $result              = new DatabaseDeltasResponse($decodedResponseBody);
600 2
        return $result;
601
    }
602
}
603