1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Arkitekto\RequestId library. |
5
|
|
|
* |
6
|
|
|
* (c) Alexandru Furculita <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE.md. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Arki\RequestId\Integrations\Symfony\Decorators; |
13
|
|
|
|
14
|
|
|
use Arki\RequestId\Providers\RequestIdProvider; |
15
|
|
|
use Arki\RequestId\Providers\RequestIdProviderFactory; |
16
|
|
|
use Arki\RequestId\RequestId; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
19
|
|
|
|
20
|
|
|
final class DefaultDecorator implements RequestDecorator, ResponseDecorator |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var RequestIdProvider |
24
|
|
|
*/ |
25
|
|
|
private $providerFactory; |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $requestHeader; |
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $responseHeader; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param RequestIdProviderFactory $providerFactory |
37
|
|
|
* @param string $requestHeader |
38
|
|
|
* @param string $responseHeader |
39
|
|
|
*/ |
40
|
|
|
public function __construct( |
41
|
|
|
RequestIdProviderFactory $providerFactory, |
42
|
|
|
$requestHeader = RequestId::HEADER_NAME, |
43
|
|
|
$responseHeader = RequestId::HEADER_NAME |
44
|
|
|
) { |
45
|
|
|
$this->providerFactory = $providerFactory; |
|
|
|
|
46
|
|
|
$this->requestHeader = $requestHeader; |
47
|
|
|
$this->responseHeader = $responseHeader; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Request $request |
52
|
|
|
* |
53
|
|
|
* @return Request |
54
|
|
|
*/ |
55
|
|
|
public function decorateRequest(Request $request) |
56
|
|
|
{ |
57
|
|
|
$provider = $this->providerFactory->create($request); |
|
|
|
|
58
|
|
|
|
59
|
|
|
if (null === $provider->getRequestId()) { |
60
|
|
|
return $request; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$request->headers->set($this->requestHeader, $provider->getRequestId()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param Response $response |
68
|
|
|
* |
69
|
|
|
* @return Response |
70
|
|
|
*/ |
71
|
|
|
public function decorateResponse(Response $response) |
72
|
|
|
{ |
73
|
|
|
if (null === $this->providerFactory->getRequestId()) { |
74
|
|
|
return $response; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$response->headers->set($this->responseHeader, $this->providerFactory->getRequestId()); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..