Passed
Push — master ( cea7d1...f386a3 )
by huang
03:09
created

CacheMiddleware::handle()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3.0021

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 16
nc 3
nop 2
dl 0
loc 24
rs 8.9713
c 1
b 0
f 0
ccs 15
cts 16
cp 0.9375
crap 3.0021
1
<?php
2
/**
3
 * @author    jan huang <[email protected]>
4
 * @copyright 2016
5
 *
6
 * @see      https://www.github.com/janhuang
7
 * @see      http://www.fast-d.cn/
8
 */
9
10
namespace FastD\Middleware;
11
12
use FastD\Packet\Json;
13
use FastD\Utils\DateObject;
14
use Psr\Http\Message\ResponseInterface;
15
use Psr\Http\Message\ServerRequestInterface;
16
17
/**
18
 * Class CacheMiddleware.
19
 */
20
class CacheMiddleware extends Middleware
21
{
22
    /**
23
     * @param ServerRequestInterface $request
24
     * @param DelegateInterface      $next
25
     *
26
     * @return ResponseInterface
27
     */
28 3
    public function handle(ServerRequestInterface $request, DelegateInterface $next)
29
    {
30 3
        $action = $request->getMethod();
31 3
        if ('GET' !== $action) {
32
            return $next->next($request);
33
        }
34
35 3
        $key = md5($request->getUri()->getPath());
36 3
        $cache = cache()->getItem($key);
37 3
        if ($cache->isHit()) {
38 2
            $value = Json::decode($cache->get());
39
40 2
            return json($value)
0 ignored issues
show
Documentation introduced by
$value is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41 2
                ->withHeader('X-Cache', $key);
42
        }
43 1
        $response = $next->next($request);
44 1
        $cache->set((string) $response->getBody());
45
46 1
        $expireAt = DateObject::createFromTimestamp(time() + config()->get('common.cache.lifetime', 60));
47 1
        $cache->expiresAt($expireAt);
48 1
        cache()->save($cache);
49
50 1
        return $response->withExpires($expireAt);
51
    }
52
}
53