Skip to content

Commit 82cbb60

Browse files
committed
Added abstract adapter factory
1 parent 848b4d2 commit 82cbb60

File tree

3 files changed

+140
-22
lines changed

3 files changed

+140
-22
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\adapter-bundle package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\AdapterBundle\Factory;
13+
14+
use Psr\Cache\CacheItemPoolInterface;
15+
use Symfony\Component\OptionsResolver\OptionsResolver;
16+
17+
/**
18+
* @author Tobias Nyholm <[email protected]>
19+
*/
20+
abstract class AbstractAdapterFactory implements AdapterFactoryInterface
21+
{
22+
/**
23+
* @param OptionsResolver $resolver
24+
*/
25+
abstract protected function configureOptionResolver(OptionsResolver $resolver);
26+
27+
/**
28+
* @param array $config
29+
*
30+
* @return CacheItemPoolInterface
31+
*/
32+
abstract protected function getAdapter(array $config);
33+
34+
/**
35+
* Get the class name that is required for this adapter. This should more often than not be the cache pool.
36+
*
37+
* @return string
38+
*/
39+
abstract protected function getRequiredClass();
40+
41+
/**
42+
* Get the name of the package where require class lives.
43+
*
44+
* @return string
45+
*/
46+
abstract protected function getPackageName();
47+
48+
/**
49+
* Get the name of the adapter.
50+
*
51+
* @return string
52+
*/
53+
abstract protected function getName();
54+
55+
/**
56+
* {@inheritdoc}
57+
*/
58+
public function createAdapter(array $options = [])
59+
{
60+
$this->verifyDependencies();
61+
62+
$resolver = new OptionsResolver();
63+
$this->configureOptionResolver($resolver);
64+
$config = $resolver->resolve($options);
65+
66+
return $this->getAdapter($config);
67+
}
68+
69+
/**
70+
* Make sure that we have the required class and throws and exception if we dont.
71+
*
72+
* @throws \LogicException
73+
*/
74+
protected function verifyDependencies()
75+
{
76+
if (!class_exists($this->getRequiredClass())) {
77+
throw new \LogicException(
78+
sprintf(
79+
'You must install the "%s" package to use the "%s" provider.',
80+
$this->getPackageName(),
81+
$this->getName()
82+
)
83+
);
84+
}
85+
}
86+
}

src/Factory/DoctrineRedisFactory.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@
1818
/**
1919
* @author Tobias Nyholm <[email protected]>
2020
*/
21-
class DoctrineRedisFactory implements AdapterFactoryInterface
21+
class DoctrineRedisFactory extends AbstractAdapterFactory
2222
{
2323
/**
2424
* {@inheritdoc}
2525
*/
26-
public function createAdapter(array $options = [])
26+
public function getAdapter(array $config)
2727
{
2828
if (!class_exists('Cache\Adapter\Doctrine\DoctrineCachePool')) {
2929
throw new \LogicException('You must install the "cache/doctrine-adapter" package to use the "doctrine_redis" provider.');
3030
}
3131

32-
$config = $this->configureOptions($options);
3332
$redis = new \Redis();
3433
$redis->connect($config['host'], $config['port']);
3534

@@ -40,21 +39,40 @@ public function createAdapter(array $options = [])
4039
}
4140

4241
/**
43-
* @param array $options
44-
*
45-
* @return array
42+
* {@inheritdoc}
4643
*/
47-
private function configureOptions(array $options)
44+
protected function configureOptionResolver(OptionsResolver $resolver)
4845
{
49-
$resolver = new OptionsResolver();
5046
$resolver->setDefaults([
5147
'host' => '127.0.0.1',
5248
'port' => '6379',
5349
]);
5450

5551
$resolver->setAllowedTypes('host', ['string']);
5652
$resolver->setAllowedTypes('port', ['string', 'int']);
53+
}
54+
55+
/**
56+
* {@inheritdoc}
57+
*/
58+
protected function getRequiredClass()
59+
{
60+
return 'Cache\Adapter\Doctrine\DoctrineCachePool';
61+
}
62+
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
protected function getPackageName()
67+
{
68+
return 'cache/doctrine-adapter';
69+
}
5770

58-
return $resolver->resolve($options);
71+
/**
72+
* {@inheritdoc}
73+
*/
74+
protected function getName()
75+
{
76+
return 'doctrine_redis';
5977
}
6078
}

src/Factory/RedisFactory.php

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,23 @@
1818
/**
1919
* @author Tobias Nyholm <[email protected]>
2020
*/
21-
class RedisFactory implements AdapterFactoryInterface
21+
class RedisFactory extends AbstractAdapterFactory
2222
{
2323
/**
2424
* {@inheritdoc}
2525
*/
26-
public function createAdapter(array $options = [])
26+
public function getAdapter(array $config)
2727
{
28-
if (!class_exists('Cache\Adapter\Redis\RedisCachePool')) {
29-
throw new \LogicException('You must install the "cache/redis-adapter" package to use the "redis" provider.');
30-
}
31-
32-
$config = $this->configureOptions($options);
3328
$client = new Client(sprintf('%s://%s:%s', $config['protocol'], $config['host'], $config['port']));
3429

3530
return new RedisCachePool($client);
3631
}
3732

3833
/**
39-
* @param array $options
40-
*
41-
* @return array
34+
* {@inheritdoc}
4235
*/
43-
private function configureOptions(array $options)
36+
protected function configureOptionResolver(OptionsResolver $resolver)
4437
{
45-
$resolver = new OptionsResolver();
4638
$resolver->setDefaults([
4739
'host' => '127.0.0.1',
4840
'port' => '6379',
@@ -52,7 +44,29 @@ private function configureOptions(array $options)
5244
$resolver->setAllowedTypes('host', ['string']);
5345
$resolver->setAllowedTypes('port', ['string', 'int']);
5446
$resolver->setAllowedTypes('protocol', ['string']);
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
protected function getRequiredClass()
53+
{
54+
return 'Cache\Adapter\Redis\RedisCachePool';
55+
}
5556

56-
return $resolver->resolve($options);
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
protected function getPackageName()
61+
{
62+
return 'cache/redis-adapter';
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
protected function getName()
69+
{
70+
return 'redis';
5771
}
5872
}

0 commit comments

Comments
 (0)