Completed
Push — master ( 4d722f...452aee )
by Alexander
17s queued 14s
created

PsrHttpClient::doRequest()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 46
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 46
rs 8.2114
cc 8
nc 12
nop 2
1
<?php
2
3
namespace TelegramBot\Api\Http;
4
5
use Http\Message\MultipartStream\MultipartStreamBuilder;
0 ignored issues
show
Bug introduced by
The type Http\Message\MultipartSt...\MultipartStreamBuilder 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Psr\Http\Client\ClientExceptionInterface;
0 ignored issues
show
Bug introduced by
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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

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
Bug introduced by
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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

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
Bug introduced by
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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

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
Bug introduced by
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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

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
        return self::jsonValidate($content);
89
    }
90
91
    /**
92
     * @inheritDoc
93
     */
94
    protected function doDownload($url)
95
    {
96
        $request = $this->requestFactory->createRequest('GET', $url);
97
98
        try {
99
            return $this->http->sendRequest($request)->getBody()->getContents();
100
        } catch (ClientExceptionInterface $exception) {
101
            throw new HttpException($exception->getMessage(), $exception->getCode(), $exception);
102
        }
103
    }
104
105
    /**
106
     * @param string $jsonString
107
     * @return array
108
     * @throws InvalidJsonException
109
     */
110
    private static function jsonValidate($jsonString)
111
    {
112
        /** @var array $json */
113
        $json = json_decode($jsonString, true);
114
115
        if (json_last_error() !== JSON_ERROR_NONE) {
116
            throw new InvalidJsonException(json_last_error_msg(), json_last_error());
117
        }
118
119
        return $json;
120
    }
121
}
122