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

lmc-eu / matej-client-php / 7527331519

15 Jan 2024 10:11AM UTC coverage: 100.0%. Remained the same
7527331519

Pull #135

github

web-flow
Merge 117257645 into 9666a6254
Pull Request #135: Update to new coding standard

20 of 21 new or added lines in 5 files covered. (95.24%)

400 existing lines in 23 files now uncovered.

758 of 758 relevant lines covered (100.0%)

18.54 hits per line

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

100.0
/src/Http/RequestManager.php
1
<?php declare(strict_types=1);
2

3
namespace Lmc\Matej\Http;
4

5
use Http\Client\Common\Plugin\AuthenticationPlugin;
6
use Http\Client\Common\Plugin\HeaderSetPlugin;
7
use Http\Client\Common\PluginClient;
8
use Http\Client\HttpClient;
9
use Http\Discovery\HttpClientDiscovery;
10
use Http\Discovery\MessageFactoryDiscovery;
11
use Http\Message\MessageFactory;
12
use Lmc\Matej\Http\Plugin\ExceptionPlugin;
13
use Lmc\Matej\Matej;
14
use Lmc\Matej\Model\Request;
15
use Lmc\Matej\Model\Response;
16
use Psr\Http\Message\RequestInterface;
17

18
/**
19
 * Encapsulates HTTP layer, ie. request/response handling.
20
 * This class should not be typically used directly - its supposed to be called internally from `Matej` class.
21
 */
22
class RequestManager
23
{
24
    public const CLIENT_VERSION_HEADER = 'Matej-Client-Version';
25
    public const REQUEST_ID_HEADER = 'Matej-Request-Id';
26
    public const RESPONSE_ID_HEADER = 'Matej-Response-Id';
27

28
    /** @var string */
29
    private $baseUrl = 'https://%s.matej.lmc.cz';
30
    /** @var string */
31
    protected $accountId;
32
    /** @var string */
33
    protected $apiKey;
34
    /** @var HttpClient */
35
    protected $httpClient;
36
    /** @var MessageFactory */
37
    protected $messageFactory;
38
    /** @var ResponseDecoderInterface */
39
    protected $responseDecoder;
40

41
    public function __construct(string $accountId, string $apiKey)
42
    {
UNCOV
43
        $this->accountId = $accountId;
4✔
UNCOV
44
        $this->apiKey = $apiKey;
4✔
45
    }
1✔
46

47
    public function sendRequest(Request $request): Response
48
    {
UNCOV
49
        $httpRequest = $this->createHttpRequestFromMatejRequest($request);
4✔
50

UNCOV
51
        $client = $this->createConfiguredHttpClient();
4✔
52

UNCOV
53
        $httpResponse = $client->sendRequest($httpRequest);
4✔
54

UNCOV
55
        return $this->getResponseDecoder()->decode($httpResponse, $request->getResponseClass());
4✔
56
    }
57

58
    /** @codeCoverageIgnore */
59
    public function setHttpClient(HttpClient $httpClient): void
60
    {
61
        $this->httpClient = $httpClient;
62
    }
63

64
    /** @codeCoverageIgnore */
65
    public function setMessageFactory(MessageFactory $messageFactory): void
66
    {
67
        $this->messageFactory = $messageFactory;
68
    }
69

70
    /** @codeCoverageIgnore */
71
    public function setResponseDecoder(ResponseDecoderInterface $responseDecoder): void
72
    {
73
        $this->responseDecoder = $responseDecoder;
74
    }
75

76
    /** @codeCoverageIgnore */
77
    public function setBaseUrl(string $baseUrl): void
78
    {
79
        $this->baseUrl = $baseUrl;
80
    }
81

82
    protected function getHttpClient(): HttpClient
83
    {
UNCOV
84
        if ($this->httpClient === null) {
4✔
85
            // @codeCoverageIgnoreStart
86
            $this->httpClient = HttpClientDiscovery::find();
87
            // @codeCoverageIgnoreEnd
88
        }
89

UNCOV
90
        return $this->httpClient;
4✔
91
    }
92

93
    protected function getMessageFactory(): MessageFactory
94
    {
UNCOV
95
        if ($this->messageFactory === null) {
4✔
UNCOV
96
            $this->messageFactory = MessageFactoryDiscovery::find();
4✔
97
        }
98

UNCOV
99
        return $this->messageFactory;
4✔
100
    }
101

102
    protected function getResponseDecoder(): ResponseDecoderInterface
103
    {
UNCOV
104
        if ($this->responseDecoder === null) {
4✔
UNCOV
105
            $this->responseDecoder = new ResponseDecoder();
4✔
106
        }
107

UNCOV
108
        return $this->responseDecoder;
4✔
109
    }
110

111
    protected function createConfiguredHttpClient(): HttpClient
112
    {
UNCOV
113
        return new PluginClient(
4✔
UNCOV
114
            $this->getHttpClient(),
4✔
UNCOV
115
            [
3✔
UNCOV
116
                new HeaderSetPlugin($this->getDefaultHeaders()),
4✔
UNCOV
117
                new AuthenticationPlugin(new HmacAuthentication($this->apiKey)),
4✔
UNCOV
118
                new ExceptionPlugin(),
4✔
UNCOV
119
            ]
3✔
UNCOV
120
        );
3✔
121
    }
122

123
    protected function createHttpRequestFromMatejRequest(Request $request): RequestInterface
124
    {
UNCOV
125
        $requestBody = json_encode($request->getData()); // TODO: use \Safe\json_encode
4✔
UNCOV
126
        $uri = $this->buildBaseUrl() . $request->getPath();
4✔
127

UNCOV
128
        return $this->getMessageFactory()
4✔
UNCOV
129
            ->createRequest(
4✔
UNCOV
130
                $request->getMethod(),
4✔
UNCOV
131
                $uri,
3✔
UNCOV
132
                [
3✔
UNCOV
133
                    'Content-Type' => 'application/json',
4✔
UNCOV
134
                    static::REQUEST_ID_HEADER => $request->getRequestId(),
4✔
UNCOV
135
                ],
3✔
UNCOV
136
                $requestBody
3✔
UNCOV
137
            );
3✔
138
    }
139

140
    protected function buildBaseUrl(): string
141
    {
UNCOV
142
        return sprintf($this->baseUrl, $this->accountId);
4✔
143
    }
144

145
    private function getDefaultHeaders(): array
146
    {
UNCOV
147
        return [
3✔
UNCOV
148
            static::CLIENT_VERSION_HEADER => Matej::CLIENT_ID . '/' . Matej::VERSION,
4✔
UNCOV
149
        ];
3✔
150
    }
151
}
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