Skip to content

Commit 3178cd3

Browse files
authored
[3.0] Implement HTTP client using httplug (#137)
* Implement only HTTP client using httplug * Bump minimum PHP test
1 parent b1440bd commit 3178cd3

File tree

11 files changed

+307
-133
lines changed

11 files changed

+307
-133
lines changed

.travis.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
language: php
22

33
php:
4-
- 5.3
5-
- 5.4
6-
- 5.5
74
- 5.6
85
- 7.0
96
- 7.1
@@ -24,9 +21,9 @@ env:
2421

2522
matrix:
2623
include:
27-
- php: 5.3
24+
- php: 5.6
2825
env: setup=lowest
29-
- php: 5.5
26+
- php: 7.0
3027
env: setup=lowest
3128

3229
install:

composer.json

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -27,52 +27,29 @@
2727
"classmap": ["src/Omnipay/Omnipay.php"]
2828
},
2929
"require": {
30-
"php": ">=5.3.2",
31-
"guzzle/guzzle": "~3.9",
32-
"symfony/http-foundation": "~2.1|~3.0"
30+
"php": "^5.6|^7",
31+
"php-http/client-implementation": "^1",
32+
"php-http/message": "^1.5",
33+
"php-http/discovery": "^1.2.1",
34+
"symfony/http-foundation": "^2.1|^3"
3335
},
3436
"require-dev": {
35-
"omnipay/tests": "~2.0",
36-
"squizlabs/php_codesniffer": "~1.5"
37+
"omnipay/tests": "^3",
38+
"squizlabs/php_codesniffer": "^2.8.1"
3739
},
3840
"extra": {
3941
"branch-alias": {
40-
"dev-master": "2.5.x-dev"
41-
},
42-
"gateways": [
43-
"AuthorizeNet_AIM",
44-
"AuthorizeNet_SIM",
45-
"Buckaroo_CreditCard",
46-
"Buckaroo_Ideal",
47-
"Buckaroo_PayPal",
48-
"CardSave",
49-
"Coinbase",
50-
"Dummy",
51-
"Eway_Rapid",
52-
"FirstData_Connect",
53-
"GoCardless",
54-
"Manual",
55-
"Migs_ThreeParty",
56-
"Migs_TwoParty",
57-
"Mollie",
58-
"MultiSafepay",
59-
"Netaxept",
60-
"NetBanx",
61-
"PayFast",
62-
"Payflow_Pro",
63-
"PaymentExpress_PxPay",
64-
"PaymentExpress_PxPost",
65-
"PayPal_Express",
66-
"PayPal_Pro",
67-
"Pin",
68-
"SagePay_Direct",
69-
"SagePay_Server",
70-
"SecurePay_DirectPost",
71-
"Stripe",
72-
"TargetPay_Directebanking",
73-
"TargetPay_Ideal",
74-
"TargetPay_Mrcash",
75-
"WorldPay"
76-
]
77-
}
42+
"dev-master": "3.0.x-dev"
43+
}
44+
},
45+
"suggest": {
46+
"php-http/guzzle6-adapter": "Use the Guzzle adapter to send your requests."
47+
},
48+
"scripts": {
49+
"test": "phpunit",
50+
"check-style": "phpcs -p --standard=PSR2 src/",
51+
"fix-style": "phpcbf -p --standard=PSR2 src/"
52+
},
53+
"minimum-stability": "dev",
54+
"prefer-stable": true
7855
}

src/Omnipay/Common/AbstractGateway.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
namespace Omnipay\Common;
77

8-
use Guzzle\Http\ClientInterface;
9-
use Guzzle\Http\Client as HttpClient;
8+
use Omnipay\Common\Http\Client;
109
use Symfony\Component\HttpFoundation\ParameterBag;
1110
use Symfony\Component\HttpFoundation\Request as HttpRequest;
1211

@@ -50,7 +49,7 @@ abstract class AbstractGateway implements GatewayInterface
5049
protected $parameters;
5150

5251
/**
53-
* @var \Guzzle\Http\ClientInterface
52+
* @var Client
5453
*/
5554
protected $httpClient;
5655

