Completed
Pull Request — master (#63)
by Yuichi
07:08 queued 02:00
created

KintoneApi::record()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
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
namespace CybozuHttp\Api;
4
5
use CybozuHttp\Client;
6
use CybozuHttp\Api\Kintone\App;
7
use CybozuHttp\Api\Kintone\Apps;
8
use CybozuHttp\Api\Kintone\PreviewApp;
9
use CybozuHttp\Api\Kintone\Record;
10
use CybozuHttp\Api\Kintone\Records;
11
use CybozuHttp\Api\Kintone\Cursor;
12
use CybozuHttp\Api\Kintone\File;
13
use CybozuHttp\Api\Kintone\Comment;
14
use CybozuHttp\Api\Kintone\Comments;
15
use CybozuHttp\Api\Kintone\Graph;
16
use CybozuHttp\Api\Kintone\Space;
17
use CybozuHttp\Api\Kintone\Thread;
18
use CybozuHttp\Api\Kintone\Guests;
19
use CybozuHttp\Middleware\JsonStream;
20
21
/**
22
 * @author ochi51 <[email protected]>
23
 */
24
class KintoneApi
25
{
26
    public const API_PREFIX = '/k/v1/';
27
    public const GUEST_SPACE_PREFIX = '/k/guest/';
28
29
    /**
30
     * @var Client
31
     */
32
    private $client;
33
34
    /**
35
     * @var App
36
     */
37
    private $app;
38
39
    /**
40
     * @var Apps
41
     */
42
    private $apps;
43
44
    /**
45
     * @var PreviewApp
46
     */
47
    private $preview;
48
49
    /**
50
     * @var Record
51
     */
52
    private $record;
53
54
    /**
55
     * @var Records
56
     */
57
    private $records;
58
59
    /**
60
     * @var Cursor
61
     */
62
    private $cursor;
63
64
    /**
65
     * @var File
66
     */
67
    private $file;
68
69
    /**
70
     * @var Comment
71
     */
72
    private $comment;
73
74
    /**
75
     * @var Comments
76
     */
77
    private $comments;
78
79
    /**
80
     * @var Graph
81
     */
82
    private $graph;
83
84
    /**
85
     * @var Space
86
     */
87
    private $space;
88
89
    /**
90
     * @var Thread
91
     */
92
    private $thread;
93
94
    /**
95
     * @var Guests
96
     */
97
    private $guests;
98
99 1
    public function __construct(Client $client)
100
    {
101 1
        $this->client = $client;
102 1
        $this->app = new App($client);
103 1
        $this->apps = new Apps($client);
104 1
        $this->preview = new PreviewApp($client);
105 1
        $this->record = new Record($client);
106 1
        $this->records = new Records($client);
107 1
        $this->cursor = new Cursor($client);
108 1
        $this->file = new File($client);
109 1
        $this->comment = new Comment($client);
110 1
        $this->comments = new Comments($client);
111 1
        $this->graph = new Graph($client);
112 1
        $this->space = new Space($client);
113 1
        $this->thread = new Thread($client);
114 1
        $this->guests = new Guests($client);
115 1
    }
116
117
    /**
118
     * @param string $api
119
     * @param integer|null $guestSpaceId
120
     * @return string
121
     */
122 47
    public static function generateUrl($api, $guestSpaceId = null): string
123
    {
124 47
        if ($guestSpaceId && is_numeric($guestSpaceId)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $guestSpaceId 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...
125 43
            return self::GUEST_SPACE_PREFIX . $guestSpaceId .'/v1/'. $api;
126
        }
127
128 47
        return self::API_PREFIX . $api;
129
    }
130
131
    /**
132
     * @return Client
133
     */
134 2
    public function getClient(): Client
135
    {
136 2
        return $this->client;
137
    }
138
139
    /**
140
     * @return App
141
     */
142 12
    public function app(): App
143
    {
144 12
        return $this->app;
145
    }
146
147
    /**
148
     * @return Apps
149
     */
150 1
    public function apps(): Apps
151
    {
152 1
        return $this->apps;
153
    }
154
155
    /**
156
     * @return PreviewApp
157
     */
158 36
    public function preview(): PreviewApp
159
    {
160 36
        return $this->preview;
161
    }
162
163
    /**
164
     * @return Record
165
     */
166 8
    public function record(): Record
167
    {
168 8
        return $this->record;
169
    }
170
171
    /**
172
     * @return Records
173
     */
174 5
    public function records(): Records
175
    {
176 5
        return $this->records;
177
    }
178
179
    /**
180
     * @return Cursor
181
     */
182 1
    public function cursor(): Cursor
183
    {
184 1
        return $this->cursor;
185
    }
186
187
    /**
188
     * @return File
189
     */
190 3
    public function file(): File
191
    {
192 3
        return $this->file;
193
    }
194
195
    /**
196
     * @return Comment
197
     */
198 2
    public function comment(): Comment
199
    {
200 2
        return $this->comment;
201
    }
202
203
    /**
204
     * @return Comments
205
     */
206 3
    public function comments(): Comments
207
    {
208 3
        return $this->comments;
209
    }
210
211
    /**
212
     * @return Graph
213
     */
214 1
    public function graph(): Graph
215
    {
216 1
        return $this->graph;
217
    }
218
219
    /**
220
     * @return Space
221
     */
222 45
    public function space(): Space
223
    {
224 45
        return $this->space;
225
    }
226
227
    /**
228
     * @return Thread
229
     */
230 2
    public function thread(): Thread
231
    {
232 2
        return $this->thread;
233
    }
234
235
    /**
236
     * @return Guests
237
     */
238 2
    public function guests(): Guests
239
    {
240 2
        return $this->guests;
241
    }
242
243
    /**
244
     * Post bulkRequest
245
     * https://cybozudev.zendesk.com/hc/ja/articles/201941814
246
     *
247
     * @param array $requests
248
     * @param integer $guestSpaceId
249
     * @return array
250
     */
251 1
    public function postBulkRequest(array $requests, $guestSpaceId = null): array
252
    {
253 1
        $options = ['json' => ['requests' => $requests]];
254
255
        /** @var JsonStream $stream */
256 1
        $stream = $this->client
257 1
            ->post(self::generateUrl('bulkRequest.json', $guestSpaceId), $options)
258 1
            ->getBody();
259
260 1
        return $stream->jsonSerialize()['results'];
261
    }
262
}
263