TestRailAPIClient   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 16
eloc 53
c 1
b 1
f 0
dl 0
loc 160
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A _create_handle() 0 6 1
A set_password() 0 3 1
A send_post() 0 3 1
A set_user() 0 3 1
A get_user() 0 3 1
A get_password() 0 3 1
A send_get() 0 3 1
A __construct() 0 4 1
B _send_request() 0 62 7
A _close_handle() 0 3 1
1
<?php
2
namespace seretos\testrail\connector;
3
4
/**
5
 * TestRail API binding for PHP (API v2, available since TestRail 3.0)
6
 *
7
 * Learn more:
8
 *
9
 * http://docs.gurock.com/testrail-api2/start
10
 * http://docs.gurock.com/testrail-api2/accessing
11
 *
12
 * Copyright Gurock Software GmbH. See license.md for details.
13
 */
14
15
class TestRailAPIClient implements ApiConnectorInterface
16
{
17
    private $_user;
18
    private $_password;
19
    private $_url;
20
21
    public function __construct($base_url)
22
    {
23
        $base_url = trim($base_url, '/').'/';
24
        $this->_url = $base_url.'index.php?/api/v2/';
25
    }
26
27
    /**
28
     * Get/Set User
29
     *
30
     * Returns/sets the user used for authenticating the API requests.
31
     */
32
    public function get_user()
33
    {
34
        return $this->_user;
35
    }
36
37
    public function set_user($user)
38
    {
39
        $this->_user = $user;
40
    }
41
42
    /**
43
     * Get/Set Password
44
     *
45
     * Returns/sets the password used for authenticating the API requests.
46
     */
47
    public function get_password()
48
    {
49
        return $this->_password;
50
    }
51
52
    public function set_password($password)
53
    {
54
        $this->_password = $password;
55
    }
56
57
    /**
58
     * Send Get
59
     *
60
     * Issues a GET request (read) against the API and returns the result
61
     * (as PHP array).
62
     *
63
     * Arguments:
64
     *
65
     * $uri                 The API method to call including parameters
66
     *                      (e.g. get_case/1)
67
     * @throws TestRailAPIException
68
     */
69
    public function send_get($uri)
70
    {
71
        return $this->_send_request('GET', $uri, null);
72
    }
73
74
    /**
75
     * Send POST
76
     *
77
     * Issues a POST request (write) against the API and returns the result
78
     * (as PHP array).
79
     *
80
     * Arguments:
81
     *
82
     * $uri                 The API method to call including parameters
83
     *                      (e.g. add_case/1)
84
     * $data                The data to submit as part of the request (as
85
     *                      PHP array, strings must be UTF-8 encoded)
86
     * @throws TestRailAPIException
87
     */
88
    public function send_post($uri, $data)
89
    {
90
        return $this->_send_request('POST', $uri, $data);
91
    }
92
93
    protected function _create_handle()
94
    {
95
        $ch = curl_init();
96
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
97
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
98
        return $ch;
99
    }
100
101
    /**
102
     * @param $method
103
     * @param $uri
104
     * @param $data
105
     * @return array|mixed
106
     * @throws TestRailAPIException
107
     */
108
    protected function _send_request($method, $uri, $data)
109
    {
110
        $ch = $this->_create_handle();
111
        curl_setopt($ch, CURLOPT_URL, $this->_url.$uri);
112
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
113
114
        if ($method == 'POST')
115
        {
116
            if (is_array($data))
117
            {
118
                $data_str = json_encode($data);
119
            } else
120
            {
121
                $data_str = '';
122
            }
123
124
            curl_setopt($ch, CURLOPT_POST, true);
125
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_str);
126
        } else
127
        {
128
            curl_setopt($ch, CURLOPT_POST, false);
129
        }
130
131
        curl_setopt(
132
            $ch,
133
            CURLOPT_HTTPHEADER,
134
            array(
135
                'Expect: ',
136
                'Content-Type: application/json'
137
            )
138
        );
139
140
        curl_setopt($ch, CURLOPT_USERPWD, "$this->_user:$this->_password");
141
        $response = curl_exec($ch);
142
        if ($response === false)
143
        {
144
            throw new TestRailAPIException(curl_error($ch));
145
        }
146
147
        if ($response)
148
        {
149
            $result = json_decode($response, true); // As array
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

149
            $result = json_decode(/** @scrutinizer ignore-type */ $response, true); // As array
Loading history...
150
        } else
151
        {
152
            $result = array();
153
        }
154
155
        $info = curl_getinfo($ch);
156
        if ($info['http_code'] != 200)
157
        {
158
            throw new TestRailAPIException(
159
                sprintf(
160
                    'TestRail API returned HTTP %s (%s)',
161
                    $info['http_code'],
162
                    isset($result['error']) ?
163
                        '"'.$result['error'].'"' : 'No additional error message received'
164
                )
165
            );
166
        }
167
168
        $this->_close_handle($ch);
169
        return $result;
170
    }
171
172
    protected function _close_handle($ch)
173
    {
174
        curl_close($ch);
175
    }
176
}