DataSyncClient   D
last analyzed

Complexity

Total Complexity 58

Size/Duplication

Total Lines 534
Duplicated Lines 6.74 %

Coupling/Cohesion

Components 1
Dependencies 21

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 58
lcom 1
cbo 21
dl 36
loc 534
ccs 165
cts 165
cp 1
rs 4.5599
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A getDatabaseId() 0 8 2
A setDatabaseId() 0 4 1
A getContext() 0 7 2
A setContext() 0 8 3
A __construct() 0 12 3
A getDatabasesUrl() 0 20 5
A getDatabaseUrl() 0 17 4
A getDatabaseSnapshotUrl() 0 20 5
B getDatabaseDeltasUrl() 0 27 6
C sendRequest() 0 44 14
A getDatabases() 0 14 3
A createDatabase() 8 8 1
A getDatabase() 8 8 1
A deleteDatabase() 0 5 1
A updateDatabaseTitle() 0 14 1
A getDatabaseSnapshot() 10 10 1
A saveDelta() 0 23 4
A getDelta() 10 10 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like DataSyncClient often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use DataSyncClient, and based on these observations, apply Extract Interface, too.

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 Yandex\Common\AbstractServiceClient;
15
use GuzzleHttp\Psr7\Response;
16
use GuzzleHttp\Exception\ClientException;
17
use Yandex\Common\Exception\ForbiddenException;
18
use Yandex\Common\Exception\IncorrectDataFormatException;
19
use Yandex\DataSync\Exception\IncorrectRevisionNumberException;
20
use Yandex\Common\Exception\InvalidArgumentException;
21
use Yandex\DataSync\Exception\MaxDatabasesCountException;
22
use Yandex\Common\Exception\NotFoundException;
23
use Yandex\DataSync\Exception\RevisionOnServerOverCurrentException;
24
use Yandex\DataSync\Exception\RevisionTooOldException;
25
use Yandex\Common\Exception\TooManyRequestsException;
26
use Yandex\Common\Exception\UnauthorizedException;
27
use Yandex\Common\Exception\UnavailableResourceException;
28
use Yandex\DataSync\Exception\DataSyncException;
29
use Yandex\DataSync\Models\Database;
30
use Yandex\DataSync\Responses\DatabaseDeltasResponse;
31
use Yandex\DataSync\Responses\DatabaseSnapshotResponse;
32
use Yandex\DataSync\Responses\DatabasesResponse;
33
34
/**
35
 * Class DataSyncClient
36
 *
37
 * @category Yandex
38
 * @package  DataSync
39
 *
40
 * @author   Alexander Khaylo <[email protected]>
41
 * @created  01.03.15 12:07
42
 */
