PreviewApp::putSettings()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 7
dl 0
loc 17
ccs 13
cts 13
cp 1
crap 1
rs 9.9
1
<?php
2
3
namespace CybozuHttp\Api\Kintone;
4
5
use CybozuHttp\Client;
6
use CybozuHttp\Api\KintoneApi;
7
use CybozuHttp\Middleware\JsonStream;
8
9
/**
10
 * @author ochi51 <[email protected]>
11
 */
12
class PreviewApp
13
{
14
    /**
15
     * @var Client
16
     */
17
    private $client;
18
19 1
    public function __construct(Client $client)
20
    {
21 1
        $this->client = $client;
22
    }
23
24
    /**
25
     * Deploy app
26
     * https://cybozudev.zendesk.com/hc/ja/articles/204699420
27
     *
28
     * @param integer $id
29
     * @param integer $guestSpaceId
30
     * @param integer $revision
31
     * @param boolean $revert
32
     * @return array
33
     */
34 23
    public function deploy($id, $guestSpaceId = null, $revision = -1, $revert = false): array
35
    {
36 23
        return $this->deployApps([[
37 23
            'app' => $id,
38 23
            'revision' => $revision
39 23
        ]], $guestSpaceId, $revert);
40
    }
41
42
    /**
43
     * Deploy apps
44
     * https://cybozudev.zendesk.com/hc/ja/articles/204699420
45
     *
46
     * @param array   $apps
47
     * @param integer $guestSpaceId
48
     * @param boolean $revert
49
     * @return array
50
     */
51 23
    public function deployApps(array $apps, $guestSpaceId = null, $revert = false): array
52
    {
53 23
        $options = ['json' => compact('apps', 'revert')];
54
55
        /** @var JsonStream $stream */
56 23
        $stream = $this->client
57 23
            ->post(KintoneApi::generateUrl('preview/app/deploy.json', $guestSpaceId), $options)
58 23
            ->getBody();
59
60 23
        return $stream->jsonSerialize();
61
    }
62
63
    /**
64
     * get deploy status
65
     * https://cybozudev.zendesk.com/hc/ja/articles/204699420
66
     *
67
     * @param integer $id
68
     * @param integer $guestSpaceId
69
     * @return array
70
     */
71 23
    public function getDeployStatus($id, $guestSpaceId = null): array
72
    {
73 23
        return $this->getDeployStatuses([$id], $guestSpaceId)[0];
74
    }
75
76
    /**
77
     * get deploy statuses
78
     * https://cybozudev.zendesk.com/hc/ja/articles/204699420
79
     *
80
     * @param array $ids
81
     * @param integer $guestSpaceId
82
     * @return array
83
     */
84 23
    public function getDeployStatuses(array $ids, $guestSpaceId = null): array
85
    {
86 23
        $options = ['json' => ['apps' => $ids]];
87
88
        /** @var JsonStream $stream */
89 23
        $stream = $this->client
90 23
            ->get(KintoneApi::generateUrl('preview/app/deploy.json', $guestSpaceId), $options)
91 23
            ->getBody();
92
93 23
        return $stream->jsonSerialize()['apps'];
94
    }
95
96
    /**
97
     * Post preview app
98
     * https://cybozudev.zendesk.com/hc/ja/articles/202931674#step1
99
     *
100
     * @param string  $name
101
     * @param integer $spaceId
102
     * @param integer $threadId
103
     * @param integer $guestSpaceId
104
     * @return array
105
     */
106 36
    public function post($name, $spaceId = null, $threadId = null, $guestSpaceId = null): array
107
    {
108 36
        $options = ['json' => ['name' => $name]];
109 36
        if ($spaceId !== null) {
110 36
            $options['json']['space'] = $spaceId;
111 36
            $options['json']['thread'] = $threadId;
112
        }
113
114
        /** @var JsonStream $stream */
115 36
        $stream = $this->client
116 36
            ->post(KintoneApi::generateUrl('preview/app.json', $guestSpaceId), $options)
117 36
            ->getBody();
118
119 36
        return $stream->jsonSerialize();
120
    }
121
122
    /**
123
     * Get app settings
124
     * https://cybozudev.zendesk.com/hc/ja/articles/204694170
125
     *
126
     * @param integer $id
127
     * @param integer $guestSpaceId
128
     * @param string $lang
129
     * @return array
130
     */
131 1
    public function getSettings($id, $guestSpaceId = null, $lang = 'default'): array
132
    {
133 1
        $options = ['json' => ['app' => $id, 'lang' => $lang]];
134
135
        /** @var JsonStream $stream */
136 1
        $stream = $this->client
137 1
            ->get(KintoneApi::generateUrl('preview/app/settings.json', $guestSpaceId), $options)
138 1
            ->getBody();
139
140 1
        return $stream->jsonSerialize();
141
    }
142
143
    /**
144
     * Get app form fields
145
     * https://cybozudev.zendesk.com/hc/ja/articles/204783170#anchor_getform_fields
146
     *
147
     * @param integer $id
148
     * @param integer $guestSpaceId
149
     * @param string $lang
150
     * @return array
151
     */
152 1
    public function getFields($id, $guestSpaceId = null, $lang = 'default'): array
153
    {
154 1
        $options = ['json' => ['app' => $id, 'lang' => $lang]];
155
156
        /** @var JsonStream $stream */
157 1
        $stream = $this->client
158 1
            ->get(KintoneApi::generateUrl('preview/app/form/fields.json', $guestSpaceId), $options)
159 1
            ->getBody();
160
161 1
        return $stream->jsonSerialize();
162
    }
163
164
    /**
165
     * Get app form layout
166
     * https://cybozudev.zendesk.com/hc/ja/articles/204783170#anchor_getform_layout
167
     *
168
     * @param integer $id
169
     * @param integer $guestSpaceId
170
     * @param string $lang
171
     * @return array
172
     */
173 1
    public function getLayout($id, $guestSpaceId = null, $lang = 'default'): array
174
    {
175 1
        $options = ['json' => ['app' => $id, 'lang' => $lang]];
176
177
        /** @var JsonStream $stream */
178 1
        $stream = $this->client
179 1
            ->get(KintoneApi::generateUrl('preview/app/form/layout.json', $guestSpaceId), $options)
180 1
            ->getBody();
181
182 1
        return $stream->jsonSerialize();
183
    }
184
185
    /**
186
     * Get app views
187
     * https://cybozudev.zendesk.com/hc/ja/articles/204529784
188
     *
189
     * @param integer $id
190
     * @param integer $guestSpaceId
191
     * @param string $lang
192
     * @return array
193
     */
194 1
    public function getViews($id, $guestSpaceId = null, $lang = 'default'): array
195
    {
196 1
        $options = ['json' => ['app' => $id, 'lang' => $lang]];
197
198
        /** @var JsonStream $stream */
199 1
        $stream = $this->client
200 1
            ->get(KintoneApi::generateUrl('preview/app/views.json', $guestSpaceId), $options)
201 1
            ->getBody();
202
203 1
        return $stream->jsonSerialize();
204
    }
205
206
    /**
207
     * Get app acl
208
     * https://cybozudev.zendesk.com/hc/ja/articles/204529754
209
     *
210
     * @param integer $id
211
     * @param integer $guestSpaceId
212
     * @param string $lang
213
     * @return array
214
     */
215 1
    public function getAcl($id, $guestSpaceId = null, $lang = 'default'): array
216
    {
217 1
        $options = ['json' => ['app' => $id, 'lang' => $lang]];
218
219
        /** @var JsonStream $stream */
220 1
        $stream = $this->client
221 1
            ->get(KintoneApi::generateUrl('preview/app/acl.json', $guestSpaceId), $options)
222 1
            ->getBody();
223
224 1
        return $stream->jsonSerialize();
225
    }
226
227
    /**
228
     * Get record acl
229
     * https://cybozudev.zendesk.com/hc/ja/articles/204791510
230
     *
231
     * @param integer $id
232
     * @param integer $guestSpaceId
233
     * @param string $lang
234
     * @return array
235
     */
236 1
    public function getRecordAcl($id, $guestSpaceId = null, $lang = 'default'): array
237
    {
238 1
        $options = ['json' => ['app' => $id, 'lang' => $lang]];
239
240
        /** @var JsonStream $stream */
241 1
        $stream = $this->client
242 1
            ->get(KintoneApi::generateUrl('preview/record/acl.json', $guestSpaceId), $options)
243 1
            ->getBody();
244
245 1
        return $stream->jsonSerialize();
246
    }
247
248
    /**
249
     * Get field acl
250
     * https://cybozudev.zendesk.com/hc/ja/articles/204791520
251
     *
252
     * @param integer $id
253
     * @param integer $guestSpaceId
254
     * @param string $lang
255
     * @return array
256
     */
257 1
    public function getFieldAcl($id, $guestSpaceId = null, $lang = 'default'): array
258
    {
259 1
        $options = ['json' => ['app' => $id, 'lang' => $lang]];
260
261
        /** @var JsonStream $stream */
262 1
        $stream = $this->client
263 1
            ->get(KintoneApi::generateUrl('preview/field/acl.json', $guestSpaceId), $options)
264 1
            ->getBody();
265
266 1
        return $stream->jsonSerialize();
267
    }
268
269
    /**
270
     * Get app JavaScript and CSS customize
271
     * https://cybozudev.zendesk.com/hc/ja/articles/204529824
272
     *
273
     * @param integer $id
274
     * @param integer $guestSpaceId
275
     * @return array
276
     */
277 4
    public function getCustomize($id, $guestSpaceId = null): array
278
    {
279 4
        $options = ['json' => ['app' => $id]];
280
281
        /** @var JsonStream $stream */
282 4
        $stream = $this->client
283 4
            ->get(KintoneApi::generateUrl('preview/app/customize.json', $guestSpaceId), $options)
284 4
            ->getBody();
285
286 4
        return $stream->jsonSerialize();
287
    }
288
289
    /**
290
     * Get app status list
291
     * https://cybozudev.zendesk.com/hc/ja/articles/216972946
292
     *
293
     * @param integer $id
294
     * @param string  $lang
295
     * @param integer $guestSpaceId
296
     * @return array
297
     */
298 1
    public function getStatus($id, $lang = 'ja', $guestSpaceId = null): array
299
    {
300 1
        $options = ['json' => ['app' => $id, 'lang' => $lang]];
301
302
        /** @var JsonStream $stream */
303 1
        $stream = $this->client
304 1
            ->get(KintoneApi::generateUrl('preview/app/status.json', $guestSpaceId), $options)
305 1
            ->getBody();
306
307 1
        return $stream->jsonSerialize();
308
    }
309
310
    /**
311
     * Put preview app settings
312
     * https://cybozudev.zendesk.com/hc/ja/articles/204730520
313
     *
314
     * @param integer $id
315
     * @param string  $name
316
     * @param string  $description
317
     * @param array   $icon
318
     * @param string  $theme
319
     * @param integer $guestSpaceId
320
     * @param integer $revision
321
     * @return array
322
     */
323 24
    public function putSettings($id, $name, $description, array $icon, $theme, $guestSpaceId = null, $revision = -1): array
324
    {
325 24
        $options = ['json' => [
326 24
            'app' => $id,
327 24
            'name' => $name,
328 24
            'description' => $description,
329 24
            'icon' => $icon,
330 24
            'theme' => $theme,
331 24
            'revision' => $revision
332 24
        ]];
333
334
        /** @var JsonStream $stream */
335 24
        $stream = $this->client
336 24
            ->put(KintoneApi::generateUrl('preview/app/settings.json', $guestSpaceId), $options)
337 24
            ->getBody();
338
339 24
        return $stream->jsonSerialize();
340
    }
341
342
    /**
343
     * Post form fields to preview app
344
     * https://cybozudev.zendesk.com/hc/ja/articles/204529724#anchor_changeform_addfields
345
     *
346
     * @param integer $id
347
     * @param array   $fields
348
     * @param integer $guestSpaceId
349
     * @param integer $revision
350
     * @return array
351
     */
352 27
    public function postFields($id, array $fields, $guestSpaceId = null, $revision = -1): array
353
    {
354 27
        $options = ['json' => [
355 27
            'app' => $id,
356 27
            'properties' => $fields,
357 27
            'revision' => $revision
358 27
        ]];
359
360
        /** @var JsonStream $stream */
361 27
        $stream = $this->client
362 27
            ->post(KintoneApi::generateUrl('preview/app/form/fields.json', $guestSpaceId), $options)
363 27
            ->getBody();
364
365 27
        return $stream->jsonSerialize();
366
    }
367
368
    /**
369
     * Put form fields to preview app
370
     * https://cybozudev.zendesk.com/hc/ja/articles/204529724#anchor_changeform_changefields
371
     *
372
     * @param integer $id
373
     * @param array   $fields
374
     * @param integer $guestSpaceId
375
     * @param integer $revision
376
     * @return array
377
     */
378 1
    public function putFields($id, array $fields, $guestSpaceId = null, $revision = -1): array
379
    {
380 1
        $options = ['json' => [
381 1
            'app' => $id,
382 1
            'properties' => $fields,
383 1
            'revision' => $revision
384 1
        ]];
385
386
        /** @var JsonStream $stream */
387 1
        $stream = $this->client
388 1
            ->put(KintoneApi::generateUrl('preview/app/form/fields.json', $guestSpaceId), $options)
389 1
            ->getBody();
390
391 1
        return $stream->jsonSerialize();
392
    }
393
394
    /**
395
     * Delete form fields to preview app
396
     * https://cybozudev.zendesk.com/hc/ja/articles/204529724#anchor_changeform_deletefields
397
     *
398
     * @param integer $id
399
     * @param array   $fields
400
     * @param integer $guestSpaceId
401
     * @param integer $revision
402
     * @return array
403
     */
404 1
    public function deleteFields($id, array $fields, $guestSpaceId = null, $revision = -1): array
405
    {
406 1
        $options = ['json' => [
407 1
            'app' => $id,
408 1
            'fields' => $fields,
409 1
            'revision' => $revision
410 1
        ]];
411
412
        /** @var JsonStream $stream */
413 1
        $stream = $this->client
414 1
            ->delete(KintoneApi::generateUrl('preview/app/form/fields.json', $guestSpaceId), $options)
415 1
            ->getBody();
416
417 1
        return $stream->jsonSerialize();
418
    }
419
420
    /**
421
     * Put form layout to preview app
422
     * https://cybozudev.zendesk.com/hc/ja/articles/204529724#anchor_changeform_changelayout
423
     *
424
     * @param integer $id
425
     * @param array   $layout
426
     * @param integer $guestSpaceId
427
     * @param integer $revision
428
     * @return array
429
     */
430 23
    public function putLayout($id, array $layout, $guestSpaceId = null, $revision = -1): array
431
    {
432 23
        $options = ['json' => [
433 23
            'app' => $id,
434 23
            'layout' => $layout,
435 23
            'revision' => $revision
436 23
        ]];
437
438
        /** @var JsonStream $stream */
439 23
        $stream = $this->client
440 23
            ->put(KintoneApi::generateUrl('preview/app/form/layout.json', $guestSpaceId), $options)
441 23
            ->getBody();
442
443 23
        return $stream->jsonSerialize();
444
    }
445
446
    /**
447
     * Put views to preview app
448
     * https://cybozudev.zendesk.com/hc/ja/articles/204529794
449
     *
450
     * @param integer $id
451
     * @param array   $views
452
     * @param integer $guestSpaceId
453
     * @param integer $revision
454
     * @return array
455
     */
456 24
    public function putViews($id, array $views, $guestSpaceId = null, $revision = -1): array
457
    {
458 24
        $options = ['json' => [
459 24
            'app' => $id,
460 24
            'views' => $views,
461 24
            'revision' => $revision
462 24
        ]];
463
464
        /** @var JsonStream $stream */
465 24
        $stream = $this->client
466 24
            ->put(KintoneApi::generateUrl('preview/app/views.json', $guestSpaceId), $options)
467 24
            ->getBody();
468
469 24
        return $stream->jsonSerialize();
470
    }
471
472
    /**
473
     * Put app acl to preview app
474
     * https://cybozudev.zendesk.com/hc/ja/articles/201941844
475
     *
476
     * @param integer $id
477
     * @param array   $rights
478
     * @param integer $guestSpaceId
479
     * @param integer $revision
480
     * @return array
481
     */
482 1
    public function putAcl($id, array $rights, $guestSpaceId = null, $revision = -1): array
483
    {
484 1
        $options = ['json' => [
485 1
            'app' => $id,
486 1
            'rights' => $rights,
487 1
            'revision' => $revision
488 1
        ]];
489
490
        /** @var JsonStream $stream */
491 1
        $stream = $this->client
492 1
            ->put(KintoneApi::generateUrl('preview/app/acl.json', $guestSpaceId), $options)
493 1
            ->getBody();
494
495 1
        return $stream->jsonSerialize();
496
    }
497
498
    /**
499
     * Put record acl to preview app
500
     * https://cybozudev.zendesk.com/hc/ja/articles/201941854
501
     *
502
     * @param integer $id
503
     * @param array   $rights
504
     * @param integer $guestSpaceId
505
     * @param integer $revision
506
     * @return array
507
     */
508 1
    public function putRecordAcl($id, array $rights, $guestSpaceId = null, $revision = -1): array
509
    {
510 1
        $options = ['json' => compact('id', 'rights', 'revision')];
511
512
        /** @var JsonStream $stream */
513 1
        $stream = $this->client
514 1
            ->put(KintoneApi::generateUrl('preview/record/acl.json', $guestSpaceId), $options)
515 1
            ->getBody();
516
517 1
        return $stream->jsonSerialize();
518
    }
519
520
    /**
521
     * Put field acl to preview app
522
     * https://cybozudev.zendesk.com/hc/ja/articles/201941854
523
     *
524
     * @param integer $id
525
     * @param array   $rights
526
     * @param integer $guestSpaceId
527
     * @param integer $revision
528
     * @return array
529
     */
530 1
    public function putFieldAcl($id, array $rights, $guestSpaceId = null, $revision = -1): array
531
    {
532 1
        $options = ['json' => compact('id', 'rights', 'revision')];
533
534
        /** @var JsonStream $stream */
535 1
        $stream = $this->client
536 1
            ->put(KintoneApi::generateUrl('preview/field/acl.json', $guestSpaceId), $options)
537 1
            ->getBody();
538
539 1
        return $stream->jsonSerialize();
540
    }
541
542
    /**
543
     * Put app JavaScript & CSS customize to preview app
544
     * https://cybozudev.zendesk.com/hc/ja/articles/204529834
545
     *
546
     * @param integer $id
547
     * @param array   $js
548
     * @param array   $css
549
     * @param array   $mobileJs
550
     * @param integer $guestSpaceId
551
     * @param string  $scope
552
     * @param integer $revision
553
     * @return array
554
     */
555 4
    public function putCustomize($id, array $js = [], array $css = [], array $mobileJs = [], $guestSpaceId = null, $scope = 'ALL', $revision = -1): array
556
    {
557 4
        $options = ['json' => [
558 4
            'app' => $id,
559 4
            'desktop' => compact('js', 'css'),
560 4
            'mobile' => [
561 4
                'js' => $mobileJs
562 4
            ],
563 4
            'scope' => $scope,
564 4
            'revision' => $revision
565 4
        ]];
566
567
        /** @var JsonStream $stream */
568 4
        $stream = $this->client
569 4
            ->put(KintoneApi::generateUrl('preview/app/customize.json', $guestSpaceId), $options)
570 4
            ->getBody();
571
572 4
        return $stream->jsonSerialize();
573
    }
574
575
    /**
576
     * Put app status and action
577
     * https://cybozudev.zendesk.com/hc/ja/articles/217905503
578
     *
579
     * @param integer $id
580
     * @param array   $states
581
     * @param array   $actions
582
     * @param boolean $enable
583
     * @param integer $guestSpaceId
584
     * @return array
585
     */
586 23
    public function putStatus($id, array $states, array $actions, $enable = true, $guestSpaceId = null): array
587
    {
588 23
        $options = [
589 23
            'json' => [
590 23
                'app' => $id,
591 23
                'enable' => $enable,
592 23
                'states' => $states,
593 23
                'actions' => $actions
594 23
            ]
595 23
        ];
596
597
        /** @var JsonStream $stream */
598 23
        $stream = $this->client
599 23
            ->put(KintoneApi::generateUrl('preview/app/status.json', $guestSpaceId), $options)
600 23
            ->getBody();
601
602 23
        return $stream->jsonSerialize();
603
    }
604
}
605