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

lmc-eu / matej-client-php / 7469814138

20 Oct 2022 11:42AM UTC coverage: 100.0%. Remained the same
7469814138

push

github

OndraM
Block fixup commits from accidental merge

746 of 746 relevant lines covered (100.0%)

27.9 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
    {
43
        $this->accountId = $accountId;
6✔
44
        $this->apiKey = $apiKey;
6✔
45
    }
3✔
46

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

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

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

55
        return $this->getResponseDecoder()->decode($httpResponse, $request->getResponseClass());
6✔
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
    {
84
        if ($this->httpClient === null) {
6✔
85
            // @codeCoverageIgnoreStart
86
            $this->httpClient = HttpClientDiscovery::find();
87
            // @codeCoverageIgnoreEnd
88
        }
89

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

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

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

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

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

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

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

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

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

145
    private function getDefaultHeaders(): array
146
    {
147
        return [
3✔
148
            static::CLIENT_VERSION_HEADER => Matej::CLIENT_ID . '/' . Matej::VERSION,
6✔
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

© 2026 Coveralls, Inc