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

h4kuna / fio / 14419053840

12 Apr 2025 11:11AM UTC coverage: 90.451% (-0.3%) from 90.767%
14419053840

push

github

web-flow
converts server error response to exception (#62)

2 of 2 new or added lines in 1 file covered. (100.0%)

2 existing lines in 1 file now uncovered.

521 of 576 relevant lines covered (90.45%)

0.9 hits per line

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

95.12
/src/Utils/Queue.php
1
<?php declare(strict_types=1);
2

3
namespace h4kuna\Fio\Utils;
4

5
use h4kuna\Fio\Contracts\RequestBlockingServiceContract;
6
use h4kuna\Fio\Exceptions;
7
use h4kuna\Fio\Pay\Response;
8
use h4kuna\Fio\Pay\XMLResponse;
9
use Nette\Utils\Strings;
10
use Psr\Http\Client\ClientExceptionInterface;
11
use Psr\Http\Client\ClientInterface;
12
use Psr\Http\Message\RequestInterface;
13
use Psr\Http\Message\ResponseInterface;
14

15
class Queue
16
{
17
        private const HEADER_CONFLICT = 409;
18
        private int $limitLoop = 3;
19

20

21
        public function __construct(
1✔
22
                private ClientInterface $client,
23
                private FioRequestFactory $requestFactory,
24
                private RequestBlockingServiceContract $requestBlockingService
25
        ) {
26
        }
1✔
27

28

29
        /**
30
         * @param positive-int $limitLoop
31
         */
32
        public function setLimitLoop(int $limitLoop): void
1✔
33
        {
34
                $this->limitLoop = $limitLoop;
1✔
35
        }
1✔
36

37

38
        /**
39
         * @throws Exceptions\ServiceUnavailable
40
         */
41
        public function download(string $token, string $url): ResponseInterface
1✔
42
        {
43
                $response = $this->request($token, $this->requestFactory->get($url));
1✔
44
                $this->detectDownloadResponse($response);
1✔
45

46
                return $response;
1✔
47
        }
48

49

50
        /**
51
         * @throws Exceptions\ServiceUnavailable
52
         */
53
        private function detectDownloadResponse(ResponseInterface $response): void
1✔
54
        {
55
                $contentTypeHeaders = $response->getHeader('Content-Type');
1✔
56
                $contentType = array_shift($contentTypeHeaders);
1✔
57
                if ($contentType !== null && str_contains($contentType, 'text/xml')) {
1✔
UNCOV
58
                        $xmlResponse = $this->createXmlResponse($response);
×
59

UNCOV
60
                        throw new Exceptions\ServiceUnavailable($xmlResponse->status(), $xmlResponse->code());
×
61
                }
62
        }
1✔
63

64

65
        /**
66
         * @param array{token: string, type: string, lng?: string} $params
67
         * @param string $content string is filepath or content
68
         */
69
        public function import(array $params, string $content): Response
1✔
70
        {
71
                $response = $this->request(
1✔
72
                        $params['token'],
1✔
73
                        $this->requestFactory->post(Fio::REST_URL . 'import/', $params, $content),
1✔
74
                );
75

76
                return $this->createXmlResponse($response);
1✔
77
        }
78

79

80
        private function request(string $token, RequestInterface $request): ResponseInterface
1✔
81
        {
82
                $request = $request->withHeader('X-Powered-By', 'h4kuna/fio');
1✔
83
                $response = null;
1✔
84
                try {
85
                        for ($i = 0; $i < $this->limitLoop && $response === null; ++$i) {
1✔
86
                                $response = $this->requestBlockingService->synchronize($token, function () use ($request) {
1✔
87
                                        $response = $this->client->sendRequest($request);
1✔
88

89
                                        if ($response->getStatusCode() === self::HEADER_CONFLICT) {
1✔
90
                                                return null;
1✔
91
                                        }
92

93
                                        return $response;
1✔
94
                                });
1✔
95
                        }
96
                } catch (ClientExceptionInterface $e) {
1✔
97
                        $message = str_replace($token, Strings::truncate($token, 10), $e->getMessage());
1✔
98
                        throw new Exceptions\ServiceUnavailable($message, $e->getCode()); // in url is token, don't need keep previous exception
1✔
99
                }
100

101
                if ($response === null) {
1✔
102
                        throw new Exceptions\QueueLimit(sprintf('You have limit up requests to server "%s". Too many requests in short time interval.', $this->limitLoop));
1✔
103
                }
104

105
                if ($response->getStatusCode() >= 500) {
1✔
106
                        throw new Exceptions\ServiceUnavailable(sprintf('%d %s', $response->getStatusCode(), $response->getReasonPhrase()));
1✔
107
                }
108

109
                return $response;
1✔
110
        }
111

112

113
        /**
114
         * @throws Exceptions\ServiceUnavailable
115
         */
116
        private function createXmlResponse(ResponseInterface $response): XMLResponse
1✔
117
        {
118
                return new XMLResponse(Fio::getContents($response));
1✔
119
        }
120

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