Passed
Pull Request — master (#77)
by Yuichi
04:41 queued 02:24
created

Users::getByCsv()   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 0
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\Client;
6
use CybozuHttp\Api\UserApi;
7
use CybozuHttp\Middleware\JsonStream;
8
9
/**
10
 * @author ochi51 <[email protected]>
11
 */
12
class Users
13
{
14
    public const MAX_GET_USERS = 100;
15
16
    /**
17
     * @var Client
18
     */
19
    private $client;
20
21
    /**
22
     * @var Csv
23
     */
24
    private $csv;
25
26
    public function __construct(Client $client, Csv $csv)
27
    {
28
        $this->client = $client;
29
        $this->csv = $csv;
30
    }
31
32
    /**
33
     * Get users
34
     * https://cybozudev.zendesk.com/hc/ja/articles/202363040#step2
35
     *
36
     * @param array $ids
37
     * @param array $codes
38
     * @param integer $offset
39
     * @param integer $limit
40
     * @return array
41
     */
42
    public function get(array $ids = [], array $codes = [], $offset = 0, $limit = self::MAX_GET_USERS): array
43
    {
44
        $options = ['json' => compact('ids', 'codes')];
45
        $options['json']['size'] = $limit;
46
        $options['json']['offset'] = $offset;
47
48
        /** @var JsonStream $stream */
49
        $stream = $this->client
50
            ->get(UserApi::generateUrl('users.json'), $options)
51
            ->getBody();
52
53
        return $stream->jsonSerialize()['users'];
54
    }
55
56
    /**
57
     * Get users by csv
58
     * https://cybozudev.zendesk.com/hc/ja/articles/202363040#step1
59
     *
60
     * @return string
61
     * @throws \InvalidArgumentException
62
     */
63
    public function getByCsv(): string
64
    {
65
        return $this->csv->get('user');
66
    }
67
68
    /**
69
     * Post users by csv
70
     * https://cybozudev.zendesk.com/hc/ja/articles/202111404
71
     *
72
     * @param $filename
73
     * @return int
74
     * @throws \InvalidArgumentException
75
     */
76
    public function postByCsv($filename): int
77
    {
78
        return $this->csv->post('user', $filename);
79
    }
80
}
81