Passed
Push — feature/unit-tests ( 3fd24a...669eab )
by Yuichi
02:24
created

Cursor::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 4
dl 0
loc 12
ccs 0
cts 8
cp 0
crap 6
rs 10
1
<?php
2
3
namespace CybozuHttp\Api\Kintone;
4
5
use CybozuHttp\Api\KintoneApi;
6
use CybozuHttp\Client;
7
use CybozuHttp\Middleware\JsonStream;
8
9
/**
10
 * @author ochi51 <[email protected]>
11
 */
12
class Cursor
13
{
14
    public const MAX_GET_RECORDS = 500;
15
16
    /**
17
     * @var Client
18
     */
19
    private $client;
20
21 1
    public function __construct(Client $client)
22
    {
23 1
        $this->client = $client;
24
    }
25
26
    /**
27
     * Get all records
28
     * https://developer.cybozu.io/hc/ja/articles/360029152012
29
     *
30
     * @param int $appId
31
     * @param string $query
32
     * @param int|null $guestSpaceId
33
     * @param array|null $fields
34
     * @return array
35
     */
36
    public function all($appId, $query = '', $guestSpaceId = null, $fields = null): array
37
    {
38
        $records = [];
39
        $cursorId = $this->create($appId, $query, $guestSpaceId, $fields)['id'];
40
41
        while(true) {
42
            $result = $this->proceed($cursorId, $guestSpaceId);
43
            array_push($records, ...$result['records']);
44
            if (!$result['next']) {
45
                break;
46
            }
47
        }
48
49
        return $records;
50
    }
51
52
    /**
53
     * https://developer.cybozu.io/hc/ja/articles/360029152012#step1
54
     *
55
     * @param int $appId
56
     * @param string $query
57
     * @param int|null $guestSpaceId
58
     * @param array|null $fields
59
     * @return array ['id' => $cursorId, 'totalCount' => $totalCount]
60
     */
61
    public function create($appId, $query = '', $guestSpaceId = null, $fields = null): array
62
    {
63
        $options = ['json' => ['app' => $appId, 'query' => $query, 'size' => self::MAX_GET_RECORDS]];
64
        if ($fields) {
65
            $options['json']['fields'] = $fields;
66
        }
67
        /** @var JsonStream $stream */
68
        $stream = $this->client
69
            ->post(KintoneApi::generateUrl('records/cursor.json', $guestSpaceId), $options)
70
            ->getBody();
71
72
        return $stream->jsonSerialize();
73
    }
74
75
    /**
76
     * https://developer.cybozu.io/hc/ja/articles/360029152012#step2
77
     *
78
     * @param string $cursorId
79
     * @param int|null $guestSpaceId
80
     * @return array|null
81
     */
82
    public function proceed(string $cursorId, $guestSpaceId = null): ?array
83
    {
84
        $options = ['json' => ['id' => $cursorId]];
85
        /** @var JsonStream $stream */
86
        $stream = $this->client
87
            ->get(KintoneApi::generateUrl('records/cursor.json', $guestSpaceId), $options)
88
            ->getBody();
89
90
        return $stream->jsonSerialize();
91
    }
92
93
    /**
94
     * https://developer.cybozu.io/hc/ja/articles/360029152012#step3
95
     *
96
     * @param string $cursorId
97
     * @param int|null $guestSpaceId
98
     * @return array|null
99
     */
100
    public function delete(string $cursorId, $guestSpaceId = null): ?array
101
    {
102
        $options = ['json' => ['id' => $cursorId]];
103
        /** @var JsonStream $stream */
104
        $stream = $this->client
105
            ->delete(KintoneApi::generateUrl('records/cursor.json', $guestSpaceId), $options)
106
            ->getBody();
107
108
        return $stream->jsonSerialize();
109
    }
110
}
111