CacheableByVarnish   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 39
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 12 2
1
<?php
2
3
namespace RichanFongdasen\Varnishable\Middleware;
4
5
use Closure;
6
use RichanFongdasen\Varnishable\VarnishableService;
7
use Symfony\Component\HttpFoundation\Request;
8
9
class CacheableByVarnish
10
{
11
    /**
12
     * Varnishable Service Object.
13
     *
14
     * @var \RichanFongdasen\Varnishable\VarnishableService
15
     */
16
    protected $varnishable;
17
18
    /**
19
     * CacheableByVarnish Middleware constructor.
20
     */
21
    public function __construct()
22
    {
23
        $this->varnishable = app(VarnishableService::class);
24
    }
25
26
    /**
27
     * Handle an incoming request.
28
     *
29
     * @param \Symfony\Component\HttpFoundation\Request $request
30
     * @param \Closure                                  $next
31
     * @param int                                       $cacheDuration
32
     *
33
     * @return \Symfony\Component\HttpFoundation\Response
34
     */
35
    public function handle(Request $request, Closure $next, int $cacheDuration = 0)
36
    {
37
        $this->varnishable->setRequestHeaders($request->headers);
38
39
        if ($cacheDuration > 0) {
40
            $this->varnishable->setCacheDuration($cacheDuration);
41
        }
42
43
        $response = $next($request);
44
45
        return $this->varnishable->manipulate($response);
46
    }
47
}
48