We have detected an error in your notification set-up
(Event-ID dab39dc24f564ec7bd4628d1305fd03c).
Currently, we cannot inform you about inspection progress.
Please check that the user
557058:bca11929-8c2d-43f2-8a82-c5416880d395 still has access to your repository or
update the API account.
Complex classes like Client often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class Client extends ClientListener implements ClientInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $options = array( |
||
| 30 | 'base_url' => 'https://api.bitbucket.org', |
||
| 31 | 'api_version' => '1.0', |
||
| 32 | 'api_versions' => array('1.0', '2.0'), // supported versions |
||
| 33 | 'format' => 'json', |
||
| 34 | 'formats' => array('json', 'xml'), // supported response formats |
||
| 35 | 'user_agent' => 'bitbucket-api-php/1.1.0 (https://bitbucket.org/gentlero/bitbucket-api)', |
||
| 36 | 'timeout' => 10, |
||
| 37 | 'verify_peer' => true |
||
| 38 | ); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var BuzzClientInterface |
||
| 42 | */ |
||
| 43 | protected $client; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var RequestInterface |
||
| 47 | */ |
||
| 48 | private $lastRequest; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var MessageInterface |
||
| 52 | */ |
||
| 53 | private $lastResponse; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var MessageInterface |
||
| 57 | */ |
||
| 58 | protected $responseObj; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var RequestInterface |
||
| 62 | */ |
||
| 63 | protected $requestObj; |
||
| 64 | |||
| 65 | 305 | public function __construct(array $options = array(), BuzzClientInterface $client = null) |
|
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | */ |
||
| 77 | 8 | public function get($endpoint, $params = array(), $headers = array()) |
|
| 78 | { |
||
| 79 | 8 | if (is_array($params) && count($params) > 0) { |
|
| 80 | 1 | $endpoint .= (strpos($endpoint, '?') === false ? '?' : '&').http_build_query($params, '', '&'); |
|
| 81 | 1 | $params = array(); |
|
| 82 | } |
||
| 83 | |||
| 84 | 8 | return $this->request($endpoint, $params, 'GET', $headers); |
|
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | 1 | public function post($endpoint, $params = array(), $headers = array()) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * {@inheritdoc} |
||
| 97 | */ |
||
| 98 | 1 | public function put($endpoint, $params = array(), $headers = array()) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * {@inheritdoc} |
||
| 105 | */ |
||
| 106 | 1 | public function delete($endpoint, $params = array(), $headers = array()) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * {@inheritdoc} |
||
| 113 | */ |
||
| 114 | 14 | public function request($endpoint, $params = array(), $method = 'GET', array $headers = array()) |
|
| 115 | { |
||
| 116 | //$request = $this->createRequest($method, $endpoint); |
||
| 117 | 14 | $request = ($this->requestObj !== null) ? $this->requestObj : $this->createRequest($method, $endpoint); |
|
| 118 | |||
| 119 | // add a default content-type if none was set |
||
| 120 | 14 | if (empty($headers['Content-Type']) && in_array(strtoupper($method), array('POST', 'PUT'), true)) { |
|
| 121 | 2 | $headers['Content-Type'] = 'application/x-www-form-urlencoded'; |
|
| 122 | } |
||
| 123 | |||
| 124 | 14 | if (count($headers) > 0) { |
|
| 125 | 8 | $request->addHeaders($headers); |
|
| 126 | } |
||
| 127 | |||
| 128 | 14 | if (count($params) > 0) { |
|
| 129 | 4 | $request->setContent(is_array($params) ? http_build_query($params) : $params); |
|
| 130 | } |
||
| 131 | |||
| 132 | 14 | $response = is_object($this->responseObj) ? $this->responseObj : new Response(); |
|
| 133 | |||
| 134 | 14 | $this->executeListeners($request, 'preSend'); |
|
| 135 | |||
| 136 | 12 | $this->client->send($request, $response); |
|
| 137 | |||
| 138 | 12 | $this->executeListeners($request, 'postSend', $response); |
|
| 139 | |||
| 140 | 12 | $this->lastRequest = $request; |
|
| 141 | 12 | $this->lastResponse = $response; |
|
| 142 | |||
| 143 | 12 | return $response; |
|
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @access public |
||
| 148 | * @return BuzzClientInterface |
||
| 149 | */ |
||
| 150 | 1 | public function getClient() |
|
| 154 | |||
| 155 | /** |
||
| 156 | * {@inheritdoc} |
||
| 157 | */ |
||
| 158 | 11 | public function getResponseFormat() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * {@inheritdoc} |
||
| 165 | */ |
||
| 166 | 4 | public function setResponseFormat($format) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * {@inheritdoc} |
||
| 179 | */ |
||
| 180 | 15 | public function getApiVersion() |
|
| 184 | |||
| 185 | /** |
||
| 186 | * {@inheritdoc} |
||
| 187 | */ |
||
| 188 | 99 | public function setApiVersion($version) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Check if specified API version is the one currently in use. |
||
| 201 | * |
||
| 202 | * @access public |
||
| 203 | * @param float $version |
||
| 204 | * @return bool |
||
| 205 | */ |
||
| 206 | 1 | public function isApiVersion($version) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * {@inheritdoc} |
||
| 213 | */ |
||
| 214 | 14 | public function getApiBaseUrl() |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @access public |
||
| 221 | * @return MessageInterface |
||
| 222 | */ |
||
| 223 | 6 | public function getLastRequest() |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @access public |
||
| 230 | * @return RequestInterface |
||
| 231 | */ |
||
| 232 | 2 | public function getLastResponse() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * @access public |
||
| 239 | * @param MessageInterface $response |
||
| 240 | * @return void |
||
| 241 | */ |
||
| 242 | public function setResponse(MessageInterface $response) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @access public |
||
| 249 | * @param RequestInterface $request |
||
| 250 | * @return void |
||
| 251 | */ |
||
| 252 | 1 | public function setRequest(RequestInterface $request) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * @access protected |
||
| 259 | * @param string $method |
||
| 260 | * @param string $url |
||
| 261 | * @return RequestInterface |
||
| 262 | */ |
||
| 263 | 13 | protected function createRequest($method, $url) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Execute all available listeners. |
||
| 288 | * |
||
| 289 | * $when can be: preSend or postSend |
||
| 290 | * |
||
| 291 | * @access protected |
||
| 292 | * @param RequestInterface $request |
||
| 293 | * @param string $when When to execute the listener |
||
| 294 | * @param MessageInterface $response |
||
| 295 | */ |
||
| 296 | 14 | protected function executeListeners(RequestInterface $request, $when = 'preSend', MessageInterface $response = null) |
|
| 321 | } |
||
| 322 |