1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Klever\JustGivingApiSdk\Support; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\HandlerStack; |
7
|
|
|
use GuzzleHttp\Middleware; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class GuzzleClientFactory |
12
|
|
|
* |
13
|
|
|
* Helper class to instantiate a Guzzle HTTP client with the correct configuration for passing into the JustGiving |
14
|
|
|
* client. |
15
|
|
|
*/ |
16
|
|
|
class GuzzleClientFactory |
17
|
|
|
{ |
18
|
|
|
/** @var string */ |
19
|
|
|
protected $apiKey; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
protected $apiVersion; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
protected $username; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
protected $password; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
protected $rootDomain; |
32
|
|
|
|
33
|
|
|
/** @var array */ |
34
|
|
|
protected $userOptions; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* GuzzleClientFactory constructor. |
38
|
|
|
* |
39
|
|
|
* @param string $rootDomain |
40
|
|
|
* @param string $apiKey |
41
|
|
|
* @param string $apiVersion |
42
|
|
|
* @param string $username |
43
|
|
|
* @param string $password |
44
|
|
|
* @param array $options |
45
|
|
|
*/ |
46
|
89 |
|
public function __construct($rootDomain, $apiKey, $apiVersion, $username = '', $password = '', $options = []) |
47
|
|
|
{ |
48
|
89 |
|
$this->rootDomain = $rootDomain; |
49
|
89 |
|
$this->apiKey = $apiKey; |
50
|
89 |
|
$this->apiVersion = $apiVersion; |
51
|
89 |
|
$this->username = $username; |
52
|
89 |
|
$this->password = $password; |
53
|
89 |
|
$this->userOptions = $options; |
54
|
89 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Static method for easily creating a client. Requires the same parameters as the class constructor. |
58
|
|
|
* |
59
|
|
|
* @param array ...$args |
60
|
|
|
* @return Client |
61
|
|
|
*/ |
62
|
88 |
|
public static function build(...$args) |
63
|
|
|
{ |
64
|
88 |
|
return (new static(...$args))->createClient(); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Set up the handler stack with middleware, configure options and instantiate the Guzzle client. |
69
|
|
|
* |
70
|
|
|
* @return Client |
71
|
|
|
*/ |
72
|
88 |
|
public function createClient() |
73
|
|
|
{ |
74
|
88 |
|
$stack = HandlerStack::create(); |
75
|
88 |
|
$stack->push(Middleware::mapResponse(function(ResponseInterface $response) { |
76
|
74 |
|
return new Response($response); |
77
|
88 |
|
})); |
78
|
|
|
|
79
|
88 |
|
return new Client(array_merge([ |
80
|
88 |
|
'http_errors' => false, |
81
|
88 |
|
'handler' => $stack, |
82
|
88 |
|
'base_uri' => $this->baseUrl(), |
83
|
|
|
'headers' => [ |
84
|
88 |
|
'Accept' => 'application/json', |
85
|
88 |
|
'Authorize' => 'Basic ' . $this->buildAuthenticationValue(), |
86
|
88 |
|
'Authorization' => 'Basic ' . $this->buildAuthenticationValue(), |
87
|
|
|
] |
88
|
88 |
|
], $this->userOptions)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Return the base URL string for the API call. |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
88 |
|
public function baseUrl() |
97
|
|
|
{ |
98
|
88 |
|
return $this->rootDomain . $this->apiKey . '/v' . $this->apiVersion . '/'; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Build the base 64 encoded string that contains authentication credentials. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
89 |
|
protected function buildAuthenticationValue() |
107
|
|
|
{ |
108
|
89 |
|
return empty($this->username) |
109
|
1 |
|
? '' |
110
|
89 |
|
: base64_encode($this->username . ":" . $this->password); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|