Completed
Push — master ( 1c4beb...da5a36 )
by Richan
01:11
created

CacheableByVarnish::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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