• 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/HmacAuthentication.php
1
<?php declare(strict_types=1);
2

3
namespace Lmc\Matej\Http;
4

5
use Http\Message\Authentication;
6
use Psr\Http\Message\RequestInterface;
7

8
/**
9
 * Use api key to sign request with hmac_timestamp and hmac_sign query parameters.
10
 *
11
 * @internal
12
 */
13
class HmacAuthentication implements Authentication
14
{
15
    /** @var string */
16
    private $apiKey;
17

18
    public function __construct(string $apiKey)
19
    {
UNCOV
20
        $this->apiKey = $apiKey;
16✔
21
    }
4✔
22

23
    public function authenticate(RequestInterface $request): RequestInterface
24
    {
UNCOV
25
        $params = $this->addAuthenticationToParamsFromRequest($request);
16✔
26

UNCOV
27
        return $this->createRequestWithParams($request, $params);
16✔
28
    }
29

30
    private function addAuthenticationToParamsFromRequest(RequestInterface $request): array
31
    {
UNCOV
32
        $params = $this->parseQueryParamsFromRequest($request);
16✔
33

UNCOV
34
        $params['hmac_timestamp'] = time();
16✔
35

UNCOV
36
        $unsignedRequest = $this->createRequestWithParams($request, $params);
16✔
37

UNCOV
38
        $params['hmac_sign'] = hash_hmac('sha1', $unsignedRequest->getRequestTarget(), $this->apiKey);
16✔
39

UNCOV
40
        return $params;
16✔
41
    }
42

43
    private function parseQueryParamsFromRequest(RequestInterface $request): array
44
    {
UNCOV
45
        $uri = $request->getUri();
16✔
UNCOV
46
        $query = $uri->getQuery();
16✔
47

UNCOV
48
        $params = [];
16✔
UNCOV
49
        parse_str($query, $params);
16✔
50

UNCOV
51
        return $params;
16✔
52
    }
53

54
    private function createRequestWithParams(RequestInterface $request, array $params): RequestInterface
55
    {
UNCOV
56
        $uri = $request->getUri();
16✔
57

UNCOV
58
        $query = http_build_query($params, '', '&');
16✔
UNCOV
59
        $uri = $uri->withQuery($query);
16✔
60

UNCOV
61
        return $request->withUri($uri);
16✔
62
    }
63
}
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