WritesHttpLogs   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 13
c 4
b 0
f 0
dl 0
loc 29
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A log() 0 15 3
1
<?php
2
3
namespace Sarahman\HttpRequestApiLog\Traits;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Sarahman\HttpRequestApiLog\Helper;
7
use Sarahman\HttpRequestApiLog\Models\ApiLog;
8
9
trait WritesHttpLogs
10
{
11
    use RemovesUnwantedParams;
12
13
    protected $enableLogging = true;
14
15
    /**
16
     * Write HTTP log to the database.
17
     *
18
     * @param string            $method   Name of the method
19
     * @param string            $endpoint Name of the endpoint
20
     * @param array|null        $params   Request parameters
21
     * @param ResponseInterface $response Response received
22
     */
23
    final protected function log($method, $endpoint, $params, ResponseInterface $response)
24
    {
25
        if (!($this->enableLogging && Helper::getConfig('enabled', true))) {
26
            return;
27
        }
28
29
        $params = (array) $params;
30
31
        ApiLog::create([
32
            'client'        => class_basename($this),
33
            'method'        => $method,
34
            'endpoint'      => $endpoint,
35
            'params'        => $this->removeUnwantedParams($params),
36
            'response_code' => $response->getStatusCode(),
37
            'response'      => (string) $response->getBody(),
38
        ]);
39
    }
40
}
41