Skip to content

Commit 1aa026d

Browse files
committed
Refactor of Client's code:
Don't create default HttpClient instance till it's needed Moved part of authentication code to HttpClient DRYied AuthListener a bit
1 parent b5364df commit 1aa026d

File tree

4 files changed

+52
-28
lines changed

4 files changed

+52
-28
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"extra": {
2828
"branch-alias": {
29-
"dev-master": "1.1.x-dev"
29+
"dev-master": "1.2.x-dev"
3030
}
3131
}
3232
}

lib/Github/Client.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Github\Exception\InvalidArgumentException;
1010
use Github\HttpClient\HttpClient;
1111
use Github\HttpClient\HttpClientInterface;
12-
use Github\HttpClient\Listener\AuthListener;
1312

1413
/**
1514
* Simple yet very cool PHP GitHub client
@@ -73,7 +72,9 @@ class Client
7372
*/
7473
public function __construct(HttpClientInterface $httpClient = null)
7574
{
76-
$this->httpClient = $httpClient ?: new HttpClient($this->options);
75+
if (null !== $httpClient) {
76+
$this->httpClient = $httpClient;
77+
}
7778
}
7879

7980
/**
@@ -134,7 +135,7 @@ public function api($name)
134135
break;
135136

136137
default:
137-
throw new InvalidArgumentException();
138+
throw new InvalidArgumentException(sprintf('Undefined api instance called: "%s"', $name));
138139
}
139140

140141
return $api;
@@ -160,22 +161,18 @@ public function authenticate($tokenOrLogin, $password = null, $authMethod = null
160161
$password = null;
161162
}
162163

163-
$this->httpClient->addListener(
164-
new AuthListener(
165-
$authMethod,
166-
array(
167-
'tokenOrLogin' => $tokenOrLogin,
168-
'password' => $password
169-
)
170-
)
171-
);
164+
$this->httpClient->authenticate($tokenOrLogin, $password, $authMethod);
172165
}
173166

174167
/**
175168
* @return HttpClient
176169
*/
177170
public function getHttpClient()
178171
{
172+
if (null === $this->httpClient) {
173+
$this->httpClient = new HttpClient($this->options);
174+
}
175+
179176
return $this->httpClient;
180177
}
181178

@@ -192,15 +189,15 @@ public function setHttpClient(HttpClientInterface $httpClient)
192189
*/
193190
public function clearHeaders()
194191
{
195-
$this->httpClient->clearHeaders();
192+
$this->getHttpClient()->clearHeaders();
196193
}
197194

198195
/**
199196
* @param array $headers
200197
*/
201198
public function setHeaders(array $headers)
202199
{
203-
$this->httpClient->setHeaders($headers);
200+
$this->getHttpClient()->setHeaders($headers);
204201
}
205202

206203
/**

lib/Github/HttpClient/HttpClient.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Github\Exception\ErrorException;
1111
use Github\Exception\RuntimeException;
12+
use Github\HttpClient\Listener\AuthListener;
1213
use Github\HttpClient\Listener\ErrorListener;
1314
use Github\HttpClient\Message\Request;
1415
use Github\HttpClient\Message\Response;
@@ -65,6 +66,19 @@ public function __construct(array $options = array(), ClientInterface $client =
6566
$this->clearHeaders();
6667
}
6768

69+
public function authenticate($tokenOrLogin, $password, $authMethod)
70+
{
71+
$this->addListener(
72+
new AuthListener(
73+
$authMethod,
74+
array(
75+
'tokenOrLogin' => $tokenOrLogin,
76+
'password' => $password
77+
)
78+
)
79+
);
80+
}
81+
6882
/**
6983
* {@inheritDoc}
7084
*/

lib/Github/HttpClient/Listener/AuthListener.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,26 @@ public function preSend(RequestInterface $request)
6868
throw new InvalidArgumentException('You need to set client_id and client_secret!');
6969
}
7070

71-
$url = $request->getUrl();
72-
73-
$parameters = array(
74-
'client_id' => $this->options['tokenOrLogin'],
75-
'client_secret' => $this->options['password'],
71+
$this->setRequestUrl(
72+
$request,
73+
array(
74+
'client_id' => $this->options['tokenOrLogin'],
75+
'client_secret' => $this->options['password'],
76+
)
7677
);
77-
78-
$url .= (false === strpos($url, '?') ? '?' : '&').utf8_encode(http_build_query($parameters, '', '&'));
79-
80-
$request->fromUrl(new Url($url));
8178
break;
8279

8380
case Client::AUTH_URL_TOKEN:
8481
if (!isset($this->options['tokenOrLogin'])) {
8582
throw new InvalidArgumentException('You need to set OAuth token!');
8683
}
8784

88-
$url = $request->getUrl();
89-
$url .= (false === strpos($url, '?') ? '?' : '&').utf8_encode(http_build_query(array('access_token' => $this->options['tokenOrLogin']), '', '&'));
90-
91-
$request->fromUrl(new Url($url));
85+
$this->setRequestUrl(
86+
$request,
87+
array(
88+
'access_token' => $this->options['tokenOrLogin'],
89+
)
90+
);
9291
break;
9392

9493
default:
@@ -102,4 +101,18 @@ public function preSend(RequestInterface $request)
102101
public function postSend(RequestInterface $request, MessageInterface $response)
103102
{
104103
}
104+
105+
/**
106+
* @param RequestInterface $request
107+
* @param array $parameters
108+
*
109+
* @return Url
110+
*/
111+
private function setRequestUrl(RequestInterface $request, array $parameters = array())
112+
{
113+
$url = $request->getUrl();
114+
$url .= (false === strpos($url, '?') ? '?' : '&').utf8_encode(http_build_query($parameters, '', '&'));
115+
116+
$request->fromUrl(new Url($url));
117+
}
105118
}

0 commit comments

Comments
 (0)