Completed
Pull Request — master (#31)
by Gerardo
01:09
created

DefaultLogResponseWriter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 5
dl 0
loc 32
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B logResponse() 0 29 6
1
<?php
2
3
namespace Spatie\HttpLogger;
4
5
use Exception;
6
use Illuminate\Http\JsonResponse;
7
use Illuminate\Http\Request;
8
use Illuminate\Http\Response as HtmlResponse;
9
use Illuminate\Support\Facades\Log;
10
use Symfony\Component\HttpFoundation\Response;
11
12
class DefaultLogResponseWriter implements LogResponseWriter
13
{
14
    public function logResponse(Request $request, Response $response)
15
    {
16
        $method = strtoupper($request->getMethod());
17
18
        $uri = $request->getPathInfo();
19
20
        $message = "{$method} {$uri} - ResponseBody: {}";
21
22
        try {
23
            if ($response instanceof Response) {
24
                if ($response->getStatusCode() == 200) {
25
                    if ($response instanceof JsonResponse) {
26
                        $body = json_encode($response->getData());
27
                    } elseif ($response instanceof HtmlResponse) {
28
                        $body = $response->getContent();
29
                    } else {
30
                        $body = $response;
31
                    }
32
33
                    $message = "{$method} {$uri} - ResponseBody: {$body}";
34
                } else {
35
                    $message = "{$method} {$uri} - ResponseBodyCode: {$response->getStatusCode()}";
36
                }
37
            }
38
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
39
        }
40
41
        Log::info($message);
42
    }
43
}
44