Skip to content

Commit 81bb78b

Browse files
committed
Refactored formatting
1 parent ba936f7 commit 81bb78b

15 files changed

+266
-174
lines changed

src/Formatter/BothFormatter.php

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
5+
namespace PhpMiddleware\LogHttpMessages\Formatter;
6+
7+
use Psr\Http\Message\ResponseInterface;
8+
use Psr\Http\Message\ServerRequestInterface;
9+
10+
/**
11+
* @codeCoverageIgnore
12+
*/
13+
final class EmptyMessageFormatter implements ServerRequestFormatter, ResponseFormatter
14+
{
15+
public function formatServerRequest(ServerRequestInterface $request): FormattedMessage
16+
{
17+
return FormattedMessage::createEmpty();
18+
}
19+
20+
public function formatResponse(ResponseInterface $response): FormattedMessage
21+
{
22+
return FormattedMessage::createEmpty();
23+
}
24+
}

src/Formatter/FormattedMessage.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
5+
namespace PhpMiddleware\LogHttpMessages\Formatter;
6+
7+
final class FormattedMessage
8+
{
9+
private $value;
10+
11+
public static function fromString(string $value) : self
12+
{
13+
$instance = new self();
14+
$instance->value = $value;
15+
16+
return $instance;
17+
}
18+
19+
public static function fromArray(array $value) : self
20+
{
21+
$instance = new self();
22+
$instance->value = $value;
23+
24+
return $instance;
25+
}
26+
27+
public static function createEmpty() : self
28+
{
29+
return new self();
30+
}
31+
32+
/**
33+
* @return array|string|null
34+
*/
35+
public function getValue()
36+
{
37+
return $this->value;
38+
}
39+
}

src/Formatter/HttpMessagesFormatter.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/Formatter/RequestFormatter.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/Formatter/ResponseFormatter.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
<?php
22

3+
declare (strict_types=1);
4+
35
namespace PhpMiddleware\LogHttpMessages\Formatter;
46

57
use Psr\Http\Message\ResponseInterface;
6-
use Psr\Http\Message\ServerRequestInterface;
7-
use Zend\Diactoros\Response\Serializer;
88

