Skip to content

Commit 3160b9c

Browse files
committed
Adding a factory class
1 parent c915187 commit 3160b9c

File tree

7 files changed

+28
-14
lines changed

7 files changed

+28
-14
lines changed

doc/api_version.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ For example:
88
$client = new Github\Client();
99
echo $client->getApiVersion(); // prints "v3"
1010

11-
$client = new Github\Client($httpClient, 'v2');
11+
$client = new Github\Client(new Github\HttpClient\Builder($httpClient), 'v2');
1212
echo $client->getApiVersion(); // prints "v2"
1313
```

doc/customize.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ community provided clients is found here: https://packagist.org/providers/php-ht
1111
You can inject a HTTP client through the `Github\Client` constructor:
1212

1313
```php
14-
$client = new Github\Client(new Http\Adapter\Guzzle6\Client());
14+
$client = Github\Client::createFromHttpClient(new Http\Adapter\Guzzle6\Client());
1515
```
1616

1717
### Configure the HTTP client
@@ -39,7 +39,7 @@ class CustomUserAgentPlugin implements Plugin
3939
$httpBuilder = new Github\HttpClient\Builder(new Http\Adapter\Guzzle6\Client());
4040
$httpBuilder->addPlugin(new CustomUserAgentPlugin());
4141

42-
$client = new Github\Client(null, $httpBuilder);
42+
$client = new Github\Client($httpBuilder);
4343
```
4444

4545
### Run Test Suite

doc/security.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ The following sample code authenticates as an installation using [lcobucci/jwt](
5454
to generate a JSON Web Token (JWT).
5555

5656
```php
57-
$github = new Github\Client(new GuzzleClient(), 'machine-man-preview');
57+
$builder = new Github\HttpClient\Builder(new GuzzleClient());
58+
$github = new Github\Client($builder, 'machine-man-preview');
5859

5960
$jwt = (new Builder)
6061
->setIssuer($integrationId)

lib/Github/Client.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,14 @@ class Client
105105
/**
106106
* Instantiate a new GitHub client.
107107
*
108-
* @param HttpClient|null $httpClient
109-
* @param Builder|null $httpClientBuilder If a builder is provided we assume that $httpClient is added to the builder already.
110-
* @param string|null $apiVersion
111-
* @param string|null $enterpriseUrl
108+
* @param Builder|null $httpClientBuilder
109+
* @param string|null $apiVersion
110+
* @param string|null $enterpriseUrl
112111
*/
113-
public function __construct(HttpClient $httpClient = null, Builder $httpClientBuilder = null, $apiVersion = null, $enterpriseUrl = null)
112+
public function __construct(Builder $httpClientBuilder = null, $apiVersion = null, $enterpriseUrl = null)
114113
{
115114
$this->responseHistory = new History();
116-
$this->httpClientBuilder = $builder = $httpClientBuilder ?: new Builder($httpClient);
115+
$this->httpClientBuilder = $builder = $httpClientBuilder ?: new Builder();
117116

118117
$builder->addPlugin(new GithubExceptionThrower());
119118
$builder->addPlugin(new Plugin\HistoryPlugin($this->responseHistory));
@@ -132,6 +131,20 @@ public function __construct(HttpClient $httpClient = null, Builder $httpClientBu
132131
}
133132
}
134133

134+
/**
135+
* Create a Github\Client using a HttpClient.
136+
*
137+
* @param HttpClient $httpClient
138+
*
139+
* @return Client
140+
*/
141+
public static function createFromHttpClient(HttpClient $httpClient)
142+
{
143+
$builder = new Builder($httpClient);
144+
145+
return new self($builder);
146+
}
147+
135148
/**
136149
* @param string $name
137150
*

test/Github/Tests/Api/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected function getApiMock()
2424
->expects($this->any())
2525
->method('sendRequest');
2626

27-
$client = new \Github\Client($httpClient);
27+
$client = \Github\Client::createFromHttpClient($httpClient);
2828

2929
return $this->getMockBuilder($this->getApiClass())
3030
->setMethods(array('get', 'post', 'postRaw', 'patch', 'delete', 'put', 'head'))

test/Github/Tests/ClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function shouldPassHttpClientInterfaceToConstructor()
2929
$httpClientMock = $this->getMockBuilder(\Http\Client\HttpClient::class)
3030
->getMock();
3131

32-
$client = new Client($httpClientMock);
32+
$client = Client::createFromHttpClient($httpClientMock);
3333

3434
$this->assertInstanceOf(\Http\Client\HttpClient::class, $client->getHttpClient());
3535
}

test/Github/Tests/ResultPagerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function shouldGetAllResults()
4040
->method('sendRequest')
4141
->will($this->returnValue($response));
4242

43-
$client = new Client($httpClientMock);
43+
$client = \Github\Client::createFromHttpClient($httpClientMock);
4444

4545
// memberApi Mock
4646
$memberApi = new Members($client);
@@ -87,7 +87,7 @@ public function shouldGetAllSearchResults()
8787
->method('sendRequest')
8888
->will($this->returnValue($response));
8989

90-
$client = new Client($httpClientMock);
90+
$client = \Github\Client::createFromHttpClient($httpClientMock);
9191

9292
$searchApi = new Search($client);
9393
$method = 'users';

0 commit comments

Comments
 (0)