• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

h4kuna / ares / 15350555273

30 May 2025 03:48PM UTC coverage: 87.919%. First build
15350555273

push

github

h4kuna
feat(composer): update phpstan

14 of 36 new or added lines in 13 files covered. (38.89%)

393 of 447 relevant lines covered (87.92%)

0.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.67
/src/Http/TransportProvider.php
1
<?php declare(strict_types=1);
2

3
namespace h4kuna\Ares\Http;
4

5
use h4kuna\Ares\Exception\ServerResponseException;
6
use Nette\Utils\Json;
7
use Nette\Utils\JsonException;
8
use Psr\Http\Client\ClientExceptionInterface;
9
use Psr\Http\Client\ClientInterface;
10
use Psr\Http\Message\RequestFactoryInterface;
11
use Psr\Http\Message\RequestInterface;
12
use Psr\Http\Message\ResponseInterface;
13
use Psr\Http\Message\StreamFactoryInterface;
14
use Psr\Http\Message\StreamInterface;
15
use stdClass;
16

17
final class TransportProvider
18
{
19
        public function __construct(
1✔
20
                private RequestFactoryInterface $requestFactory,
21
                private ClientInterface $client,
22
                private StreamFactoryInterface $streamFactory,
23
        ) {
24
        }
1✔
25

26
        /**
27
         * @throws ServerResponseException
28
         */
29
        public function response(RequestInterface|string $url): ResponseInterface
1✔
30
        {
31
                $request = $url instanceof RequestInterface ? $url : $this->createRequest($url);
1✔
32
                try {
33
                        $response = $this->client->sendRequest($request);
1✔
34
                } catch (ClientExceptionInterface $e) {
×
NEW
35
                        throw ServerResponseException::fromException($e);
×
36
                }
37

38
                return $response;
1✔
39
        }
40

41
        /**
42
         * @throws ServerResponseException
43
         */
44
        public function toJson(ResponseInterface $response): stdClass
1✔
45
        {
46
                try {
47
                        $json = Json::decode($response->getBody()->getContents());
1✔
48
                        assert($json instanceof stdClass);
1✔
49
                } catch (JsonException $e) {
×
NEW
50
                        throw ServerResponseException::fromException($e);
×
51
                }
52

53
                return $json;
1✔
54
        }
55

56
        public function createRequest(string $url, string $method = 'GET'): RequestInterface
1✔
57
        {
58
                return $this->requestFactory->createRequest($method, $url)
1✔
59
                        ->withHeader('X-Powered-By', 'h4kuna/ares');
1✔
60
        }
61

62
        /**
63
         * @param array<string, mixed> $data
64
         */
65
        public function createJsonRequest(string $url, array $data = []): RequestInterface
1✔
66
        {
67
                $request = $this->createPost($url, 'application/json');
1✔
68
                if ($data !== []) {
1✔
69
                        $request = $request->withBody($this->streamFactory->createStream(Json::encode($data)));
1✔
70
                }
71

72
                return $request;
1✔
73
        }
74

75
        public function createXmlRequest(string $url, string|StreamInterface $body): RequestInterface
1✔
76
        {
77
                if (is_string($body)) {
1✔
78
                        $body = $this->streamFactory->createStream($body);
1✔
79
                }
80

81
                return $this->createPost($url, 'application/xml')
1✔
82
                        ->withBody($body);
1✔
83
        }
84

85
        private function createPost(string $url, string $contentType): RequestInterface
1✔
86
        {
87
                return $this->createRequest($url, 'POST')
1✔
88
                        ->withHeader('Content-Type', "$contentType; charset=utf-8");
1✔
89
        }
90

91
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc