Completed
Push — 1.x ( fc62ce...abf8ac )
by Akihito
02:10
created

CacheInterceptor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 94.12%

Importance

Changes 6
Bugs 0 Features 2
Metric Value
wmc 4
c 6
b 0
f 2
lcom 1
cbo 4
dl 0
loc 56
ccs 16
cts 17
cp 0.9412
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A invoke() 0 21 3
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
    /**
34
     * @param QueryRepositoryInterface $repository
35
     * @param EtagSetterInterface      $setEtag
36
     */
37 13
    public function __construct(
38
        QueryRepositoryInterface $repository,
39
        EtagSetterInterface $setEtag,
40
        Reader $annotationReader
41
    ) {
42 13
        $this->repository = $repository;
43 13
        $this->setEtag = $setEtag;
44 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...
45 13
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 11
    public function invoke(MethodInvocation $invocation)
51
    {
52 11
        $resourceObject = $invocation->getThis();
53
        /* @var $resourceObject ResourceObject */
54 11
        $stored = $this->repository->get($resourceObject->uri);
0 ignored issues
show
Compatibility introduced by
$resourceObject->uri of type object<BEAR\Resource\AbstractUri> is not a sub-type of object<BEAR\Resource\Uri>. It seems like you assume a child class of the class BEAR\Resource\AbstractUri to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
55 11
        if ($stored) {
56 8
            list($resourceObject->code, $resourceObject->headers, $resourceObject->body, $resourceObject->view) = $stored;
57
58 8
            return $resourceObject;
59
        }
60
        /* @var $cacheable Cacheable */
61
        try {
62 11
            $resourceObject = $invocation->proceed();
63 11
            $this->setEtag->__invoke($resourceObject);
64 11
            $this->repository->put($resourceObject);
65 11
        } catch (\Exception $e) {
66
            $this->repository->purge($resourceObject->uri);
67
        }
68
69 11
        return $resourceObject;
70
    }
71
}
72