1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | namespace NxSys\Library\Clients\Brex\API\Webhooks\Runtime\Client; |
12: | |
13: | use Jane\Component\OpenApiRuntime\Client\Plugin\AuthenticationRegistry; |
14: | use Psr\Http\Client\ClientInterface; |
15: | use Psr\Http\Message\RequestFactoryInterface; |
16: | use Psr\Http\Message\ResponseInterface; |
17: | use Psr\Http\Message\StreamFactoryInterface; |
18: | use Psr\Http\Message\StreamInterface; |
19: | use Symfony\Component\Serializer\SerializerInterface; |
20: | |
21: | abstract class Client |
22: | { |
23: | public const FETCH_RESPONSE = 'response'; |
24: | public const FETCH_OBJECT = 'object'; |
25: | |
26: | |
27: | |
28: | protected $httpClient; |
29: | |
30: | |
31: | |
32: | protected $requestFactory; |
33: | |
34: | |
35: | |
36: | protected $serializer; |
37: | |
38: | |
39: | |
40: | protected $streamFactory; |
41: | |
42: | public function __construct(ClientInterface $httpClient, RequestFactoryInterface $requestFactory, SerializerInterface $serializer, StreamFactoryInterface $streamFactory) |
43: | { |
44: | $this->httpClient = $httpClient; |
45: | $this->requestFactory = $requestFactory; |
46: | $this->serializer = $serializer; |
47: | $this->streamFactory = $streamFactory; |
48: | } |
49: | |
50: | public function executeEndpoint(Endpoint $endpoint, string $fetch = self::FETCH_OBJECT) |
51: | { |
52: | if (self::FETCH_RESPONSE === $fetch) { |
53: | trigger_deprecation('jane-php/open-api-common', '7.3', 'Using %s::%s method with $fetch parameter equals to response is deprecated, use %s::%s instead.', __CLASS__, __METHOD__, __CLASS__, 'executeRawEndpoint'); |
54: | |
55: | return $this->executeRawEndpoint($endpoint); |
56: | } |
57: | |
58: | return $endpoint->parseResponse($this->processEndpoint($endpoint), $this->serializer, $fetch); |
59: | } |
60: | |
61: | public function executeRawEndpoint(Endpoint $endpoint): ResponseInterface |
62: | { |
63: | return $this->processEndpoint($endpoint); |
64: | } |
65: | |
66: | private function processEndpoint(Endpoint $endpoint): ResponseInterface |
67: | { |
68: | [$bodyHeaders, $body] = $endpoint->getBody($this->serializer, $this->streamFactory); |
69: | $queryString = $endpoint->getQueryString(); |
70: | $uriGlue = false === strpos($endpoint->getUri(), '?') ? '?' : '&'; |
71: | $uri = $queryString !== '' ? $endpoint->getUri() . $uriGlue . $queryString : $endpoint->getUri(); |
72: | $request = $this->requestFactory->createRequest($endpoint->getMethod(), $uri); |
73: | if ($body) { |
74: | if ($body instanceof StreamInterface) { |
75: | $request = $request->withBody($body); |
76: | } elseif (is_resource($body)) { |
77: | $request = $request->withBody($this->streamFactory->createStreamFromResource($body)); |
78: | } elseif (strlen($body) <= 4000 && @file_exists($body)) { |
79: | |
80: | $request = $request->withBody($this->streamFactory->createStreamFromFile($body)); |
81: | } else { |
82: | $request = $request->withBody($this->streamFactory->createStream($body)); |
83: | } |
84: | } |
85: | foreach ($endpoint->getHeaders($bodyHeaders) as $name => $value) { |
86: | $request = $request->withHeader($name, $value); |
87: | } |
88: | if (count($endpoint->getAuthenticationScopes()) > 0) { |
89: | $scopes = []; |
90: | foreach ($endpoint->getAuthenticationScopes() as $scope) { |
91: | $scopes[] = $scope; |
92: | } |
93: | $request = $request->withHeader(AuthenticationRegistry::SCOPES_HEADER, $scopes); |
94: | } |
95: | |
96: | return $this->httpClient->sendRequest($request); |
97: | } |
98: | } |
99: | |