Test Setup Failed
Branch master (6d65c2)
by Florian
04:17
created

CorrelationIDMiddleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright (c) Florian Krämer (https://florian-kraemer.net)
5
 * Licensed under The GPL3 License
6
 * For full copyright and license information, please see the LICENSE.txt
7
 * Redistributions of files must retain the above copyright notice.
8
 *
9
 * @copyright Copyright (c) Florian Krämer (https://florian-kraemer.net)
10
 * @author    Florian Krämer
11
 * @link      https://github.com/Phauthentic
12
 * @license   https://opensource.org/licenses/MIT MIT License
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phauthentic\Infrastructure\Http\Middleware;
18
19
use Psr\Http\Message\ResponseInterface;
20
use Psr\Http\Message\ServerRequestInterface;
21
use Psr\Http\Server\MiddlewareInterface;
22
use Psr\Http\Server\RequestHandlerInterface;
23
24
/**
25
 * Correlation Id Middleware
26
 */
27
class CorrelationIDMiddleware implements MiddlewareInterface
28
{
29
30
    /**
31
     * @var string
32
     */
33
    protected $correlationId;
34
35
    /**
36
     * @var string
37
     */
38
    protected $attributeName = 'CorrelationId';
39
40
    /**
41
     * @var string
42
     */
43
    protected $headerName = 'CorrelationId';
44
45
    /**
46
     * Constructor
47
     *
48
     * @param string $correlationId Correlation Id
49
     * @param string $attributeName Attribute Name
50
     * @param string $headerName Header Name
51
     */
52
    public function __construct(
53
        string $correlationId,
54
        string $attributeName = 'CorrelationId',
55
        string $headerName = 'CorrelationId'
56
    ) {
57
        $this->correlationId = $correlationId;
58
        $this->attributeName = $attributeName;
59
        $this->headerName = $headerName;
60
    }
61
62
    /**
63
     * Process an incoming server request.
64
     * Processes an incoming server request in order to produce a response.
65
     * If unable to produce the response itself, it may delegate to the provided
66
     * request handler to do so.
67
     */
68
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
69
    {
70
        $request = $request->withAttribute(
71
            $this->attributeName,
72
            $this->correlationId
73
        );
74
75
        $request = $request->withHeader(
76
            $this->headerName,
77
            $this->correlationId
78
        );
79
80
        return $handler->handle($request);
81
    }
82
}
83