Api::syncNext()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 17
ccs 0
cts 14
cp 0
crap 6
rs 9.9666
1
<?php
2
3
namespace Distilleries\Contentful\Api\Sync;
4
5
use GuzzleHttp\RequestOptions;
6
use Distilleries\Contentful\Api\BaseApi;
7
use Distilleries\Contentful\Api\SyncApi;
8
9
class Api extends BaseApi implements SyncApi
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    protected $baseUrl = 'https://cdn.contentful.com';
15
16
    /**
17
     * Preview base URL API.
18
     *
19
     * @var string
20
     */
21
    protected $previewBaseUrl = 'https://preview.contentful.com';
22
23
    /**
24
     * Sync token next URL parameter.
25
     *
26
     * @var string
27
     */
28
    protected $syncToken = '';
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function syncInitial(string $type = 'Entry'): array
34
    {
35
        $response = $this->client->request('GET', $this->url('sync'), [
36
            RequestOptions::QUERY => [
37
                'type' => $type,
38
                'initial' => true,
39
                'access_token' => $this->accessToken(),
40
            ],
41
        ]);
42
43
        $response = $this->decodeResponse($response);
44
        $this->setSyncToken($response);
45
46
        return $response['items'];
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function syncNext(): array
53
    {
54
        if (empty($this->syncToken)) {
55
            return [];
56
        }
57
58
        $response = $this->client->request('GET', $this->url('sync'), [
59
            RequestOptions::QUERY => [
60
                'sync_token' => $this->syncToken,
61
                'access_token' => $this->accessToken(),
62
            ],
63
        ]);
64
65
        $response = $this->decodeResponse($response);
66
        $this->setSyncToken($response);
67
68
        return $response['items'];
69
    }
70
71
    /**
72
     * Return access token to use.
73
     *
74
     * @return string
75
     */
76
    private function accessToken(): string
77
    {
78
        $token = config('contentful.use_preview') ? 'preview' : 'live';
79
80
        return $this->config['tokens']['delivery'][$token];
81
    }
82
83
    /**
84
     * Parse sync token from response nextSyncUrl parameter.
85
     *
86
     * @param  array  $response
87
     * @return void
88
     */
89
    private function setSyncToken(array $response)
90
    {
91
        $this->syncToken = '';
92
93
        if (isset($response['nextPageUrl']) && ! empty($response['nextPageUrl'])) {
94
            $data = parse_url($response['nextPageUrl']);
95
96
            if (isset($data['query']) && ! empty($data['query'])) {
97
                parse_str($data['query'], $params);
98
99
                if (isset($params['sync_token'])) {
100
                    $this->syncToken = $params['sync_token'];
101
                }
102
            }
103
        }
104
    }
105
}
106