addHeadersToApmResponse()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace TechDeCo\ElasticApmAgent\Convenience\Middleware\OpenTransactionRequestEnricher;
5
6
use Psr\Http\Message\ResponseInterface;
7
use TechDeCo\ElasticApmAgent\Convenience\Middleware\OpenTransactionResponseEnricher;
8
use TechDeCo\ElasticApmAgent\Convenience\OpenTransaction;
9
use TechDeCo\ElasticApmAgent\Message\Response;
10
use function array_map;
11
use function implode;
12
use function in_array;
13
use function strtolower;
14
15
final class ResponseHeaderBlacklistEnricher implements OpenTransactionResponseEnricher
16
{
17
    /**
18
     * @var string[]
19
     */
20
    private $headerBlacklist;
21
22 4
    public function __construct(string ...$headerBlacklist)
23
    {
24 4
        $this->headerBlacklist = array_map('strtolower', $headerBlacklist);
25 4
    }
26
27 4
    public function enrichFromResponse(OpenTransaction $transaction, ResponseInterface $response): void
28
    {
29 4
        $apmResponse = $this->addHeadersToApmResponse(
30 4
            $response,
31 4
            $transaction->getContext()->getResponse() ?? Response::fromHttpResponse($response)
32
        );
33 4
        $transaction->setContext(
34 4
            $transaction->getContext()->withResponse($apmResponse)
35
        );
36 4
    }
37
38 4
    private function addHeadersToApmResponse(ResponseInterface $httpResponse, Response $apmResponse): Response
39
    {
40 4
        foreach ($httpResponse->getHeaders() as $name => $valueList) {
41 3
            if (in_array(strtolower($name), $this->headerBlacklist, true)) {
42 2
                continue;
43
            }
44
45 3
            $apmResponse = $apmResponse->withHeader($name, implode(',', $valueList));
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $apmResponse. This often makes code more readable.
Loading history...
46
        }
47
48 4
        return $apmResponse;
49
    }
50
}
51