ApiLoggerMiddleware   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 5
eloc 22
c 2
b 0
f 1
dl 0
loc 55
ccs 21
cts 21
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handleRequest() 0 5 1
A handleResponse() 0 29 3
1
<?php
2
3
namespace Nixilla\Api\LoggerBundle\Middleware;
4
5
use Buzz\Middleware\MiddlewareInterface;
6
use Nixilla\Api\LoggerBundle\Logger\ApiInterface;
7
use Psr\Http\Message\RequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
10
class ApiLoggerMiddleware implements MiddlewareInterface
11
{
12
    /** @var ApiInterface */
13
    private $logger;
14
15
    /** @var float */
16
    private $startTime;
17
18
    /**
19
     * ApiLoggerMiddleware constructor.
20
     *
21
     * @param ApiInterface $logger
22
     */
23 4
    public function __construct(ApiInterface $logger)
24
    {
25 4
        $this->logger = $logger;
26 4
    }
27
28 1
    public function handleRequest(RequestInterface $request, callable $next)
29
    {
30 1
        $this->startTime = microtime(true);
0 ignored issues
show
Documentation Bug introduced by
It seems like microtime(true) can also be of type string. However, the property $startTime is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
31
32 1
        return $next($request);
33
34
    }
35
36 2
    public function handleResponse(RequestInterface $request, ResponseInterface $response, callable $next)
37
    {
38 2
        $time = microtime(true) - $this->startTime;
39
40 2
        if($request->getHeader('Content-Type') == 'application/json')
0 ignored issues
show
introduced by
The condition $request->getHeader('Con...) == 'application/json' is always false.
Loading history...
41
        {
42 1
            $payload = json_decode($request->getBody(), true) ?: [];
43
        }
44
        else {
45 1
            parse_str($request->getBody(), $payload);
46
        }
47
48 2
        $this->logger->logCall(
49 2
            sprintf('%s://%s', $request->getUri()->getScheme(), $request->getUri()->getHost()),
50 2
            $request->getUri()->getPath(),
51 2
            $request->getMethod(),
52 2
            $time,
53 2
            $request->getHeaders(),
54 2
            $payload,
55 2
            $response->getHeaders(),
56 2
            $response->getBody()->getContents()
57
        );
58
59 2
        /**
60
         * need to rewind the contents so it can be retrieved later!!!
61
         */
62
        $response->getBody()->rewind();
63
        
64
        return $next($request, $response);
65
    }
66
}
67