Completed
Push — master ( 903df6...64a9b8 )
by Alexander
18s queued 15s
created

PsrHttpClient::jsonValidate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace TelegramBot\Api\Http;
4
5
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...
6
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...
7
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...
8
use TelegramBot\Api\HttpException;
9
use TelegramBot\Api\InvalidJsonException;
10
11
class PsrHttpClient extends AbstractHttpClient
12
{
13
    /**
14
     * @var ClientInterface
15
     */
16
    private $http;
17
18
    /**
19
     * @var RequestFactoryInterface
20
     */
21
    private $requestFactory;
22
23
    public function __construct(ClientInterface $http, RequestFactoryInterface $requestFactory)
24
    {
25
        $this->http = $http;
26
        $this->requestFactory = $requestFactory;
27
    }
28
29
    /**
30
     * @inheritDoc
31
     */
32
    protected function doRequest($url, array $data = null)
33
    {
34
        if ($data) {
35
            $method = 'POST';
36
        } else {
37
            $method = 'GET';
38
        }
39
40
        $request = $this->requestFactory->createRequest($method, $url);
41
        try {
42
            $response = $this->http->sendRequest($request);
43
        } catch (ClientExceptionInterface $exception) {
44
            throw new HttpException($exception->getMessage(), $exception->getCode(), $exception);
45
        }
46
47
        $content = $response->getBody()->getContents();
48
49
        return self::jsonValidate($content);
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55
    protected function doDownload($url)
56
    {
57
        $request = $this->requestFactory->createRequest('GET', $url);
58
59
        try {
60
            return $this->http->sendRequest($request)->getBody()->getContents();
61
        } catch (ClientExceptionInterface $exception) {
62
            throw new HttpException($exception->getMessage(), $exception->getCode(), $exception);
63
        }
64
    }
65
66
    /**
67
     * @param string $jsonString
68
     * @return array
69
     * @throws InvalidJsonException
70
     */
71
    private static function jsonValidate($jsonString)
72
    {
73
        /** @var array $json */
74
        $json = json_decode($jsonString, true);
75
76
        if (json_last_error() !== JSON_ERROR_NONE) {
77
            throw new InvalidJsonException(json_last_error_msg(), json_last_error());
78
        }
79
80
        return $json;
81
    }
82
}
83