This repository has been archived by the owner on Mar 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathClient.php
174 lines (152 loc) Β· 4.39 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
namespace Xeviant\Paystack;
use Http\Client\Common\HttpMethodsClientInterface;
use Http\Client\Common\Plugin\AddHostPlugin;
use Http\Client\Common\Plugin\HistoryPlugin;
use Http\Client\Common\Plugin\RedirectPlugin;
use Http\Client\HttpClient;
use Http\Discovery\UriFactoryDiscovery;
use Xeviant\Paystack\App\PaystackApplication;
use Xeviant\Paystack\Contract\ApiInterface;
use Xeviant\Paystack\Contract\ApplicationInterface;
use Xeviant\Paystack\Contract\Config;
use Xeviant\Paystack\Contract\EventInterface;
use Xeviant\Paystack\Exception\BadMethodCallException;
use Xeviant\Paystack\Exception\InvalidArgumentException;
use Xeviant\Paystack\HttpClient\Builder;
use Xeviant\Paystack\HttpClient\Plugin\HeaderDefaultsPlugin;
use Xeviant\Paystack\HttpClient\Plugin\History;
use Xeviant\Paystack\Model\Customer;
use Xeviant\Paystack\Model\Model;
class Client
{
/**
* @var History
*/
private $responseHistory;
/**
* @var Builder
*/
private $httpClientBuilder;
/**
* @var string
*/
private $apiVersion;
/**
* @var Config
*/
private $config;
/**
* @var EventInterface
*/
private $event;
/**
* @var ApplicationInterface
*/
private $app;
/**
* Client constructor.
*
* @param ApplicationInterface $app
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function __construct(ApplicationInterface $app = null)
{
$this->app = $app ?? new PaystackApplication();
$this->config = $this->app->make(Config::class);
$this->responseHistory = new History();
$this->httpClientBuilder = $builder = $this->app->make(Builder::class);
$builder->addPlugin(new HistoryPlugin($this->responseHistory));
$builder->addPlugin(new RedirectPlugin());
$builder->addPlugin(new AddHostPlugin(UriFactoryDiscovery::find()->createUri('https://api.paystack.co')));
$builder->addPlugin(new HeaderDefaultsPlugin([], $this->config));
$this->apiVersion = $this->config ? $this->config->getApiVersion() : 'v1';
$builder->addHeaderValue('Accept', sprintf('application/json'));
$this->httpClientBuilder = $builder;
$this->event = $this->app->make(EventInterface::class);
}
/**
* Creates The Paystack client with an HTTP Client.
*
* @param HttpClient $httpClient
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*
* @return Client
*/
public static function createWithHttpClient(HttpClient $httpClient): self
{
$app = new PaystackApplication();
$app->bind(Builder::class, function ($app) use ($httpClient) {
return new Builder($httpClient);
});
return new self($app);
}
/**
* Retrieves an HTTP Client Object.
*
* @return \Http\Client\Common\HttpMethodsClientInterface
*/
public function getHttpClient(): HttpMethodsClientInterface
{
return $this->getHttpClientBuilder()->getHttpClient();
}
/**
* Retrieves an HTTP Client builder object.
*
* @return Builder
*/
protected function getHttpClientBuilder(): Builder
{
return $this->httpClientBuilder;
}
public function getEvent(): EventInterface
{
return $this->event;
}
/**
* Gets the API Instance.
*
* @param $name
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*
* @return ApiInterface
*/
public function api($name): ApiInterface
{
try {
return $this->app->makeApi($name);
} catch (InvalidArgumentException $e) {
throw $e;
}
}
/**
* @param $name
* @param $arguments
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*
* @return ApiInterface
*/
public function __call($name, $arguments): ApiInterface
{
try {
return $this->api($name);
} catch (InvalidArgumentException $e) {
throw new BadMethodCallException(sprintf('Undefined method called: "%s', $name));
}
}
public function model($attribute)
{
switch ($attribute) {
case 'customer':
return new Customer();
}
}
public function __get($attribute)
{
return $this->model($attribute);
}
}