|
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; |
|
|
|
|
|
|
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
|
|
|
|
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.