Skip to content

Commit

Permalink
Make sure we can start using this TaggingPSRItem without dropping all…
Browse files Browse the repository at this point in the history
… existing data. (#89)

* Make sure we can start using this TaggingPSRItem without dropping all existing data.
  • Loading branch information
Nyholm authored Aug 8, 2016
1 parent 2254983 commit 8ae3042
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.

## UNRELEASED
## 0.4.3

### Fixed

* Do not lose the data when you start using the `TaggablePSR6PoolAdapter`

## 0.4.2

Expand Down
20 changes: 18 additions & 2 deletions TaggablePSR6ItemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ public function get()
{
$rawItem = $this->cacheItem->get();

if (is_array($rawItem) && isset($rawItem['value'])) {
// If it is a cache item we created
if ($this->isItemCreatedHere($rawItem)) {
return $rawItem['value'];
}

// This is an item stored before we used this fake cache
return $rawItem;
}

/**
Expand Down Expand Up @@ -178,12 +182,24 @@ private function initializeTags()
if ($this->cacheItem->isHit()) {
$rawItem = $this->cacheItem->get();

if (is_array($rawItem) && isset($rawItem['tags'])) {
if ($this->isItemCreatedHere($rawItem)) {
$this->tags = $rawItem['tags'];
}
}

$this->initialized = true;
}
}

/**
* Verify that the raw data is a cache item created by this class.
*
* @param mixed $rawItem
*
* @return bool
*/
private function isItemCreatedHere($rawItem)
{
return is_array($rawItem) && isset($rawItem['value']) && isset($rawItem['tags']) && count($rawItem) === 2;
}
}

0 comments on commit 8ae3042

Please sign in to comment.