Skip to content

Commit

Permalink
Updated: Using container of endpoints in EndpointFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromdev committed Feb 27, 2017
1 parent 044982d commit a86b882
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
2 changes: 2 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Elasticsearch\ConnectionPool\StaticNoPingConnectionPool;
use Elasticsearch\Connections\ConnectionFactory as ElasticsearchConnectionFactory;
use Elasticsearch\Serializers\SmartSerializer;
use ElasticsearchModule\Container\Endpoints;
use ElasticsearchModule\Service\ClientFactory;
use ElasticsearchModule\Service\ConnectionFactory;
use ElasticsearchModule\Service\ConnectionPoolFactory;
Expand Down Expand Up @@ -65,6 +66,7 @@
],
'endpoint' => [
'default' => [
'container' => Endpoints::class,
'transport' => 'elasticsearch.transport.default',
'serializer' => SmartSerializer::class,
],
Expand Down
39 changes: 25 additions & 14 deletions src/Service/EndpointFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ElasticsearchModule\Service;

use Elasticsearch\Serializers\SerializerInterface;
use ElasticsearchModule\Container\EndpointsInterface;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\ServiceLocatorInterface;

Expand All @@ -20,23 +21,17 @@ protected function create(ServiceLocatorInterface $serviceLocator, $config)
$serializer = $this->getServiceOrClassObject($serviceLocator, $config['serializer']);

if (!$serializer instanceof SerializerInterface) {
throw new ServiceNotCreatedException(sprintf(
"The serializer must be instance of %s, %s given",
SerializerInterface::class,
is_object($serializer) ? get_class($serializer) : gettype($serializer)
));
throw $this->getInvalidTypeException('serializer', SerializerInterface::class, $serializer);
}
$transport = $serviceLocator->get($config['transport']);
$container = $config['container'];
$container = new $container($transport, $serializer);

return function($class) use ($transport, $serializer) {
$endpointClass = "\\Elasticsearch\\Endpoints\\$class";

if (in_array($class, ['Bulk', 'MSearch', 'MPercolate'])) {
return new $endpointClass($transport, $serializer);
} else {
return new $endpointClass($transport);
}
};
if (!$container instanceof EndpointsInterface) {
throw $this->getInvalidTypeException('container', EndpointsInterface::class, $container);
}

return $container;
}

/**
Expand All @@ -46,4 +41,20 @@ public function getServiceType()
{
return 'endpoint';
}

/**
* @param string $name
* @param string $className
* @param mixed $var
* @return ServiceNotCreatedException
*/
private function getInvalidTypeException($name, $className, $var)
{
return new ServiceNotCreatedException(sprintf(
"The %s must be instance of %s, %s given",
$name,
$className,
is_object($var) ? get_class($var) : gettype($var)
));
}
}
24 changes: 24 additions & 0 deletions tests/Service/EndpointFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace ElasticsearchModuleTest\Service;

use Elasticsearch\Endpoints\AbstractEndpoint;
use Elasticsearch\Serializers\SmartSerializer;
use ElasticsearchModule\Container\Endpoints;
use ElasticsearchModule\Service\EndpointFactory;
use ElasticsearchModuleTest\Traits\Tests\EndpointTrait;
use stdClass;
Expand Down Expand Up @@ -35,6 +37,7 @@ public function testThrowExceptionWhenFindAnInvalidSerializer()
'elasticsearch' => [
'endpoint' => [
'default' => [
'container' => Endpoints::class,
'serializer' => stdClass::class,
'transport' => 'elasticsearch.transport.default',
],
Expand All @@ -45,4 +48,25 @@ public function testThrowExceptionWhenFindAnInvalidSerializer()

$factory->createService($endpointContainer);
}

/**
* @expectedException \Zend\ServiceManager\Exception\ServiceNotCreatedException
*/
public function testThrowExceptionWhenAnInvalidEndpointContainer()
{
$endpointContainer = $this->getContainerWithEndpointDependencies([
'elasticsearch' => [
'endpoint' => [
'default' => [
'container' => stdClass::class,
'serializer' => SmartSerializer::class,
'transport' => 'elasticsearch.transport.default',
],
],
],
]);
$factory = new EndpointFactory('default');

$endpoint = $factory->createService($endpointContainer);
}
}

0 comments on commit a86b882

Please sign in to comment.