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

h4kuna / ares / 8853066536

22 Apr 2024 09:43AM UTC coverage: 89.487% (+0.4%) from 89.112%
8853066536

push

github

h4kuna
feat(VIES): add checker VAT in EU

41 of 45 new or added lines in 8 files covered. (91.11%)

1 existing line in 1 file now uncovered.

349 of 390 relevant lines covered (89.49%)

0.89 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\Exceptions\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
        {
25
        }
1✔
26

27

28
        public function response(RequestInterface|string $url): ResponseInterface
1✔
29
        {
30
                $request = $url instanceof RequestInterface ? $url : $this->createRequest($url);
1✔
31
                try {
32
                        $response = $this->client->sendRequest($request);
1✔
33
                } catch (ClientExceptionInterface $e) {
×
34
                        throw new ServerResponseException($e->getMessage(), $e->getCode(), $e);
×
35
                }
36

37
                return $response;
1✔
38
        }
39

40

41
        public function toJson(ResponseInterface $response): stdClass
1✔
42
        {
43
                try {
44
                        $json = Json::decode($response->getBody()->getContents());
1✔
45
                        assert($json instanceof stdClass);
1✔
NEW
46
                } catch (JsonException $e) {
×
NEW
47
                        throw new ServerResponseException($e->getMessage(), $e->getCode(), $e);
×
48
                }
49

50
                return $json;
1✔
51
        }
52

53

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

60

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

71
                return $request;
1✔
72
        }
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

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

92
}
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

© 2025 Coveralls, Inc