43
class DataSyncClient extends AbstractServiceClient
44
{
45
    /**
46
     * DB app context.
47
     */
48
    const CONTEXT_APP = 'app';
49
50
    /**
51
     * DB user context.
52
     */
53
    const CONTEXT_USER = 'user';
54
55
    /**
56
     * Requested version of API
57
     *
58
     * @var string
59
     */
60
    private $version = 'v1';
61
62
    /**
63
     * API domain
64
     *
65
     * @var string
66
     */
67
    protected $serviceDomain = 'cloud-api.yandex.net';
68
69
    /**
70
     * @var string
71
     */
72
    private $context;
73
74
    /**
75
     * @var string
76
     */
77
    private $databaseId;
78
79
    /**
80
     * @return string
81
     * @throws InvalidArgumentException
82
     */
83 27
    public function getDatabaseId()
84
    {
85 27
        if (!$this->databaseId) {
86 1
            throw new InvalidArgumentException('Empty database id');
87
        }
88
89 26
        return $this->databaseId;
90
    }
91
92
    /**
93
     * @param string $databaseId
94
     */
95 26
    public function setDatabaseId($databaseId)
96
    {
97 26
        $this->databaseId = $databaseId;
98 26
    }
99
100
    /**
101
     * @return string
102
     * @throws InvalidArgumentException
103
     */
104 29
    public function getContext()
105
    {
106 29
        if (!$this->context) {
107 1
            throw new InvalidArgumentException('Empty context');
108
        }
109 28
        return $this->context;
110
    }
111
112
    /**
113
     * @param string $context
114
     *
115
     * @throws InvalidArgumentException
116
     */
117 29
    public function setContext($context)
118
    {
119 29
        if ($context === self::CONTEXT_APP || $context === self::CONTEXT_USER) {
120 28
            $this->context = $context;
121
        } else {
122 1
            throw new InvalidArgumentException('Incorrect context');
123
        }
124 28
    }
125
126
    /**
127
     * @param string $token access token
128
     * @param null   $context
129
     * @param null   $databaseId
130
     */
131 31
    public function __construct($token = '', $context = null, $databaseId = null)
132
    {
133 31
        $this->setAccessToken($token);
134
135 31
        if ($context) {
136 2
            $this->setContext($context);
137
        }
138
139 30
        if ($databaseId) {
140 1
            $this->setDatabaseId($databaseId);
141
        }
142 30
    }
143
144
    /**
145
     * @param null|string $context
146
     * @param array       $fields
147
     * @param null        $limit
148
     * @param null        $offset
149
     *
150
     * @return string
151
     * @throws InvalidArgumentException
152
     */
153 2
    protected function getDatabasesUrl($context = null, $fields = [], $limit = null, $offset = null)
154
    {
155 2
        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...
156 2
            $this->setContext($context);
157
        }
158
159 2
        $params = [];
160 2
        if ($limit) {
161 1
            $params['limit'] = $limit;
162
        }
163 2
        if ($offset) {
164 1
            $params['offset'] = $offset;
165
        }
166 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...
167 1
            $params['fields'] = implode(',', $fields);
168
        }
169
170 2
        return $this->serviceScheme . '://' . $this->serviceDomain . '/' . $this->version . '/data/'
171 2
        . $this->getContext() . '/databases/?' . http_build_query($params);
172
    }
173
174
    /**
175
     * @param null|string $databaseId
176
     * @param null|string $context
177
     * @param array       $fields
178
     *
179
     * @return string
180
     * @throws InvalidArgumentException
181
     */
182 19
    protected function getDatabaseUrl($databaseId = null, $context = null, $fields = [])
183
    {
184 19
        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...
185 19
            $this->setContext($context);
186
        }
187 19
        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...
188 19
            $this->setDatabaseId($databaseId);
189
        }
190
191 19
        $params = [];
192 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...
193 1
            $params['fields'] = implode(',', $fields);
194
        }
195
196 19
        return $this->serviceScheme . '://' . $this->serviceDomain . '/' . $this->version . '/data/'
197 19
        . $this->getContext() . '/databases/' . $this->getDatabaseId() . '/?' . http_build_query($params);
198
    }
199
200
    /**
201
     * @param null|string $databaseId
202
     * @param null|string $context
203
     * @param null        $collectionId
204
     * @param array       $fields
205
     *
206
     * @return string
207
     * @throws InvalidArgumentException
208
     */
209 2
    protected function getDatabaseSnapshotUrl($databaseId = null, $context = null, $collectionId = null, $fields = [])
210
    {
211 2
        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...
212 2
            $this->setContext($context);
213
        }
214 2
        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...
215 2
            $this->setDatabaseId($databaseId);
216
        }
217
218 2
        $params = [];
219 2
        if ($collectionId) {
220 1
            $params['collection_id'] = $collectionId;
221
        }
222 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...
223 1
            $params['fields'] = implode(',', $fields);
224
        }
225
226 2
        return $this->serviceScheme . '://' . $this->serviceDomain . '/' . $this->version . '/data/'
227 2
        . $this->getContext() . '/databases/' . $this->getDatabaseId() . '/snapshot/?' . http_build_query($params);
228
    }
229
230
    /**
231
     * @param null|string $databaseId
232
     * @param null|string $context
233
     * @param array       $fields
234
     * @param int         $baseRevision
235
     * @param int         $limit
236
     *
237
     * @return string
238
     * @throws InvalidArgumentException
239
     */
240 4
    protected function getDatabaseDeltasUrl(
241
        $databaseId = null,
242
        $context = null,
243
        $fields = [],
244
        $baseRevision = null,
245
        $limit = null
246
    ) {
247 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...
248 4
            $this->setContext($context);
249
        }
250 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...
251 4
            $this->setDatabaseId($databaseId);
252
        }
253 4
        $params = [];
254 4
        if ($baseRevision !== null) {
255 2
            $params['base_revision'] = $baseRevision;
256
        }
257 4
        if ($limit) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $limit of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
258 2
            $params['limit'] = $limit;
259
        }
260 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...
261 2
            $params['fields'] = implode(',', $fields);
262
        }
263
264 4
        return $this->serviceScheme . '://' . $this->serviceDomain . '/' . $this->version . '/data/'
265 4
        . $this->getContext() . '/databases/' . $this->getDatabaseId() . '/deltas?' . http_build_query($params);
266
    }
267
268
    /**
269
     * Sends a request
270
     *
271
     * @param string              $method  HTTP method
272
     * @param string $uri     URI object or string.
273
     * @param array               $options Request options to apply.
274
     *
275
     * @return Response|\Psr\Http\Message\ResponseInterface
276
     * @throws DataSyncException
277
     * @throws ForbiddenException
278
     * @throws IncorrectDataFormatException
279
     * @throws IncorrectRevisionNumberException
280
     * @throws InvalidArgumentException
281
     * @throws MaxDatabasesCountException
282
     * @throws NotFoundException
283
     * @throws RevisionOnServerOverCurrentException
284
     * @throws RevisionTooOldException
285
     * @throws TooManyRequestsException
286
     * @throws UnauthorizedException
287
     * @throws UnavailableResourceException
288
     */
289 14
    protected function sendRequest($method, $uri, array $options = [])
290
    {
291
        try {
292 14
            $response = $this->getClient()->request($method, $uri, $options);
293 13
        } catch (ClientException $ex) {
294 13
            $result  = $ex->getResponse();
295 13
            $code    = $result->getStatusCode();
296 13
            $message = $result->getReasonPhrase();
297
298
            switch ($code) {
299 13
                case 400:
300 1
                    throw new InvalidArgumentException($message);
301 12
                case 401:
302 1
                    throw new UnauthorizedException($message);
303 11
                case 403:
304 1
                    throw new ForbiddenException($message);
305 10
                case 404:
306 1
                    throw new NotFoundException($message);
307 9
                case 406:
308 1
                    throw new IncorrectDataFormatException($message);
309 8
                case 409:
310 1
                    throw new RevisionOnServerOverCurrentException($message);
311 7
                case 410:
312 1
                    throw new RevisionTooOldException($message);
313 6
                case 412:
314 1
                    throw new IncorrectRevisionNumberException($message);
315 5
                case 415:
316 1
                    throw new IncorrectDataFormatException($message);
317 4
                case 423:
318 1
                    throw new UnavailableResourceException($message);
319 3
                case 429:
320 1
                    throw new TooManyRequestsException($message);
321 2
                case 507:
322 1
                    throw new MaxDatabasesCountException($message);
323
            }
324
325 1
            throw new DataSyncException(
326 1
                'Service responded with error code: "' . $code . '" and message: "' . $message . '"',
327 1
                $code
328
            );
329
        }
330
331 1
        return $response;
332
    }
333
334
    /**
335
     * @param null|string $context
336
     * @param array       $fields
337
     * @param null        $limit
338
     * @param null        $offset
339
     *
340
     * @return DatabasesResponse
341
     * @throws DataSyncException
342
     * @throws ForbiddenException
343
     * @throws IncorrectDataFormatException
344
     * @throws InvalidArgumentException
345
     * @throws MaxDatabasesCountException
346
     * @throws NotFoundException
347
     * @throws TooManyRequestsException
348
     * @throws UnauthorizedException
349
     * @throws UnavailableResourceException
350
     */
351 2
    public function getDatabases($context = null, $fields = [], $limit = null, $offset = null)
