1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
5
|
|
|
* Date: 26/01/16 |
6
|
|
|
* Time: 23:55. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace NilPortugues\Foundation\Infrastructure\Model\Repository\Cache; |
12
|
|
|
|
13
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Fields; |
14
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Filter; |
15
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Identity; |
16
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Pageable; |
17
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\PageRepository; |
18
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\ReadRepository; |
19
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Sort; |
20
|
|
|
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\WriteRepository; |
21
|
|
|
use Stash\Interfaces\PoolInterface; |
22
|
|
|
|
23
|
|
|
class RepositoryCache implements ReadRepository, WriteRepository, PageRepository |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var PoolInterface |
27
|
|
|
*/ |
28
|
|
|
protected $cache; |
29
|
|
|
/** |
30
|
|
|
* @var ReadRepository|WriteRepository|PageRepository |
31
|
|
|
*/ |
32
|
|
|
protected $repository; |
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $cacheNamespace; |
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $cacheNamespaceFindBy; |
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $cacheNamespacePage; |
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $cacheNamespaceCount; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var null |
52
|
|
|
*/ |
53
|
|
|
protected $cacheTime = null; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* RepositoryCache constructor. |
57
|
|
|
* |
58
|
|
|
* @param PoolInterface $cache |
59
|
|
|
* @param mixed $repository |
60
|
|
|
* @param string $classFQN |
61
|
|
|
* @param null $cacheTime |
62
|
|
|
*/ |
63
|
|
|
public function __construct(PoolInterface $cache, $repository, $classFQN, $cacheTime = null) |
64
|
|
|
{ |
65
|
|
|
$this->cache = $cache; |
66
|
|
|
$this->repository = $repository; |
67
|
|
|
$this->cacheTime = ($cacheTime) ? strtotime(sprintf('now + %s seconds', $cacheTime)) : $cacheTime; |
|
|
|
|
68
|
|
|
|
69
|
|
|
$this->buildCacheKeys($classFQN); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Creates the cache key for the repository. |
74
|
|
|
* |
75
|
|
|
* @param string $classFQN |
76
|
|
|
*/ |
77
|
|
|
protected function buildCacheKeys($classFQN) |
78
|
|
|
{ |
79
|
|
|
$parts = explode('\\', $classFQN); |
80
|
|
|
$classFQN = array_pop($parts); |
81
|
|
|
|
82
|
|
|
$baseKey = str_replace('\\', '.', strtolower($classFQN)); |
83
|
|
|
$this->cacheNamespace = sprintf('/%s/', $baseKey); |
84
|
|
|
$this->cacheNamespaceFindBy = sprintf('/%s/findby/', $baseKey); |
85
|
|
|
$this->cacheNamespacePage = sprintf('/%s/paginated/', $baseKey); |
86
|
|
|
$this->cacheNamespaceCount = sprintf('/%s/count/', $baseKey); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function find(Identity $id, Fields $fields = null) |
93
|
|
|
{ |
94
|
|
|
$key = $this->cacheNamespace.$id->id(); |
95
|
|
|
$cachedItem = $this->cache->getItem($key); |
96
|
|
|
|
97
|
|
|
if (!$cachedItem->isMiss() && null !== ($result = $cachedItem->get())) { |
98
|
|
|
return $result; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$entity = $this->repository->find($id, $fields); |
|
|
|
|
102
|
|
|
$cachedItem->set($entity, $this->cacheTime); |
|
|
|
|
103
|
|
|
|
104
|
|
|
return $entity; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
|
|
public function findBy(Filter $filter = null, Sort $sort = null, Fields $fields = null) |
111
|
|
|
{ |
112
|
|
|
$cachedItem = $this->cache->getItem( |
113
|
|
|
$this->cacheNamespaceFindBy.md5(serialize($filter).serialize($sort).serialize($fields)) |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
if (!$cachedItem->isMiss() && null !== ($result = $cachedItem->get())) { |
117
|
|
|
return $result; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$result = $this->repository->findBy($filter, $sort, $fields); |
|
|
|
|
121
|
|
|
$cachedItem->set($result, $this->cacheTime); |
|
|
|
|
122
|
|
|
|
123
|
|
|
return $result; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
|
|
public function add(Identity $value) |
130
|
|
|
{ |
131
|
|
|
$entity = $this->repository->add($value); |
|
|
|
|
132
|
|
|
|
133
|
|
|
$cachedItem = $this->cache->getItem($this->cacheNamespace.$entity->id()); |
134
|
|
|
$cachedItem->set($entity, $this->cacheTime); |
|
|
|
|
135
|
|
|
|
136
|
|
|
return $entity; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
|
|
public function remove(Identity $id) |
143
|
|
|
{ |
144
|
|
|
$result = $this->repository->remove($id); |
|
|
|
|
145
|
|
|
|
146
|
|
|
$this->cache->getItem($this->cacheNamespace.$id->id())->clear(); |
147
|
|
|
$this->cache->getItem($this->cacheNamespacePage)->clear(); |
148
|
|
|
$this->cache->getItem($this->cacheNamespaceFindBy)->clear(); |
149
|
|
|
$this->cache->getItem($this->cacheNamespaceCount)->clear(); |
150
|
|
|
|
151
|
|
|
return $result; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
*/ |
157
|
|
View Code Duplication |
public function findAll(Pageable $pageable = null) |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
$cachedItem = $this->cache->getItem($this->cacheNamespacePage.md5(serialize($pageable))); |
160
|
|
|
|
161
|
|
|
if (!$cachedItem->isMiss() && null !== ($result = $cachedItem->get())) { |
162
|
|
|
return $result; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$result = $this->repository->findAll($pageable); |
|
|
|
|
166
|
|
|
$cachedItem->set($result, $this->cacheTime); |
|
|
|
|
167
|
|
|
|
168
|
|
|
return $result; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* {@inheritdoc} |
173
|
|
|
*/ |
174
|
|
View Code Duplication |
public function count(Filter $filter = null) |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
$cachedItem = $this->cache->getItem($this->cacheNamespaceCount.md5(serialize($filter))); |
177
|
|
|
|
178
|
|
|
if (!$cachedItem->isMiss() && null !== ($result = $cachedItem->get())) { |
179
|
|
|
return $result; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$result = $this->repository->count($filter); |
|
|
|
|
183
|
|
|
$cachedItem->set($result, $this->cacheTime); |
|
|
|
|
184
|
|
|
|
185
|
|
|
return $result; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* {@inheritdoc} |
190
|
|
|
*/ |
191
|
|
|
public function exists(Identity $id) |
192
|
|
|
{ |
193
|
|
|
return $this->repository->exists($id); |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* {@inheritdoc} |
198
|
|
|
*/ |
199
|
|
|
public function addAll(array $values) |
200
|
|
|
{ |
201
|
|
|
$result = $this->repository->addAll($values); |
|
|
|
|
202
|
|
|
|
203
|
|
|
$this->cache->getItem($this->cacheNamespace)->clear(); |
204
|
|
|
|
205
|
|
|
return $result; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* {@inheritdoc} |
210
|
|
|
*/ |
211
|
|
|
public function removeAll(Filter $filter = null) |
212
|
|
|
{ |
213
|
|
|
$result = $this->repository->removeAll($filter); |
|
|
|
|
214
|
|
|
|
215
|
|
|
$this->cache->getItem($this->cacheNamespace)->clear(); |
216
|
|
|
|
217
|
|
|
return $result; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
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 mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.