Passed
Pull Request — master (#181)
by
unknown
02:01
created

UserService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 130
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

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