352
    {
353 2
        $response            = $this->sendRequest('GET', $this->getDatabasesUrl($context, $fields, $limit, $offset));
354 2
        $decodedResponseBody = $this->getDecodedBody($response->getBody());
355 2
        $databasesResponse   = new DatabasesResponse($decodedResponseBody);
356 2
        if ($databasesResponse->getItems()) {
357 1
            $databases = $databasesResponse->getItems()->getAll();
358 1
            foreach ($databases as $database) {
359 1
                $database->setContext($this->getContext());
360
            }
361
        }
362
363 2
        return $databasesResponse;
364
    }
365
366
    /**
367
     * @param null|string $databaseId
368
     * @param null|string $context
369
     * @param array       $fields
370
     *
371
     * @return Database
372
     * @throws DataSyncException
373
     * @throws ForbiddenException
374
     * @throws IncorrectDataFormatException
375
     * @throws InvalidArgumentException
376
     * @throws MaxDatabasesCountException
377
     * @throws NotFoundException
378
     * @throws TooManyRequestsException
379
     * @throws UnauthorizedException
380
     * @throws UnavailableResourceException
381
     */
382 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...
383
    {
384 1
        $response            = $this->sendRequest('PUT', $this->getDatabaseUrl($databaseId, $context, $fields));
385 1
        $decodedResponseBody = $this->getDecodedBody($response->getBody());
386 1
        $database            = new Database($decodedResponseBody);
387 1
        $database->setContext($this->getContext());
388 1
        return $database;
389
    }
390
391
    /**
392
     * @param null|string $databaseId
393
     * @param null|string $context
394
     * @param array       $fields
395
     *
396
     * @return Database
397
     * @throws DataSyncException
398
     * @throws ForbiddenException
399
     * @throws IncorrectDataFormatException
400
     * @throws InvalidArgumentException
401
     * @throws MaxDatabasesCountException
402
     * @throws NotFoundException
403
     * @throws TooManyRequestsException
404
     * @throws UnauthorizedException
405
     * @throws UnavailableResourceException
406
     */
407 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...
408
    {
409 16
        $response            = $this->sendRequest('GET', $this->getDatabaseUrl($databaseId, $context, $fields));
410 3
        $decodedResponseBody = $this->getDecodedBody($response->getBody());
411 3
        $database            = new Database($decodedResponseBody);
412 3
        $database->setContext($this->getContext());
413 3
        return $database;
414
    }
415
416
    /**
417
     * @param null|string $databaseId
418
     * @param null|string $context
419
     *
420
     * @return bool
421
     *
422
     * @throws DataSyncException
423
     * @throws ForbiddenException
424
     * @throws IncorrectDataFormatException
425
     * @throws InvalidArgumentException
426
     * @throws MaxDatabasesCountException
427
     * @throws NotFoundException
428
     * @throws TooManyRequestsException
429
     * @throws UnauthorizedException
430
     * @throws UnavailableResourceException
431
     */
432 1
    public function deleteDatabase($databaseId = null, $context = null)
433
    {
434 1
        $response = $this->sendRequest('DELETE', $this->getDatabaseUrl($databaseId, $context));
435 1
        return $response->getStatusCode() === 204;
436
    }
437
438
    /**
439
     * @param string $title
440
     * @param null   $databaseId
441
     * @param null   $context
442
     * @param array  $fields
443
     *
444
     * @return Database
445
     * @throws DataSyncException
446
     * @throws ForbiddenException
447
     * @throws IncorrectDataFormatException
448
     * @throws InvalidArgumentException
449
     * @throws MaxDatabasesCountException
450
     * @throws NotFoundException
451
     * @throws TooManyRequestsException
452
     * @throws UnauthorizedException
453
     * @throws UnavailableResourceException
454
     */
455 1
    public function updateDatabaseTitle($title, $databaseId = null, $context = null, $fields = [])
456
    {
457 1
        $response            = $this->sendRequest(
458 1
            'PATCH',
459 1
            $this->getDatabaseUrl($databaseId, $context, $fields),
460
            [
461 1
                'json' => ['title' => $title]
462
            ]
463
        );
464 1
        $decodedResponseBody = $this->getDecodedBody($response->getBody());
465 1
        $database            = new Database($decodedResponseBody);
466 1
        $database->setContext($this->getContext());
467 1
        return $database;
468
    }
