TelegramBot /
Api
| 1 | <?php |
||
| 2 | |||
| 3 | namespace TelegramBot\Api\Http; |
||
| 4 | |||
| 5 | use Http\Message\MultipartStream\MultipartStreamBuilder; |
||
|
0 ignored issues
–
show
|
|||
| 6 | use Psr\Http\Client\ClientExceptionInterface; |
||
|
0 ignored issues
–
show
The type
Psr\Http\Client\ClientExceptionInterface was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 7 | use Psr\Http\Client\ClientInterface; |
||
|
0 ignored issues
–
show
The type
Psr\Http\Client\ClientInterface was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 8 | use Psr\Http\Message\RequestFactoryInterface; |
||
|
0 ignored issues
–
show
The type
Psr\Http\Message\RequestFactoryInterface was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 9 | use Psr\Http\Message\StreamFactoryInterface; |
||
|
0 ignored issues
–
show
The type
Psr\Http\Message\StreamFactoryInterface was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 10 | use TelegramBot\Api\HttpException; |
||
| 11 | use TelegramBot\Api\InvalidJsonException; |
||
| 12 | |||
| 13 | class PsrHttpClient extends AbstractHttpClient |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var ClientInterface |
||
| 17 | */ |
||
| 18 | private $http; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var RequestFactoryInterface |
||
| 22 | */ |
||
| 23 | private $requestFactory; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var StreamFactoryInterface |
||
| 27 | */ |
||
| 28 | private $streamFactory; |
||
| 29 | |||
| 30 | public function __construct( |
||
| 31 | ClientInterface $http, |
||
| 32 | RequestFactoryInterface $requestFactory, |
||
| 33 | StreamFactoryInterface $streamFactory |
||
| 34 | ) { |
||
| 35 | $this->http = $http; |
||
| 36 | $this->requestFactory = $requestFactory; |
||
| 37 | $this->streamFactory = $streamFactory; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @inheritDoc |
||
| 42 | */ |
||
| 43 | protected function doRequest($url, array $data = null) |
||
| 44 | { |
||
| 45 | if ($data) { |
||
| 46 | $method = 'POST'; |
||
| 47 | $data = array_filter($data); |
||
| 48 | } else { |
||
| 49 | $method = 'GET'; |
||
| 50 | } |
||
| 51 | |||
| 52 | $request = $this->requestFactory->createRequest($method, $url); |
||
| 53 | if ($method === 'POST') { |
||
| 54 | $multipart = false; |
||
| 55 | |||
| 56 | /** @var array $data */ |
||
| 57 | foreach ($data as &$value) { |
||
| 58 | if ($value instanceof \CURLFile) { |
||
| 59 | $value = fopen($value->getFilename(), 'r'); |
||
| 60 | $multipart = true; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | unset($value); |
||
| 64 | |||
| 65 | if ($multipart) { |
||
| 66 | $builder = new MultipartStreamBuilder($this->streamFactory); |
||
| 67 | foreach ($data as $name => $value) { |
||
| 68 | $builder->addResource($name, $value); |
||
| 69 | } |
||
| 70 | $stream = $builder->build(); |
||
| 71 | $request = $request->withHeader('Content-Type', "multipart/form-data; boundary={$builder->getBoundary()}"); |
||
| 72 | } else { |
||
| 73 | $stream = $this->streamFactory->createStream(http_build_query($data)); |
||
| 74 | $request = $request->withHeader('Content-Type', 'application/x-www-form-urlencoded'); |
||
| 75 | } |
||
| 76 | |||
| 77 | $request = $request->withBody($stream); |
||
| 78 | } |
||
| 79 | |||
| 80 | try { |
||
| 81 | $response = $this->http->sendRequest($request); |
||
| 82 | } catch (ClientExceptionInterface $exception) { |
||
| 83 | throw new HttpException($exception->getMessage(), $exception->getCode(), $exception); |
||
| 84 | } |
||
| 85 | |||
| 86 | $content = $response->getBody()->getContents(); |
||
| 87 | |||
| 88 | $json = self::jsonValidate($content); |
||
| 89 | |||
| 90 | if (!\in_array($response->getStatusCode(), [200, 304])) { |
||
| 91 | $errorDescription = array_key_exists('description', $json) ? $json['description'] : $response->getReasonPhrase(); |
||
| 92 | $errorParameters = array_key_exists('parameters', $json) ? $json['parameters'] : []; |
||
| 93 | |||
| 94 | throw new HttpException($errorDescription, $response->getStatusCode(), null, $errorParameters); |
||
| 95 | } |
||
| 96 | |||
| 97 | return $json; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @inheritDoc |
||
| 102 | */ |
||
| 103 | protected function doDownload($url) |
||
| 104 | { |
||
| 105 | $request = $this->requestFactory->createRequest('GET', $url); |
||
| 106 | |||
| 107 | try { |
||
| 108 | return $this->http->sendRequest($request)->getBody()->getContents(); |
||
| 109 | } catch (ClientExceptionInterface $exception) { |
||
| 110 | throw new HttpException($exception->getMessage(), $exception->getCode(), $exception); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param string $jsonString |
||
| 116 | * @return array |
||
| 117 | * @throws InvalidJsonException |
||
| 118 | */ |
||
| 119 | private static function jsonValidate($jsonString) |
||
| 120 | { |
||
| 121 | /** @var array $json */ |
||
| 122 | $json = json_decode($jsonString, true); |
||
| 123 | |||
| 124 | if (json_last_error() !== JSON_ERROR_NONE) { |
||
| 125 | throw new InvalidJsonException(json_last_error_msg(), json_last_error()); |
||
| 126 | } |
||
| 127 | |||
| 128 | return $json; |
||
| 129 | } |
||
| 130 | } |
||
| 131 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths