Passed
Push — 6.0 ( 3dc798...7ca19f )
by liu
02:48
created

CheckRequestCache::handle()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 8
nop 2
dl 0
loc 31
rs 8.8333
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think\middleware;
14
15
use Closure;
16
use think\Cache;
17
use think\Request;
18
use think\Response;
19
20
class CheckRequestCache
1 ignored issue
show
Coding Style introduced by
Missing doc comment for class CheckRequestCache
Loading history...
21
{
22
    protected $cache;
23
24
    public function __construct(Cache $cache)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
25
    {
26
        $this->cache = $cache;
27
    }
28
29
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $next should have a doc-comment as per coding-style.
Loading history...
30
     * 设置当前地址的请求缓存
31
     * @access public
32
     * @param Request $request
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
33
     * @param         $next
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 9
Loading history...
34
     * @return Response
35
     */
36
    public function handle($request, Closure $next)
37
    {
38
        $cache = $request->cache();
39
40
        if ($cache) {
41
            list($key, $expire, $tag) = $cache;
42
43
            if (strtotime($request->server('HTTP_IF_MODIFIED_SINCE')) + $expire > $request->server('REQUEST_TIME')) {
44
                // 读取缓存
45
                return Response::create()->code(304);
46
            } elseif ($this->cache->has($key)) {
47
                list($content, $header) = $this->cache->get($key);
48
49
                return Response::create($content)->header($header);
50
            }
51
        }
52
53
        $response = $next($request);
54
55
        if (200 == $response->getCode() && $response->isAllowCache()) {
56
            if ($cache) {
57
                $header                  = $response->getHeader();
58
                $header['Cache-Control'] = 'max-age=' . $expire . ',must-revalidate';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $expire does not seem to be defined for all execution paths leading up to this point.
Loading history...
59
                $header['Last-Modified'] = gmdate('D, d M Y H:i:s') . ' GMT';
60
                $header['Expires']       = gmdate('D, d M Y H:i:s', time() + $expire) . ' GMT';
61
62
                $this->cache->tag($tag)->set($key, [$response->getContent(), $header], $expire);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $tag does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $key does not seem to be defined for all execution paths leading up to this point.
Loading history...
63
            }
64
        }
65
66
        return $response;
67
    }
68
}
69