Skip to content

Commit cd8da31

Browse files
committed
Improve readability of middleware
1 parent c600eb0 commit cd8da31

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

src/RequestIdMiddleware.php

+30-5
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,43 @@ public function __construct(
4949
*/
5050
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
5151
{
52-
$requestIdProvider = $this->requestIdProviderFactory->create($request);
53-
$this->requestId = $requestIdProvider->getRequestId();
54-
$requestWithAttribute = $request->withAttribute(self::ATTRIBUTE_NAME, $this->requestId);
52+
$requestWithAttribute = $this->attachRequestIdToAttribute($request);
5553

5654
$response = $delegate->process($requestWithAttribute);
5755

58-
if (is_string($this->responseHeader)) {
59-
return $response->withHeader($this->responseHeader, $this->requestId);
56+
if ($this->canAttachToResponse()) {
57+
return $this->attachRequestIdToResponse($response);
6058
}
6159
return $response;
6260
}
6361

62+
/**
63+
* @return ResponseInterface
64+
*/
65+
private function attachRequestIdToAttribute(ServerRequestInterface $request)
66+
{
67+
$requestIdProvider = $this->requestIdProviderFactory->create($request);
68+
$this->requestId = $requestIdProvider->getRequestId();
69+
70+
return $request->withAttribute(self::ATTRIBUTE_NAME, $this->requestId);
71+
}
72+
73+
/**
74+
* @return ResponseInterface
75+
*/
76+
private function attachRequestIdToResponse(ResponseInterface $response)
77+
{
78+
return $response->withHeader($this->responseHeader, $this->requestId);
79+
}
80+
81+
/**
82+
* @return bool
83+
*/
84+
private function canAttachToResponse()
85+
{
86+
return is_string($this->responseHeader) && !empty($this->responseHeader);
87+
}
88+
6489
/**
6590
* @return mixed
6691
*

0 commit comments

Comments
 (0)