Skip to content

Commit

Permalink
Fixed CacheItem implementation and marked internal.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Jun 15, 2016
1 parent c1fcbdf commit 9951cb5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/Porter/Cache/CacheItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

use Psr\Cache\CacheItemInterface;

/**
* @internal Only this library may create instances of this class.
*/
final class CacheItem implements CacheItemInterface
{
private $key;
Expand All @@ -11,7 +14,7 @@ final class CacheItem implements CacheItemInterface

private $hit;

public function __construct($key, $value, $hit = false)
private function __construct($key, $value, $hit)
{
$this->key = "$key";
$this->value = $value;
Expand All @@ -35,7 +38,9 @@ public function isHit()

public function set($value)
{
throw new CacheOperationProhibitedException('Cannot directly modify an item\'s value.');
$this->value = $value;

return $this;
}

public function expiresAt($expiration)
Expand Down
10 changes: 9 additions & 1 deletion src/Porter/Cache/MemoryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ class MemoryCache extends \ArrayObject implements CacheItemPoolInterface
{
public function getItem($key)
{
return new CacheItem($key, $this->hasItem($key) ? $this[$key] : null, $this->hasItem($key));
return call_user_func(
\Closure::bind(
function () use ($key) {
return new self($key, $this->hasItem($key) ? $this[$key] : null, $this->hasItem($key));
},
$this,
CacheItem::class
)
);
}

public function getItems(array $keys = [])
Expand Down
3 changes: 1 addition & 2 deletions src/Porter/Connector/CachingConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use Psr\Cache\CacheItemPoolInterface;
use ScriptFUSION\Porter\Cache\CacheEnabler;
use ScriptFUSION\Porter\Cache\CacheItem;
use ScriptFUSION\Porter\Cache\MemoryCache;
use ScriptFUSION\Porter\Options\EncapsulatedOptions;

Expand Down Expand Up @@ -34,7 +33,7 @@ public function fetch($source, EncapsulatedOptions $options = null)

$data = $this->fetchFreshData($source, $options);

isset($hash) && $this->cache->save(new CacheItem($hash, $data));
isset($hash) && $this->cache->save($this->cache->getItem($hash)->set($data));

return $data;
}
Expand Down
4 changes: 2 additions & 2 deletions test/Unit/Porter/Cache/MemoryCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ public function testDeleteInvalidItem()

public function testSave()
{
$this->cache->save(new CacheItem('bar', 'baz'));
$this->cache->save($this->cache->getItem('bar')->set('baz'));

self::assertSame('baz', $this->cache->getItem('bar')->get());
}

public function testSaveDeferred()
{
$this->cache->saveDeferred(new CacheItem('bar', 'baz'));
$this->cache->save($this->cache->getItem('bar')->set('baz'));

self::assertSame('baz', $this->cache->getItem('bar')->get());
}
Expand Down

0 comments on commit 9951cb5

Please sign in to comment.