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

h4kuna / fio / 9611444634

21 Jun 2024 09:27AM UTC coverage: 90.391% (-0.3%) from 90.714%
9611444634

push

github

h4kuna
feat(FioRequestFactory): more guzzle independent

0 of 3 new or added lines in 1 file covered. (0.0%)

508 of 562 relevant lines covered (90.39%)

0.9 hits per line

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

8.33
/src/Utils/FioRequestFactory.php
1
<?php declare(strict_types=1);
2

3
namespace h4kuna\Fio\Utils;
4

5
use GuzzleHttp\Psr7\MultipartStream;
6
use GuzzleHttp\Psr7\Utils;
7
use Psr\Http\Message\RequestFactoryInterface;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\StreamFactoryInterface;
10
use Psr\Http\Message\StreamInterface;
11

12
class FioRequestFactory
13
{
14
        public function __construct(
1✔
15
                private RequestFactoryInterface $requestFactory,
16
                private StreamFactoryInterface $streamFactory
17
        )
18
        {
19
        }
1✔
20

21

22
        public function get(string $uri): RequestInterface
23
        {
24
                return $this->requestFactory->createRequest('GET', $uri);
×
25
        }
26

27

28
        /**
29
         * @param array{token: string, type: string, lng?: string} $params
30
         * @param string $content string is filepath or content
31
         */
32
        public function post(string $uri, array $params, string $content): RequestInterface
33
        {
34
                $request = $this->requestFactory->createRequest('POST', $uri);
×
35

36
                if (is_file($content)) {
×
37
                        $filename = basename($content);
×
38
                        $stream = $this->streamFactory->createStreamFromFile($content);
×
39
                } else {
40
                        $filename = 'h4kuna.memory.xml';
×
41
                        $stream = $this->streamFactory->createStreamFromResource(
×
42
                                $this->createTempFile($content)
×
43
                        );
44
                }
45

NEW
46
                $multipart = $this->createMultiPart($filename, $stream, $params);
×
47

NEW
48
                return $request->withHeader('Content-Type', 'multipart/form-data; boundary=' . $multipart->getBoundary())
×
NEW
49
                        ->withBody($multipart);
×
50
        }
51

52

53
        /**
54
         * @param array{token: string, type: string, lng?: string} $params
55
         */
56
        private function createMultiPart(string $filename, StreamInterface $file, array $params): MultipartStream
57
        {
58
                $newPost = [
×
59
                        [
60
                                'name' => 'file',
×
61
                                'filename' => $filename, // require!
×
62
                                'contents' => $file,
×
63
                        ],
64
                ];
65
                foreach ($params as $name => $value) {
×
66
                        $newPost[] = ['name' => $name, 'contents' => $value];
×
67
                }
68

69
                return new MultipartStream($newPost);
×
70
        }
71

72

73
        /**
74
         * @return resource
75
         */
76
        protected function createTempFile(string $content)
77
        {
78
                $resource = Utils::tryFopen('php://memory', 'r+');
×
79
                fwrite($resource, $content);
×
80
                fseek($resource, 0);
×
81

82
                return $resource;
×
83
        }
84

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