CacheMiddleware   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 11
dl 0
loc 43
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A handle() 0 3 1
A terminate() 0 12 3
1
<?php
2
3
namespace Swis\Laravel\StaticRequestCache\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Swis\Laravel\StaticRequestCache\StaticRequestCache;
8
use Symfony\Component\HttpFoundation\Response;
9
10
class CacheMiddleware
11
{
12
    /**
13
     * @var \Swis\Laravel\StaticRequestCache\StaticRequestCache
14
     */
15
    protected $staticRequestCache;
16
17
    /**
18
     * @param \Swis\Laravel\StaticRequestCache\StaticRequestCache $staticRequestCache
19
     */
20 45
    public function __construct(StaticRequestCache $staticRequestCache)
21
    {
22 45
        $this->staticRequestCache = $staticRequestCache;
23 45
    }
24
25
    /**
26
     * Handle an incoming request.
27
     *
28
     * @param  Request $request
29
     * @param  \Closure $next
30
     * @return mixed
31
     */
32 9
    public function handle($request, Closure $next)
33
    {
34 9
        return $next($request);
35
    }
36
37
    /**
38
     * @param \Illuminate\Http\Request                   $request
39
     * @param \Symfony\Component\HttpFoundation\Response $response
40
     */
41 28
    public function terminate(Request $request, Response $response): void
42
    {
43 8
        $store = function () use ($request, $response) {
44 36
            if ($this->staticRequestCache->shouldStoreResponse($request, $response)) {
45 27
                $this->staticRequestCache->store($request, $response);
46
            }
47 36
        };
48
49 36
        if (config('static-html-cache.graceful')) {
50 27
            rescue($store);
51
        } else {
52 9
            $store();
53
        }
54 27
    }
55
}
56