|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Teazee package. |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @license MIT License |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace Teazee\Provider; |
|
11
|
|
|
|
|
12
|
|
|
use Http\Client\HttpClient; |
|
13
|
|
|
use Http\Message\MessageFactory; |
|
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
15
|
|
|
use Psr\Http\Message\UriInterface; |
|
16
|
|
|
use Teazee\Exception\ServiceMissingException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Michael Crumm <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
abstract class AbstractHttpProvider extends AbstractProvider |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var HttpClient |
|
25
|
|
|
*/ |
|
26
|
|
|
private $client; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var MessageFactory |
|
30
|
|
|
*/ |
|
31
|
|
|
private $messageFactory; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* AbstractHttpProvider Constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param HttpClient $client HttpClient makes HTTP requests. |
|
37
|
|
|
* @param MessageFactory $messageFactory MessageFactory creates Request objects. |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(HttpClient $client = null, MessageFactory $messageFactory = null) |
|
40
|
|
|
{ |
|
41
|
10 |
|
parent::__construct(); |
|
42
|
|
|
|
|
43
|
|
|
if (null === $client && !class_exists('Http\Discovery\HttpClientDiscovery')) { |
|
44
|
1 |
|
throw ServiceMissingException::noHttpClient(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
10 |
|
$this->client = $client ?: \Http\Discovery\HttpClientDiscovery::find(); |
|
48
|
|
|
|
|
49
|
|
|
if (null === $messageFactory && !class_exists('Http\Discovery\MessageFactoryDiscovery')) { |
|
50
|
1 |
|
throw ServiceMissingException::noMessageFactory(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
10 |
|
$this->messageFactory = $messageFactory ?: \Http\Discovery\MessageFactoryDiscovery::find(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Returns the HttpClient instance for this Provider. |
|
58
|
|
|
* |
|
59
|
|
|
* @return HttpClient |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getClient() |
|
62
|
|
|
{ |
|
63
|
1 |
|
return $this->client; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns a ResponseInterface for the given URI/method. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string|UriInterface $uri Request URI. |
|
70
|
|
|
* @param string $method HTTP method (Defaults to 'GET'). |
|
71
|
|
|
* |
|
72
|
|
|
* @return ResponseInterface |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function getResponse($uri, $method = 'GET') |
|
75
|
|
|
{ |
|
76
|
6 |
|
$request = $this->messageFactory->createRequest($method, $uri); |
|
77
|
|
|
|
|
78
|
6 |
|
return $this->client->sendRequest($request); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|