Skip to content

Commit 8ac1643

Browse files
committed
Added option resolver
1 parent ee76b41 commit 8ac1643

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

src/Factory/AdapterFactoryInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,17 @@
1111

1212
namespace Cache\AdapterBundle\Factory;
1313

14+
use Psr\Cache\CacheItemPoolInterface;
15+
16+
/**
17+
* @author Tobias Nyholm <[email protected]>
18+
*/
1419
interface AdapterFactoryInterface
1520
{
21+
/**
22+
* @param array $options
23+
*
24+
* @return CacheItemPoolInterface
25+
*/
1626
public function createAdapter(array $options = []);
1727
}

src/Factory/DoctrineRedisFactory.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,49 @@
1313

1414
use Cache\Adapter\Doctrine\DoctrineCachePool;
1515
use Doctrine\Common\Cache\RedisCache;
16+
use Symfony\Component\OptionsResolver\OptionsResolver;
1617

18+
/**
19+
* @author Tobias Nyholm <[email protected]>
20+
*/
1721
class DoctrineRedisFactory implements AdapterFactoryInterface
1822
{
23+
/**
24+
* {@inheritdoc}
25+
*/
1926
public function createAdapter(array $options = [])
2027
{
2128
if (!class_exists('Cache\Adapter\Doctrine\DoctrineCachePool')) {
2229
throw new \LogicException('You must install the "cache/doctrine-adapter" package to use the "doctrine_redis" provider.');
2330
}
2431

25-
// TODO validate the options with symfony options resolver
26-
// TODO get ip, port and protocol from options.
27-
32+
$config = $this->configureOptions($options);
2833
$redis = new \Redis();
29-
$redis->connect('127.0.0.1', '6379');
34+
$redis->connect($config['host'], $config['port']);
3035

3136
$client = new RedisCache();
3237
$client->setRedis($redis);
3338

3439
return new DoctrineCachePool($client);
3540
}
41+
42+
43+
/**
44+
* @param array $options
45+
*
46+
* @return array
47+
*/
48+
private function configureOptions(array $options)
49+
{
50+
$resolver = new OptionsResolver();
51+
$resolver->setDefaults([
52+
'host' => '127.0.0.1',
53+
'port' => '6379',
54+
]);
55+
56+
$resolver->setAllowedTypes('host', ['string']);
57+
$resolver->setAllowedTypes('port', ['string', 'int']);
58+
59+
return $resolver->resolve($options);
60+
}
3661
}

src/Factory/RedisFactory.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,46 @@
1313

1414
use Cache\Adapter\Redis\RedisCachePool;
1515
use Predis\Client;
16+
use Symfony\Component\OptionsResolver\OptionsResolver;
1617

18+
/**
19+
* @author Tobias Nyholm <[email protected]>
20+
*/
1721
class RedisFactory implements AdapterFactoryInterface
1822
{
23+
/**
24+
* {@inheritdoc}
25+
*/
1926
public function createAdapter(array $options = [])
2027
{
2128
if (!class_exists('Cache\Adapter\Redis\RedisCachePool')) {
2229
throw new \LogicException('You must install the "cache/redis-adapter" package to use the "redis" provider.');
2330
}
2431

25-
// TOOD validate the options with symfony options resolver
26-
// TODO get ip, port and protocol from options.
27-
$client = new Client('tcp:/127.0.0.1:6379');
32+
$config = $this->configureOptions($options);
33+
$client = new Client(sprintf('%s://%s:%s', $config['protocol'], $config['host'], $config['port']));
2834

2935
return new RedisCachePool($client);
3036
}
37+
38+
/**
39+
* @param array $options
40+
*
41+
* @return array
42+
*/
43+
private function configureOptions(array $options)
44+
{
45+
$resolver = new OptionsResolver();
46+
$resolver->setDefaults([
47+
'host' => '127.0.0.1',
48+
'port' => '6379',
49+
'protocol' => 'tcp',
50+
]);
51+
52+
$resolver->setAllowedTypes('host', ['string']);
53+
$resolver->setAllowedTypes('port', ['string', 'int']);
54+
$resolver->setAllowedTypes('protocol', ['string']);
55+
56+
return $resolver->resolve($options);
57+
}
3158
}

0 commit comments

Comments
 (0)