|
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
|
2 |
|
public function __construct( |
|
53
|
|
|
string $correlationID, |
|
54
|
|
|
string $attributeName = 'CorrelationID', |
|
55
|
|
|
string $headerName = 'CorrelationID' |
|
56
|
|
|
) { |
|
57
|
2 |
|
$this->correlationID = $correlationID; |
|
58
|
2 |
|
$this->attributeName = $attributeName; |
|
59
|
2 |
|
$this->headerName = $headerName; |
|
60
|
2 |
|
} |
|
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
|
2 |
|
public function process( |
|
69
|
|
|
ServerRequestInterface $request, |
|
70
|
|
|
RequestHandlerInterface $handler |
|
71
|
|
|
): ResponseInterface |
|
72
|
|
|
{ |
|
73
|
|
|
if ( |
|
74
|
2 |
|
$request->getHeader($this->headerName) === [] |
|
75
|
2 |
|
|| $request->getAttribute($this->headerName, false) |
|
76
|
|
|
) { |
|
77
|
1 |
|
$request = $request->withAttribute( |
|
78
|
1 |
|
$this->attributeName, |
|
79
|
1 |
|
$this->correlationID |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
1 |
|
$request = $request->withHeader( |
|
83
|
1 |
|
$this->headerName, |
|
84
|
1 |
|
$this->correlationID |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
2 |
|
return $handler->handle($request); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|