Completed
Pull Request — master (#36)
by
unknown
08:22
created

KintoneApi::fields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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