Completed
Push — cache ( 387af8...e6ab69 )
by Akihito
02:15 queued 12s
created

CacheInterceptor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 1
eloc 7
nc 1
nop 3
crap 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\Cacheable;
10
use BEAR\Resource\ResourceObject;
11
use Doctrine\Common\Annotations\AnnotationReader;
12
use Doctrine\Common\Annotations\Reader;
13
use Ray\Aop\MethodInterceptor;
14
use Ray\Aop\MethodInvocation;
15
16
class CacheInterceptor implements MethodInterceptor
17
{
18
    /**
19
     * @var QueryRepositoryInterface
20
     */
21
    private $repository;
22
23
    /**
24
     * @var EtagSetterInterface
25
     */
26
    private $setEtag;
27
28
    /**
29
     * @var AnnotationReader
30
     */
31
    private $reader;
32
33 13
    public function __construct(
34
        QueryRepositoryInterface $repository,
35
        EtagSetterInterface $setEtag,
36
        Reader $annotationReader
37
    ) {
38 13
        $this->repository = $repository;
39 13
        $this->setEtag = $setEtag;
40 13
        $this->reader = $annotationReader;
0 ignored issues
show
Documentation Bug introduced by
$annotationReader is of type object<Doctrine\Common\Annotations\Reader>, but the property $reader was declared to be of type object<Doctrine\Common\A...tions\AnnotationReader>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
41 13
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 11
    public function invoke(MethodInvocation $invocation)
47
    {
48 11
        $resourceObject = $invocation->getThis();
49
        /* @var $resourceObject ResourceObject */
50 11
        $stored = $this->repository->get($resourceObject->uri);
51 11
        if ($stored) {
52 8
            list($resourceObject->code, $resourceObject->headers, $resourceObject->body, $resourceObject->view) = $stored;
53
54 8
            return $resourceObject;
55
        }
56
        /* @var $cacheable Cacheable */
57
        try {
58 11
            $resourceObject = $invocation->proceed();
59 11
            $this->repository->put($resourceObject);
60 11
            $this->setEtag->__invoke($resourceObject);
61
        } catch (\Exception $e) {
62
            $this->repository->purge($resourceObject->uri);
63
            error_log($e);
64
        }
65
66 11
        return $resourceObject;
67
    }
68
}
69