Passed
Push — master ( ff2040...6f6996 )
by KwangSeob
02:18
created

UserService::getMyself()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace JiraRestApi\User;
4
5
use JiraRestApi\Issue\Reporter;
6
7
/**
8
 * Class to perform all user related queries.
9
 *
10
 * @author Anik
11
 */
12
class UserService extends \JiraRestApi\JiraClient
13
{
14
    private $uri = '/user';
15
16
    /**
17
     * Function to create a new user.
18
     *
19
     * @param User|array $user
20
     *
21
     * @throws \JiraRestApi\JiraException
22
     * @throws \JsonMapper_Exception
23
     *
24
     * @return User|object User class
25
     */
26
    public function create($user)
27
    {
28
        $data = json_encode($user);
29
30
        $this->log->info("Create User=\n".$data);
31
32
        $ret = $this->exec($this->uri, $data, 'POST');
33
34
        return $this->json_mapper->map(
35
            json_decode($ret), new User()
36
        );
37
    }
38
39
    /**
40
     * Function to get user.
41
     *
42
     * @param array $paramArray Possible values for $paramArray 'username', 'key'.
43
     *                          "Either the 'username' or the 'key' query parameters need to be provided".
44
     *
45
     * @throws \JiraRestApi\JiraException
46
     * @throws \JsonMapper_Exception
47
     *
48
     * @return User|object User class
49
     */
50
    public function get($paramArray)
51
    {
52
        $queryParam = '?'.http_build_query($paramArray);
53
54
        $ret = $this->exec($this->uri.$queryParam, null);
55
56
        $this->log->info("Result=\n".$ret);
57
58
        return $this->json_mapper->map(
59
                json_decode($ret), new User()
60
        );
61
    }
62
63
    /**
64
     * Returns a list of users that match the search string and/or property.
65
     *
66
     * @param array $paramArray
67
     *
68
     * @throws \JiraRestApi\JiraException
69
     * @throws \JsonMapper_Exception
70
     *
71
     * @return User[]
72
     */
73
    public function findUsers($paramArray)
74
    {
75
        $queryParam = '?'.http_build_query($paramArray);
76
77
        $ret = $this->exec($this->uri.'/search'.$queryParam, null);
78
79
        $this->log->info("Result=\n".$ret);
80
81
        $userData = json_decode($ret);
82
        $users = [];
83
84
        foreach ($userData as $user) {
85
            $users[] = $this->json_mapper->map(
86
                $user, new User()
87
            );
88
        }
89
90
        return $users;
91
    }
92
93
    /**
94
     * Returns a list of users that match the search string.
95
     * Please note that this resource should be called with an issue key when a list of assignable users is retrieved for editing.
96
     *
97
     * @param array $paramArray
98
     *
99
     * @throws \JiraRestApi\JiraException
100
     * @throws \JsonMapper_Exception
101
     *
102
     * @return User[]
103
     *
104
     * @see https://docs.atlassian.com/jira/REST/cloud/#api/2/user-findAssignableUsers
105
     */
106
    public function findAssignableUsers($paramArray)
107
    {
108
        $queryParam = '?'.http_build_query($paramArray);
109
110
        $ret = $this->exec($this->uri.'/assignable/search'.$queryParam, null);
111
112
        $this->log->info("Result=\n".$ret);
113
114
        $userData = json_decode($ret);
115
        $users = [];
116
117
        foreach ($userData as $user) {
118
            $users[] = $this->json_mapper->map(
119
                $user, new User()
120
            );
121
        }
122
123
        return $users;
124
    }
125
126
    /**
127
     * Delete a User.
128
     *
129
     * @param $paramArray
130
     *
131
     * @throws \JiraRestApi\JiraException
132
     *
133
     * @return bool
134
     */
135
    public function deleteUser($paramArray)
136
    {
137
        $queryParam = '?'.http_build_query($paramArray);
138
139
        $ret = $this->exec($this->uri.$queryParam, null, 'DELETE');
140
141
        return $ret;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $ret returns the type string which is incompatible with the documented return type boolean.
Loading history...
142
    }
143
144
    /**
145
     * get a user info details.
146
     *
147
     * @throws \JiraRestApi\JiraException
148
     *
149
     * @return Reporter user Object
150
     */
151
    public function getMyself()
152
    {
153
        $ret = $this->exec('myself', null);
154
155
        $user = $this->json_mapper->map(
156
            json_decode($ret), new Reporter()
157
        );
158
159
        return $user;
160
    }
161
}
162