|
13 | 13 |
|
14 | 14 | use Cache\Adapter\Doctrine\DoctrineCachePool; |
15 | 15 | use Doctrine\Common\Cache\RedisCache; |
| 16 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
16 | 17 |
|
| 18 | +/** |
| 19 | + * @author Tobias Nyholm <[email protected]> |
| 20 | + */ |
17 | 21 | class DoctrineRedisFactory implements AdapterFactoryInterface |
18 | 22 | { |
| 23 | + /** |
| 24 | + * {@inheritdoc} |
| 25 | + */ |
19 | 26 | public function createAdapter(array $options = []) |
20 | 27 | { |
21 | 28 | if (!class_exists('Cache\Adapter\Doctrine\DoctrineCachePool')) { |
22 | 29 | throw new \LogicException('You must install the "cache/doctrine-adapter" package to use the "doctrine_redis" provider.'); |
23 | 30 | } |
24 | 31 |
|
25 | | - // TODO validate the options with symfony options resolver |
26 | | - // TODO get ip, port and protocol from options. |
27 | | - |
28 | | - $redis = new \Redis(); |
29 | | - $redis->connect('127.0.0.1', '6379'); |
| 32 | + $config = $this->configureOptions($options); |
| 33 | + $redis = new \Redis(); |
| 34 | + $redis->connect($config['host'], $config['port']); |
30 | 35 |
|
31 | 36 | $client = new RedisCache(); |
32 | 37 | $client->setRedis($redis); |
33 | 38 |
|
34 | 39 | return new DoctrineCachePool($client); |
35 | 40 | } |
| 41 | + |
| 42 | + /** |
| 43 | + * @param array $options |
| 44 | + * |
| 45 | + * @return array |
| 46 | + */ |
| 47 | + private function configureOptions(array $options) |
| 48 | + { |
| 49 | + $resolver = new OptionsResolver(); |
| 50 | + $resolver->setDefaults([ |
| 51 | + 'host' => '127.0.0.1', |
| 52 | + 'port' => '6379', |
| 53 | + ]); |
| 54 | + |
| 55 | + $resolver->setAllowedTypes('host', ['string']); |
| 56 | + $resolver->setAllowedTypes('port', ['string', 'int']); |
| 57 | + |
| 58 | + return $resolver->resolve($options); |
| 59 | + } |
36 | 60 | } |
0 commit comments