9-
final class ResponseFormatter implements HttpMessagesFormatter
9+
interface ResponseFormatter
1010
{
11-
public function format(ServerRequestInterface $request, ResponseInterface $response)
12-
{
13-
return Serializer::toString($response);
14-
}
11+
public function formatResponse(ResponseInterface $response) : FormattedMessage;
1512
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
5+
namespace PhpMiddleware\LogHttpMessages\Formatter;
6+
7+
use Psr\Http\Message\ServerRequestInterface;
8+
9+
interface ServerRequestFormatter
10+
{
11+
public function formatServerRequest(ServerRequestInterface $request) : FormattedMessage;
12+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
5+
namespace PhpMiddleware\LogHttpMessages\Formatter;
6+
7+
use Psr\Http\Message\ResponseInterface;
8+
use Psr\Http\Message\ServerRequestInterface;
9+
use Zend\Diactoros\Response\ArraySerializer as ResponseSerializer;
10+
use Zend\Diactoros\Request\ArraySerializer as RequestSerializer;
11+
12+
final class ZendDiactorosToArrayMessageFormatter implements ServerRequestFormatter, ResponseFormatter
13+
{
14+
public function formatResponse(ResponseInterface $response): FormattedMessage
15+
{
16+
$array = ResponseSerializer::toArray($response);
17+
18+
return FormattedMessage::fromArray($array);
19+
}
20+
21+
public function formatServerRequest(ServerRequestInterface $request): FormattedMessage
22+
{
23+
$array = RequestSerializer::toArray($request);
24+
25+
return FormattedMessage::fromArray($array);
26+
}
27+
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
5+
namespace PhpMiddleware\LogHttpMessages\Formatter;
6+
7+
use Psr\Http\Message\ResponseInterface;
8+
use Psr\Http\Message\ServerRequestInterface;
9+
use Zend\Diactoros\Response\Serializer as ResponseSerializer;
10+
use Zend\Diactoros\Request\Serializer as RequestSerializer;
11+
12+
final class ZendDiactorosToStringMessageFormatter implements ServerRequestFormatter, ResponseFormatter
13+
{
14+
public function formatResponse(ResponseInterface $response): FormattedMessage
15+
{
16+
$string = ResponseSerializer::toString($response);
17+
18+
return FormattedMessage::fromString($string);
19+
}
20+
21+
public function formatServerRequest(ServerRequestInterface $request): FormattedMessage
22+
{
23+
$string = RequestSerializer::toString($request);
24+
25+
return FormattedMessage::fromString($string);
26+
}
27+
}

src/LogMiddleware.php

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,38 @@
66

77
use Interop\Http\ServerMiddleware\DelegateInterface;
88
use Interop\Http\ServerMiddleware\MiddlewareInterface;
9-
use PhpMiddleware\LogHttpMessages\Formatter\HttpMessagesFormatter;
9+
use PhpMiddleware\LogHttpMessages\Formatter\ResponseFormatter;
10+
use PhpMiddleware\LogHttpMessages\Formatter\ServerRequestFormatter;
1011
use Psr\Http\Message\ResponseInterface as Response;
1112
use Psr\Http\Message\ServerRequestInterface as ServerRequest;
1213
use Psr\Log\LoggerInterface as Logger;
1314
use Psr\Log\LogLevel;
14-
use UnexpectedValueException;
1515

1616
final class LogMiddleware implements MiddlewareInterface
1717
{
18-
protected $logger;
19-
20-
protected $level;
21-
22-
protected $formatter;
23-
24-
public function __construct(HttpMessagesFormatter $formatter, Logger $logger, $level = LogLevel::INFO)
25-
{
26-
$this->formatter = $formatter;
18+
const LOG_MESSAGE = 'Request/Response';
19+
20+
private $logger;
21+
private $level;
22+
private $requestFormatter;
23+
private $responseFormatter;
24+
private $logMessage;
25+
26+
public function __construct(
27+
ServerRequestFormatter $requestFormatter,
28+
ResponseFormatter $responseFormatter,
29+
Logger $logger,
30+
string $level = LogLevel::INFO,
31+
string $logMessage = self::LOG_MESSAGE
32+
) {
33+
$this->requestFormatter = $requestFormatter;
34+
$this->responseFormatter = $responseFormatter;
2735
$this->logger = $logger;
2836
$this->level = $level;
37+
$this->logMessage = $logMessage;
2938
}
3039

31-
/**
32-
* @param ServerRequest $request
33-
* @param Response $response
34-
* @param callable $next
35-
*
36-
* @return Response
37-
*/
38-
public function __invoke(ServerRequest $request, Response $response, callable $next)
40+
public function __invoke(ServerRequest $request, Response $response, callable $next) : Response
3941
{
4042
$outResponse = $next($request, $response);
4143

@@ -44,13 +46,7 @@ public function __invoke(ServerRequest $request, Response $response, callable $n
4446
return $outResponse;
4547
}
4648

47-
/**
48-
* @param ServerRequest $request
49-
* @param DelegateInterface $delegate
50-
*
51-
* @return Response
52-
*/
53-
public function process(ServerRequest $request, DelegateInterface $delegate)
49+
public function process(ServerRequest $request, DelegateInterface $delegate) : Response
5450
{
5551
$response = $delegate->process($request);
5652

@@ -59,21 +55,14 @@ public function process(ServerRequest $request, DelegateInterface $delegate)
5955
return $response;
6056
}
6157

62-
/**
63-
* @param ServerRequest $request
64-
* @param Response $response
65-
*
66-
* @throws UnexpectedValueException
67-
*/
6858
private function logMessages(ServerRequest $request, Response $response)
6959
{
70-
$messages = $this->formatter->format($request, $response);
71-
72-
if (!is_string($messages)) {
73-
throw new UnexpectedValueException(sprintf('%s must return string', HttpMessagesFormatter::class));
74-
}
60+
$formattedRequest = $this->requestFormatter->formatServerRequest($request);
61+
$formattedResponse = $this->responseFormatter->formatResponse($response);
7562

76-
$this->logger->log($this->level, $messages);
63+
$this->logger->log($this->level, $this->logMessage, [
64+
'request' => $formattedRequest->getValue(),
65+
'response' => $formattedResponse->getValue(),
66+
]);
7767
}
78-
7968
}

0 commit comments

Comments
 (0)