@@ -62,10 +61,10 @@ abstract class AbstractGateway implements GatewayInterface
6261
/**
6362
* Create a new gateway instance
6463
*
65-
* @param ClientInterface $httpClient A Guzzle client to make API calls with
64+
* @param Client $httpClient A HTTP client to make API calls with
6665
* @param HttpRequest $httpRequest A Symfony HTTP request object
6766
*/
68-
public function __construct(ClientInterface $httpClient = null, HttpRequest $httpRequest = null)
67+
public function __construct(Client $httpClient = null, HttpRequest $httpRequest = null)
6968
{
7069
$this->httpClient = $httpClient ?: $this->getDefaultHttpClient();
7170
$this->httpRequest = $httpRequest ?: $this->getDefaultHttpRequest();
@@ -327,16 +326,11 @@ protected function createRequest($class, array $parameters)
327326
/**
328327
* Get the global default HTTP client.
329328
*
330-
* @return HttpClient
329+
* @return Client
331330
*/
332331
protected function getDefaultHttpClient()
333332
{
334-
return new HttpClient(
335-
'',
336-
array(
337-
'curl.options' => array(CURLOPT_CONNECTTIMEOUT => 60),
338-
)
339-
);
333+
return new Client();
340334
}
341335

342336
/**

src/Omnipay/Common/GatewayFactory.php

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
namespace Omnipay\Common;
77

8-
use Guzzle\Http\ClientInterface;
98
use Omnipay\Common\Exception\RuntimeException;
9+
use Omnipay\Common\Http\Client;
1010
use Symfony\Component\HttpFoundation\Request as HttpRequest;
1111

1212
/**
@@ -69,35 +69,16 @@ public function register($className)
6969
}
7070
}
7171

72-
/**
73-
* Automatically find and register all officially supported gateways
74-
*
75-
* @return array An array of gateway names
76-
*/
77-
public function find()
78-
{
79-
foreach ($this->getSupportedGateways() as $gateway) {
80-
$class = Helper::getGatewayClassName($gateway);
81-
if (class_exists($class)) {
82-
$this->register($gateway);
83-
}
84-
}
85-
86-
ksort($this->gateways);
87-
88-
return $this->all();
89-
}
90-
9172
/**
9273
* Create a new gateway instance
9374
*
9475
* @param string $class Gateway name
95-
* @param ClientInterface|null $httpClient A Guzzle HTTP Client implementation
76+
* @param Client|null $httpClient A HTTP Client implementation
9677
* @param HttpRequest|null $httpRequest A Symfony HTTP Request implementation
9778
* @throws RuntimeException If no such gateway is found
9879
* @return GatewayInterface An object of class $class is created and returned
9980
*/
100-
public function create($class, ClientInterface $httpClient = null, HttpRequest $httpRequest = null)
81+
public function create($class, Client $httpClient = null, HttpRequest $httpRequest = null)
10182
{
10283
$class = Helper::getGatewayClassName($class);
10384

@@ -107,16 +88,4 @@ public function create($class, ClientInterface $httpClient = null, HttpRequest $
10788

10889
return new $class($httpClient, $httpRequest);
10990
}
110-
111-
/**
112-
* Get a list of supported gateways which may be available
113-
*
114-
* @return array
115-
*/
116-
public function getSupportedGateways()
117-
{
118-
$package = json_decode(file_get_contents(__DIR__.'/../../../composer.json'), true);
119-
120-
return $package['extra']['gateways'];
121-
}
12291
}

src/Omnipay/Common/Http/Client.php

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
<?php
2+
3+
namespace Omnipay\Common\Http;
4+
5+
use Http\Client\HttpClient;
6+
use Http\Discovery\HttpClientDiscovery;
7+
use Http\Discovery\MessageFactoryDiscovery;
8+
use Http\Message\RequestFactory;
9+
use Psr\Http\Message\RequestInterface;
10+
use Psr\Http\Message\ResponseInterface;
11+
use Psr\Http\Message\StreamInterface;
12+
use Psr\Http\Message\UriInterface;
13+
14+
class Client implements HttpClient, RequestFactory
15+
{
16+
/**
17+
* @var HttpClient
18+
*/
19+
private $httpClient;
20+
21+
/**
22+
* @var RequestFactory
23+
*/
24+
private $requestFactory;
25+
26+
public function __construct(HttpClient $httpClient = null, RequestFactory $requestFactory = null)
27+
{
28+
$this->httpClient = $httpClient ?: HttpClientDiscovery::find();
29+
$this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find();
30+
}
31+
32+
/**
33+
* @param $method
34+
* @param $uri
35+
* @param array $headers
36+
* @param null $body
37+
* @param string $protocolVersion
38+
* @return RequestInterface
39+
*/
40+
public function createRequest($method, $uri, array $headers = [], $body = null, $protocolVersion = '1.1')
41+
{
42+
return $this->requestFactory->createRequest($method, $uri, $headers, $body, $protocolVersion);
43+
}
44+
45+
/**
46+
* @param RequestInterface $request
47+
* @return ResponseInterface
48+
*/
49+
public function sendRequest(RequestInterface $request)
50+
{
51+
return $this->httpClient->sendRequest($request);
52+
}
53+
54+
/**
55+
* Send a GET request.
56+
*
57+
* @param UriInterface|string $uri
58+
* @param array $headers
59+
* @return ResponseInterface
60+
*/
61+
public function get($uri, array $headers = [])
62+
{
63+
$request = $this->createRequest(
64+
'GET',
65+
$uri,
66+
$headers
67+
);
68+
69+
return $this->sendRequest($request);
70+
}
71+
72+
/**
73+
* Send a POST request.
74+
*
75+
* @param UriInterface|string $uri
76+
* @param array $headers
77+
* @param string|null|resource|StreamInterface $body
78+
* @return ResponseInterface
79+
*/
80+
public function post($uri, array $headers = [], $body = null)
81+
{
82+
$request = $this->createRequest(
83+
'POST',
84+
$uri,
85+
$headers,
86+
$body
87+
);
88+
89+
return $this->sendRequest($request);
90+
}
91+
92+
/**
93+
* Send a PUT request.
94+
*
95+
* @param UriInterface|string $uri
96+
* @param array $headers
97+
* @param string|null|resource|StreamInterface $body
98+
* @return ResponseInterface
99+
*/
100+
public function put($uri, array $headers = [], $body = null)
101+
{
102+
$request = $this->createRequest(
103+
'PUT',
104+
$uri,
105+
$headers,
106+
$body
107+
);
108+
109+
return $this->sendRequest($request);
110+
}
111+
112+
/**
113+
* Send a PATCH request.
114+
*
115+
* @param UriInterface|string $uri
116+
* @param array $headers
117+
* @param string|null|resource|StreamInterface $body
118+
* @return ResponseInterface
119+
*/
120+
public function patch($uri, array $headers = [], $body = null)
121+
{
122+
$request = $this->createRequest(
123+
'PATCH',
124+
$uri,
125+
$headers,
126+
$body
127+
);
128+
129+
return $this->sendRequest($request);
130+
}
131+
132+
/**
133+
* Send a DELETE request.
134+
*
135+
* @param UriInterface|string $uri
136+
* @param array $headers
137+
* @param string|null|resource|StreamInterface $body
138+
* @return ResponseInterface
139+
*/
140+
public function delete($uri, array $headers = [], $body = null)
141+
{
142+
$request = $this->createRequest(
143+
'DELETE',
144+
$uri,
145+
$headers,
146+
$body
147+
);
148+
149+
return $this->sendRequest($request);
150+
}
151+
152+
/**
153+
* Send a HEAD request.
154+
*
155+
* @param UriInterface|string $uri
156+
* @param array $headers
157+
* @return ResponseInterface
158+
*/
159+
public function head($uri, array $headers = [])
160+
{
161+
$request = $this->createRequest(
162+
'HEAD',
163+
$uri,
164+
$headers
165+
);
166+
167+
return $this->sendRequest($request);
168+
}
169+
170+
/**
171+
* Send a OPTIONS request.
172+
*
173+
* @param UriInterface|string $uri
174+
* @return ResponseInterface
175+
*/
176+
public function options($uri)
177+
{
178+
$request = $this->createRequest(
179+
'OPTIONS',
180+
$uri
181+
);
182+
183+
return $this->sendRequest($request);
184+
}
185+
}

0 commit comments

Comments
 (0)