Completed
Pull Request — 1.x (#53)
by Akihito
01:43
created

HttpCacheInterceptor::invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * This file is part of the BEAR.QueryRepository package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\QueryRepository;
8
9
use BEAR\RepositoryModule\Annotation\AbstractCacheControl;
10
use Doctrine\Common\Annotations\Reader;
11
use Ray\Aop\MethodInterceptor;
12
use Ray\Aop\MethodInvocation;
13
14
class HttpCacheInterceptor implements MethodInterceptor
15
{
16
    /**
17
     * @var Reader
18
     */
19
    private $reader;
20
21
    public function __construct(
22
        Reader $annotationReader
23
    ) {
24
        $this->reader = $annotationReader;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function invoke(MethodInvocation $invocation)
31
    {
32
        $cacheControl = $this->reader->getClassAnnotation($invocation->getMethod()->getDeclaringClass(), AbstractCacheControl::class);
33
        $ro = $invocation->proceed();
34
        if ($ro->code === 200) {
35
            $ro->headers['Cache-Control'] = (string) $cacheControl;
36
        }
37
38
        return $ro;
39
    }
40
}
41