Refactor IndexManager construction (preserves API)
Provide the default index name rather than the service itself. Additionally, inject references to each index by name instead of the definitions.
This commit is contained in:
parent
494b73a860
commit
d3a2335a55
|
@ -43,11 +43,11 @@ class FOQElasticaExtension extends Extension
|
|||
|
||||
$clientIdsByName = $this->loadClients($config['clients'], $container);
|
||||
$indexIdsByName = $this->loadIndexes($config['indexes'], $container, $clientIdsByName, $config['default_client']);
|
||||
$indexDefsByName = array_map(function($id) use ($container) {
|
||||
return $container->getDefinition($id);
|
||||
$indexRefsByName = array_map(function($id) {
|
||||
return new Reference($id);
|
||||
}, $indexIdsByName);
|
||||
|
||||
$this->loadIndexManager($indexDefsByName, $container->getDefinition($indexIdsByName[$config['default_index']]), $container);
|
||||
$this->loadIndexManager($indexRefsByName, $container);
|
||||
$this->loadReseter($this->indexConfigs, $container);
|
||||
|
||||
$container->setAlias('foq_elastica.client', sprintf('foq_elastica.client.%s', $config['default_client']));
|
||||
|
@ -371,12 +371,13 @@ class FOQElasticaExtension extends Extension
|
|||
/**
|
||||
* Loads the index manager
|
||||
*
|
||||
* @return null
|
||||
* @param array $indexRefsByName
|
||||
* @param ContainerBuilder $container
|
||||
**/
|
||||
protected function loadIndexManager(array $indexDefs, $defaultIndexId, ContainerBuilder $container)
|
||||
protected function loadIndexManager(array $indexRefsByName, ContainerBuilder $container)
|
||||
{
|
||||
$managerDef = $container->getDefinition('foq_elastica.index_manager');
|
||||
$managerDef->replaceArgument(0, $indexDefs);
|
||||
$managerDef->replaceArgument(0, $indexRefsByName);
|
||||
$managerDef->replaceArgument(1, new Reference('foq_elastica.index'));
|
||||
}
|
||||
|
||||
|
|
|
@ -2,17 +2,21 @@
|
|||
|
||||
namespace FOQ\ElasticaBundle;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class IndexManager
|
||||
{
|
||||
protected $indexes;
|
||||
protected $defaultIndex;
|
||||
protected $indexesByName;
|
||||
protected $defaultIndexName;
|
||||
|
||||
public function __construct(array $indexes, $defaultIndex)
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $indexesByName
|
||||
* @param string $defaultIndexName
|
||||
*/
|
||||
public function __construct(array $indexesByName, $defaultIndexName)
|
||||
{
|
||||
$this->indexes = $indexes;
|
||||
$this->defaultIndex = $defaultIndex;
|
||||
$this->indexesByName = $indexesByName;
|
||||
$this->defaultIndexName = $defaultIndexName;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -22,33 +26,36 @@ class IndexManager
|
|||
*/
|
||||
public function getAllIndexes()
|
||||
{
|
||||
return $this->indexes;
|
||||
return $this->indexesByName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an index by its name
|
||||
*
|
||||
* @param string $name Index to return, or the default index if null
|
||||
* @return Elastica_Index
|
||||
**/
|
||||
public function getIndex($name)
|
||||
* @throws InvalidArgumentException if no index exists for the given name
|
||||
*/
|
||||
public function getIndex($name = null)
|
||||
{
|
||||
if (!$name) {
|
||||
return $this->getDefaultIndex();
|
||||
}
|
||||
if (!isset($this->indexes[$name])) {
|
||||
throw new InvalidArgumentException(sprintf('The index "%s" does not exist', $name));
|
||||
if (null === $name) {
|
||||
$name = $this->defaultIndexName;
|
||||
}
|
||||
|
||||
return $this->indexes[$name];
|
||||
if (!isset($this->indexesByName[$name])) {
|
||||
throw new \InvalidArgumentException(sprintf('The index "%s" does not exist', $name));
|
||||
}
|
||||
|
||||
return $this->indexesByName[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default index
|
||||
*
|
||||
* @return Elastica_Index
|
||||
**/
|
||||
*/
|
||||
public function getDefaultIndex()
|
||||
{
|
||||
return $this->defaultIndex;
|
||||
return $this->getIndex($this->defaultIndexName);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,37 +6,42 @@ use FOQ\ElasticaBundle\IndexManager;
|
|||
|
||||
class IndexManagerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var FOQ\ElasticaBundle\Tests\IndexManager
|
||||
*/
|
||||
private $indexManager = null;
|
||||
private $defaultIndexName;
|
||||
private $indexesByName;
|
||||
private $indexManager;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->indexManager = new IndexManager(array('index1' => 'test1', 'index2' => 'test2'), 'defaultIndex');
|
||||
$this->defaultIndexName = 'index2';
|
||||
$this->indexesByName = array(
|
||||
'index1' => 'test1',
|
||||
'index2' => 'test2',
|
||||
);
|
||||
$this->indexManager = new IndexManager($this->indexesByName, $this->defaultIndexName);
|
||||
}
|
||||
|
||||
public function testThatWeCanGetAllIndexes()
|
||||
public function testGetAllIndexes()
|
||||
{
|
||||
$this->assertEquals(array('index1' => 'test1', 'index2' => 'test2'), $this->indexManager->getAllIndexes());
|
||||
$this->assertEquals($this->indexesByName, $this->indexManager->getAllIndexes());
|
||||
}
|
||||
|
||||
public function testGetIndex()
|
||||
{
|
||||
$this->assertEquals($this->indexesByName['index1'], $this->indexManager->getIndex('index1'));
|
||||
$this->assertEquals($this->indexesByName['index2'], $this->indexManager->getIndex('index2'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException InvalidArgumentException
|
||||
*/
|
||||
public function testThatWeCannotGetIndexWhichWasNotSet()
|
||||
public function testGetIndexShouldThrowExceptionForInvalidName()
|
||||
{
|
||||
$this->indexManager->getIndex('index8');
|
||||
$this->indexManager->getIndex('index3');
|
||||
}
|
||||
|
||||
public function testThatWeCanGetDefaultIndex()
|
||||
|
||||
public function testGetDefaultIndex()
|
||||
{
|
||||
$this->assertEquals('defaultIndex', $this->indexManager->getIndex(false));
|
||||
$this->assertEquals('defaultIndex', $this->indexManager->getDefaultIndex());
|
||||
}
|
||||
|
||||
public function testThatWeCanGetIndex()
|
||||
{
|
||||
$this->assertEquals('test2', $this->indexManager->getIndex('index2'));
|
||||
$this->assertEquals('test2', $this->indexManager->getIndex());
|
||||
$this->assertEquals('test2', $this->indexManager->getDefaultIndex());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue