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

Csv::post()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace CybozuHttp\Api\User;
4
5
use CybozuHttp\Api\Kintone\File;
6
use CybozuHttp\Client;
7
use CybozuHttp\Api\UserApi;
8
use CybozuHttp\Middleware\JsonStream;
9
10
/**
11
 * @author ochi51 <[email protected]>
12
 */
13
class Csv
14
{
15
    private static $type = [
16
        'user',
17
        'organization',
18
        'title',
19
        'group',
20
        'userOrganizations',
21
        'userGroups',
22
        'userServices'
23
    ];
24
25
    /**
26
     * @var Client
27
     */
28
    private $client;
29
30
    public function __construct(Client $client)
31
    {
32
        $this->client = $client;
33
    }
34
35
    /**
36
     * Get csv file
37
     *
38
     * @param string $type
39
     * @return string
40
     * @throws \InvalidArgumentException
41
     */
42
    public function get($type): string
43
    {
44
        if (!in_array($type, self::$type, true)) {
45
            throw new \InvalidArgumentException('Invalid type parameter');
46
        }
47
48
        $content = (string)$this->client
49
            ->get(UserApi::generateUrl("csv/{$type}.csv"))
50
            ->getBody();
51
52
        return $content;
53
    }
54
55
    /**
56
     * Post csv file
57
     *
58
     * @param string $type
59
     * @param string $filename
60
     * @return int
61
     * @throws \InvalidArgumentException
62
     */
63
    public function post($type, $filename): int
64
    {
65
        return $this->postKey($type, $this->file($filename));
66
    }
67
68
    /**
69
     * Post file key
70
     *
71
     * @param string $type
72
     * @param string $fileKey
73
     * @return int
74
     * @throws \InvalidArgumentException
75
     */
76
    public function postKey($type, $fileKey): int
77
    {
78
        if (!in_array($type, self::$type, true)) {
79
            throw new \InvalidArgumentException('Invalid type parameter');
80
        }
81
82
        $options = ['json' => ['fileKey' => $fileKey]];
83
84
        /** @var JsonStream $stream */
85
        $stream = $this->client
86
            ->post(UserApi::generateUrl("csv/{$type}.json"), $options)
87
            ->getBody();
88
89
        return $stream->jsonSerialize()['id'];
90
    }
91
92
    /**
93
     * Post file
94
     * https://cybozudev.zendesk.com/hc/ja/articles/202350470
95
     *
96
     * @param string $filename
97
     * @return string
98
     */
99
    public function file($filename): string
100
    {
101
        $options = ['multipart' =>  [
102
            [
103
                'name' => 'file',
104
                'filename' => File::getFilename($filename),
105
                'contents' => fopen($filename, 'rb'),
106
                'headers' => [
107
                    'Content-Type' => mime_content_type($filename)
108
                ]
109
            ]
110
        ]];
111
112
        /** @var JsonStream $stream */
113
        $stream = $this->client
114
            ->post(UserApi::generateUrl('file.json'), $options)
115
            ->getBody();
116
117
        return $stream->jsonSerialize()['fileKey'];
118
    }
119
120
    /**
121
     * Get post csv result
122
     * https://cybozudev.zendesk.com/hc/ja/articles/202361320
123
     *
124
     * @param int $id
125
     * @return array
126
     */
127
    public function result($id): array
128
    {
129
        $options = ['query' => ['id' => $id]];
130
131
        /** @var JsonStream $stream */
132
        $stream = $this->client
133
            ->get(UserApi::generateUrl('csv/result.json'), $options)
134
            ->getBody();
135
136
        return $stream->jsonSerialize();
137
    }
138
}
139