Skip to content

Commit 55d9897

Browse files
committed
Add CacheFactory
1 parent 3275610 commit 55d9897

2 files changed

Lines changed: 126 additions & 0 deletions

File tree

src/CacheFactory.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace TraderInteractive\Api;
4+
5+
use MongoDB;
6+
use Psr\SimpleCache\CacheInterface;
7+
use SubjectivePHP\Psr\SimpleCache\InMemoryCache;
8+
use SubjectivePHP\Psr\SimpleCache\MongoCache;
9+
use SubjectivePHP\Psr\SimpleCache\NullCache;
10+
11+
final class CacheFactory
12+
{
13+
/**
14+
* @param string $name The name of the cache object to make.
15+
* @param array $config Options to use when constructing the cache.
16+
*
17+
* @return CacheInterface
18+
*/
19+
public static function make(string $name, array $config) : CacheInterface
20+
{
21+
if ($name === MongoCache::class) {
22+
return self::getMongoCache($config);
23+
}
24+
25+
if ($name === InMemoryCache::class) {
26+
return new InMemoryCache();
27+
}
28+
29+
if ($name === NullCache::class) {
30+
return new NullCache();
31+
}
32+
33+
throw new \RuntimeException("Cannot create cache instance of '{$name}'");
34+
}
35+
36+
private static function getMongoCache(array $config) : MongoCache
37+
{
38+
return new MongoCache(self::getMongoCollectionFromConfig($config), new ResponseSerializer());
39+
}
40+
41+
private static function getMongoCollectionFromConfig(array $config) : MongoDB\Collection
42+
{
43+
$collection = $config['collection'];
44+
if ($collection instanceof MongoDB\Collection) {
45+
return $collection;
46+
}
47+
48+
$uri = $config['uri'];
49+
$database = $config['database'];
50+
return (new MongoDB\Client($uri))->selectDatabase($database)->selectCollection($collection);
51+
}
52+
}

tests/CacheFactoryTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace TraderInteractive\Api;
4+
5+
use MongoDB;
6+
use PHPUnit\Framework\TestCase;
7+
use SubjectivePHP\Psr\SimpleCache\InMemoryCache;
8+
use SubjectivePHP\Psr\SimpleCache\MongoCache;
9+
use SubjectivePHP\Psr\SimpleCache\NullCache;
10+
use SubjectivePHP\Psr\SimpleCache\RedisCache;
11+
12+
/**
13+
* @coversDefaultClass \TraderInteractive\Api\CacheFactory
14+
* @covers ::<private>
15+
*/
16+
final class CacheFactoryTest extends TestCase
17+
{
18+
/**
19+
* @param string $name The name of the cache to create.
20+
* @param array $config Config data to pass to the factory.
21+
*
22+
* @test
23+
* @covers ::make
24+
* @dataProvider provideMakeData
25+
*/
26+
public function makeCache(string $name, array $config)
27+
{
28+
$this->assertInstanceOf($name, CacheFactory::make($name, $config));
29+
}
30+
31+
/**
32+
* @return array
33+
*/
34+
public function provideMakeData() : array
35+
{
36+
return [
37+
'null cache' => [
38+
'name' => NullCache::class,
39+
'config' => [],
40+
],
41+
'in-memory cache' => [
42+
'name' => InMemoryCache::class,
43+
'config' => [],
44+
],
45+
'mongo cache with collection' => [
46+
'name' => MongoCache::class,
47+
'config' => [
48+
'collection' => $this->getMockBuilder(\MongoDB\Collection::class)
49+
->disableOriginalConstructor()
50+
->getMock(),
51+
],
52+
],
53+
'mongo cache' => [
54+
'name' => MongoCache::class,
55+
'config' => [
56+
'uri' => 'mongodb://localhost:27017',
57+
'database' => 'testing',
58+
'collection' => 'cache',
59+
],
60+
],
61+
];
62+
}
63+
64+
/**
65+
* @test
66+
* @covers ::make
67+
* @expectedException \RuntimeException
68+
* @expectedExceptionMessage Cannot create cache instance of 'Invalid'
69+
*/
70+
public function cannotMakeUnsupportedCacheInstance()
71+
{
72+
CacheFactory::make('Invalid', []);
73+
}
74+
}

0 commit comments

Comments
 (0)