bytesfield /
simple-kyc
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Bytesfield\SimpleKyc; |
||
| 4 | |||
| 5 | use GuzzleHttp\Client; |
||
| 6 | use Bytesfield\SimpleKyc\Exceptions\IsNullException; |
||
| 7 | |||
| 8 | class HttpProcessor |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * The Request KYC Handler |
||
| 12 | * |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | protected $handler; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Instance of HTTP Client |
||
| 19 | * |
||
| 20 | * @var Client |
||
| 21 | */ |
||
| 22 | protected $client; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Handler Api Key |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $apiKey; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Authentication API base Url |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $baseUrl; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Response from requests made to Handler Service |
||
| 39 | * |
||
| 40 | * @var mixed |
||
| 41 | */ |
||
| 42 | protected $response; |
||
| 43 | |||
| 44 | public function __construct(string $baseUrl, string $apiKey = null, string $handler) |
||
| 45 | { |
||
| 46 | $this->baseUrl = $baseUrl; |
||
| 47 | $this->apiKey = $apiKey; |
||
| 48 | $this->handler = $handler; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Set options for making the Client request |
||
| 53 | */ |
||
| 54 | private function setRequestOption() |
||
| 55 | { |
||
| 56 | $authHeader = []; |
||
| 57 | |||
| 58 | if ($this->handler == 'Credequity') { |
||
| 59 | $authHeader = ['Access-Key' => $this->apiKey]; |
||
| 60 | } |
||
| 61 | |||
| 62 | if ($this->handler == 'Appruve') { |
||
| 63 | $authHeader = ['Authorization' => "Bearer {$this->apiKey}"]; |
||
| 64 | } |
||
| 65 | |||
| 66 | $header = [ |
||
| 67 | 'Content-Type' => 'application/json', |
||
| 68 | 'Accept' => 'application/json' |
||
| 69 | ]; |
||
| 70 | |||
| 71 | $this->client = new Client( |
||
| 72 | [ |
||
| 73 | 'base_uri' => $this->baseUrl, |
||
| 74 | 'headers' => array_merge($authHeader, $header) |
||
| 75 | ] |
||
| 76 | ); |
||
| 77 | |||
| 78 | return $this; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param string $relativeUrl |
||
| 83 | * @param string $method |
||
| 84 | * @param array $body |
||
| 85 | * |
||
| 86 | * @throws IsNullException |
||
| 87 | */ |
||
| 88 | public function process($method, $relativeUrl, $body = []) |
||
| 89 | { |
||
| 90 | $this->setRequestOption(); |
||
| 91 | |||
| 92 | if (is_null($method)) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 93 | throw new IsNullException("Empty method not allowed"); |
||
| 94 | } |
||
| 95 | |||
| 96 | $this->response = $this->client->request( |
||
| 97 | strtoupper($method), |
||
| 98 | $this->baseUrl . $relativeUrl, |
||
| 99 | ["body" => json_encode($body)] |
||
| 100 | ); |
||
| 101 | |||
| 102 | return $this; |
||
| 103 | } |
||
| 104 | |||
| 105 | |||
| 106 | /** |
||
| 107 | * Get the whole response from the request |
||
| 108 | * |
||
| 109 | * @return array |
||
| 110 | */ |
||
| 111 | public function getResponse() |
||
| 112 | { |
||
| 113 | return json_decode($this->response->getBody()->getContents(), true); |
||
| 114 | } |
||
| 115 | } |
||
| 116 |