469
470
    /**
471
     * @param null|string $databaseId
472
     * @param null|string $context
473
     * @param null        $collectionId
474
     * @param array       $fields
475
     *
476
     * @return DatabaseSnapshotResponse
477
     * @throws DataSyncException
478
     * @throws ForbiddenException
479
     * @throws IncorrectDataFormatException
480
     * @throws InvalidArgumentException
481
     * @throws MaxDatabasesCountException
482
     * @throws NotFoundException
483
     * @throws TooManyRequestsException
484
     * @throws UnauthorizedException
485
     * @throws UnavailableResourceException
486
     */
487 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...
488
    {
489 2
        $response            = $this->sendRequest(
490 2
            'GET',
491 2
            $this->getDatabaseSnapshotUrl($databaseId, $context, $collectionId, $fields)
492
        );
493 2
        $decodedResponseBody = $this->getDecodedBody($response->getBody());
494 2
        $result              = new DatabaseSnapshotResponse($decodedResponseBody);
495 2
        return $result;
496
    }
497
498
    /**
499
     * @param array       $data
500
     * @param int         $revision
501
     * @param null|string $databaseId
502
     * @param null|string $context
503
     * @param array       $fields
504
     *
505
     * @return array
506
     * @throws DataSyncException
507
     * @throws ForbiddenException
508
     * @throws IncorrectDataFormatException
509
     * @throws IncorrectRevisionNumberException
510
     * @throws InvalidArgumentException
511
     * @throws MaxDatabasesCountException
512
     * @throws NotFoundException
513
     * @throws RevisionOnServerOverCurrentException
514
     * @throws RevisionTooOldException
515
     * @throws TooManyRequestsException
516
     * @throws UnauthorizedException
517
     * @throws UnavailableResourceException
518
     *
519
     * @see https://tech.yandex.ru/datasync/http/doc/tasks/add-changes-docpage/
520
     */
521 2
    public function saveDelta($data, $revision = 0, $databaseId = null, $context = null, $fields = [])
522
    {
523
        $options             = [
524
            'headers' => [
525 2
                'If-Match' => $revision,
526
            ],
527 2
            'json'    => $data
528
        ];
529 2
        $response            = $this->sendRequest(
530 2
            'POST',
531 2
            $this->getDatabaseDeltasUrl($databaseId, $context, $fields),
532 2
            $options
533
        );
534 2
        $decodedResponseBody = $this->getDecodedBody($response->getBody());
535 2
        if ($response->getHeader('ETag')
536 2
            && is_array($response->getHeader('ETag'))
537 2
            && count($response->getHeader('ETag')) > 0
538
        ) {
539 1
            $decodedResponseBody['revision'] = $response->getHeader('ETag')[0];
540
        }
541
542 2
        return $decodedResponseBody;
543
    }
544
545
    /**
546
     * @param int         $baseRevision
547
     * @param null|string $databaseId
548
     * @param null|string $context
549
     * @param array       $fields
550
     * @param null|int    $limit
551
     *
552
     * @return DatabaseDeltasResponse
553
     * @throws DataSyncException
554
     * @throws ForbiddenException
555
     * @throws IncorrectDataFormatException
556
     * @throws IncorrectRevisionNumberException
557
     * @throws InvalidArgumentException
558
     * @throws MaxDatabasesCountException
559
     * @throws NotFoundException
560
     * @throws RevisionOnServerOverCurrentException
561
     * @throws RevisionTooOldException
562
     * @throws TooManyRequestsException
563
     * @throws UnauthorizedException
564
     * @throws UnavailableResourceException
565
     */
566 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...
567
    {
568 2
        $response            = $this->sendRequest(
569 2
            'GET',
570 2
            $this->getDatabaseDeltasUrl($databaseId, $context, $fields, $baseRevision, $limit)
571
        );
572 2
        $decodedResponseBody = $this->getDecodedBody($response->getBody());
573 2
        $result              = new DatabaseDeltasResponse($decodedResponseBody);
574 2
        return $result;
575
    }
576
}
577