AbstractGateway.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. // Copyright 2019 Hackware SpA <human@hackware.cl>
  3. // This file is part of "Hackware Web Services Payment" and licensed under
  4. // the terms of the GNU Affero General Public License version 3, or (at your
  5. // option) a later version. You should have received a copy of this license
  6. // along with the software. If not, see <https://www.gnu.org/licenses/>.
  7. namespace Hawese\Payment\Gateways;
  8. use Buzz\Client\FileGetContents as HttpClient;
  9. use Nyholm\Psr7\Factory\Psr17Factory as Psr17ResponseFactory;
  10. use Psr\Http\Message\ResponseInterface;
  11. abstract class AbstractGateway
  12. {
  13. protected $psr17Factory;
  14. protected $httpClient;
  15. public function __construct()
  16. {
  17. $this->psr17Factory = new Psr17ResponseFactory();
  18. $this->httpClient = new HttpClient($this->psr17Factory);
  19. }
  20. public function request(
  21. string $method,
  22. string $uri,
  23. array $bodyParams = [],
  24. array $headers = []
  25. ): ResponseInterface {
  26. $builderName = static::requestBuilderName();
  27. $requestBuilder = new $builderName(
  28. $method,
  29. $uri,
  30. $bodyParams,
  31. $headers
  32. );
  33. return $this->httpClient->sendRequest($requestBuilder->build());
  34. }
  35. protected static function requestBuilderName(): string
  36. {
  37. return substr(static::class, 0, -7) . 'RequestBuilder';
  38. }
  39. }