TracingGuzzleMiddleware::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace SamuelBednarcik\ElasticAPMAgent\Tracing;
4
5
use Psr\Http\Message\RequestInterface;
6
use SamuelBednarcik\ElasticAPMAgent\Agent;
7
use SamuelBednarcik\ElasticAPMAgent\TraceParent;
8
9
class TracingGuzzleMiddleware
10
{
11
    /**
12
     * @var Agent
13
     */
14
    private $agent;
15
16
    public function __construct(Agent $agent)
17
    {
18
        $this->agent = $agent;
19
    }
20
21
    /**
22
     * @param callable $handler
23
     * @return \Closure
24
     */
25
    public function __invoke(callable $handler)
26
    {
27
        return function (RequestInterface $request, array $options) use ($handler) {
28
            $transaction = $this->agent->getTransaction();
29
30
            if ($transaction !== null) {
31
                if ($transaction->getTraceId() !== null && $transaction->getId() !== null) {
32
                    $header = new TraceParent($transaction->getTraceId(), $transaction->getId(), '01');
33
                    $request = $request->withHeader(TraceParent::HEADER_NAME, $header->__toString());
34
                }
35
            }
36
37
            return $handler($request, $options);
38
        };
39
    }
40
}
41