48
39
public function createEmpty()
50
return new RequiredFile($this->getStore(), $this->getLog(), $this);
54
* @param string $nonce
55
* @return RequiredFile
56
* @throws NotFoundException
58
public function getByNonce($nonce)
60
$this->getLog()->debug('Required file by Nonce: ' . $nonce);
62
if ($this->directory === null) {
63
$item = $this->pool->getItem('inventory/directory');
66
$this->directory = json_decode($item->get(), true);
68
throw new NotFoundException();
72
if (count($this->directory) <= 0) {
73
$this->getLog()->debug('Empty inventory directory');
74
throw new NotFoundException();
77
if (array_key_exists($nonce, $this->directory)) {
78
// Get the nonce out of the relevent display inventory
79
$displayId = $this->directory[$nonce];
81
$this->getLog()->debug('Nonce resolves to displayId ' . $displayId);
83
return $this->getByDisplayAndNonce($displayId, $nonce);
85
$this->getLog()->debug('Nonce ' . $nonce . ' does not exist in directory.');
86
throw new NotFoundException();
91
* Get Required files for display
92
* @param int $displayId
94
public function setDisplay($displayId)
96
if ($this->displayId === $displayId)
99
// Persist what we currently have
102
// Load fresh display
106
'_totalComplete' => 0,
107
'_totalSizeComplete' => 0,
114
$item = $this->pool->getItem('inventory/' . $displayId);
116
if ($item->isHit()) {
117
$files = json_decode($item->get(), true);
119
foreach ($files as $key => $element) {
121
$file = $this->createEmpty()->hydrate($element);
123
$this->files['_total'] = $this->files['_total'] + 1;
124
$this->files['_totalSize'] = $this->files['_totalSize'] + $file->size;
126
if ($file->complete == 1) {
127
$this->files['_totalComplete'] = $this->files['_totalComplete'] + 1;
128
$this->files['_totalSizeComplete'] = $this->files['_totalSizeComplete'] + $file->size;
131
// Add the required file to the appropriate array
132
$this->addFileToLookupKey($file);
133
$this->addFileToStore($file);
137
//$this->getLog()->debug('Cache: ' . json_encode($this->files));
139
// Store the current displayId
140
$this->displayId = $displayId;
144
* @param RequiredFile $file
146
private function addFileToStore($file)
148
$this->files['nonce'][$file->nonce] = $file;
152
* @param RequiredFile $file
154
private function addFileToLookupKey($file)
156
// Add the required file to the appropriate array
157
if ($file->layoutId != 0 && $file->regionId != 0 && $file->mediaId != 0) {
158
$this->files['widget'][$file->mediaId] = $file->nonce;
159
} else if ($file->mediaId != 0) {
160
$this->files['media'][$file->mediaId] = $file->nonce;
161
} else if ($file->layoutId != 0) {
162
$this->files['layout'][$file->layoutId] = $file->nonce;
167
* @param RequiredFile $file
168
* @param string $nonce
170
public function addOrReplace($file, $nonce)
172
if ($this->displayId != $file->displayId)
173
$this->setDisplay($file->displayId);
175
// Given the required file we've been provided, find that in our current cache and replace the nonce and the pointer to it
177
// We are an existing required file, which needs removing and then adding.
178
$this->remove($file, $nonce);
181
// pop it in the current array, according to its nature
182
$this->addFileToStore($file);
184
// Add the required file to the appropriate array
185
$this->addFileToLookupKey($file);
189
* Removes a required file from the cache
190
* @param RequiredFile $file
191
* @param string $nonce
193
private function remove($file, $nonce)
195
// Remove from the cache
196
if ($file->layoutId != 0 && $file->regionId != 0 && $file->mediaId != 0) {
197
unset($this->files['widget'][$file->mediaId]);
198
} else if ($file->mediaId != 0) {
199
unset($this->files['media'][$file->mediaId]);
200
} else if ($file->layoutId != 0) {
201
unset($this->files['layout'][$file->layoutId]);
204
unset($this->files['nonce'][$nonce]);
210
public function getTotalCount()
212
return $this->files['_total'];
218
public function getCompleteCount()
220
return $this->files['_totalComplete'];
226
public function getTotalSize()
228
return $this->files['_totalSize'];
234
public function getCompleteSize()
236
return $this->files['_totalSizeComplete'];
242
public function getLayoutIds()
244
return array_keys($this->files['layout']);
250
public function getMediaIds()
252
return array_keys($this->files['media']);
258
public function getWidgetIds()
260
return array_keys($this->files['widget']);
264
* @param int $displayId
265
* @param string $nonce
266
* @return RequiredFile
267
* @throws NotFoundException
269
public function getByDisplayAndNonce($displayId, $nonce)
271
if ($this->displayId != $displayId)
272
$this->setDisplay($displayId);
274
if (!isset($this->files['nonce'][$nonce]))
275
throw new NotFoundException('Nonce not in directory required files.');
277
$rf = $this->files['nonce'][$nonce];
280
throw new NotFoundException();
41
return new RequiredFile($this->getStore(), $this->getLog());
45
* @param array $params
46
* @return RequiredFile[]
48
private function query($params)
52
if ($this->statement === null) {
53
$this->statement = $this->getStore()->getConnection()->prepare('
56
WHERE `displayId` = :displayId
58
AND `itemId` = :itemId
62
$this->statement->execute($params);
64
foreach ($this->statement->fetchAll(\PDO::FETCH_ASSOC) as $item) {
65
$files[] = $this->createEmpty()->hydrate($item);
359
127
public function createForLayout($displayId, $layoutId, $size, $path)
362
$nonce = $this->getByDisplayAndLayout($displayId, $layoutId);
130
$requiredFile = $this->getByDisplayAndLayout($displayId, $layoutId);
364
132
catch (NotFoundException $e) {
365
$nonce = $this->createEmpty();
133
$requiredFile = $this->createEmpty();
368
$nonce->displayId = $displayId;
369
$nonce->layoutId = $layoutId;
370
$nonce->size = $size;
371
$nonce->storedAs = $path;
136
$requiredFile->displayId = $displayId;
137
$requiredFile->type = 'L';
138
$requiredFile->itemId = $layoutId;
139
$requiredFile->size = $size;
140
$requiredFile->path = $path;
141
return $requiredFile;
376
145
* Create for Get Resource
377
146
* @param $displayId
381
148
* @return RequiredFile
383
public function createForGetResource($displayId, $layoutId, $regionId, $mediaId)
150
public function createForGetResource($displayId, $widgetId)
386
$nonce = $this->getByDisplayAndWidget($displayId, $mediaId);
153
$requiredFile = $this->getByDisplayAndWidget($displayId, $widgetId);
388
155
catch (NotFoundException $e) {
389
$nonce = $this->createEmpty();
156
$requiredFile = $this->createEmpty();
392
$nonce->displayId = $displayId;
393
$nonce->layoutId = $layoutId;
394
$nonce->regionId = $regionId;
395
$nonce->mediaId = $mediaId;
159
$requiredFile->displayId = $displayId;
160
$requiredFile->type = 'W';
161
$requiredFile->itemId = $widgetId;
162
return $requiredFile;
407
173
public function createForMedia($displayId, $mediaId, $size, $path)
410
$nonce = $this->getByDisplayAndMedia($displayId, $mediaId);
176
$requiredFile = $this->getByDisplayAndMedia($displayId, $mediaId);
412
178
catch (NotFoundException $e) {
413
$nonce = $this->createEmpty();
416
$nonce->displayId = $displayId;
417
$nonce->mediaId = $mediaId;
418
$nonce->size = $size;
419
$nonce->storedAs = $path;
427
public function expireAll($displayId)
429
$this->setDisplay($displayId);
431
// Go through each nonce and set it to a short expiry
432
foreach ($this->files['nonce'] as $file) {
433
/** @var RequiredFile $file */
439
* Persist the current pool to the cache
441
public function persist()
443
if ($this->displayId == null)
446
$directoryItem = $this->pool->getItem('inventory/directory');
448
if ($directoryItem->isHit()) {
449
$directory = json_decode($directoryItem->get(), true);
454
// Combine our nonce directory with the existing global directory
455
foreach ($this->files['nonce'] as $key => $value) {
456
/** @var RequiredFile $value */
457
if ($value->isExpired()) {
458
unset($directory[$key]);
459
$this->remove($value, $value->nonce);
461
$directory[$key] = $this->displayId;
465
// Overwrite the pool cache
466
$item = $this->pool->getItem('inventory/' . $this->displayId);
467
$item->set(json_encode($this->files['nonce']));
468
$item->expiresAfter(new \DateInterval('P2M'));
471
$directoryItem->set(json_encode($directory));
472
$directoryItem->expiresAfter(new \DateInterval('P2M'));
474
$this->pool->saveDeferred($item);
475
$this->pool->saveDeferred($directoryItem);
179
$requiredFile = $this->createEmpty();
182
$requiredFile->displayId = $displayId;
183
$requiredFile->type = 'M';
184
$requiredFile->itemId = $mediaId;
185
$requiredFile->size = $size;
186
$requiredFile->path = $path;
187
return $requiredFile;
b'\\ No newline at end of file'