Completed
Push — php7.1 ( 829f77...5f4644 )
by Yuichi
06:33
created

KintoneApi   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 15
dl 0
loc 225
ccs 51
cts 51
cp 1
rs 10
c 0
b 0
f 0

16 Methods

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