|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ElasticsearchModule\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Elasticsearch\Serializers\SerializerInterface; |
|
6
|
|
|
use ElasticsearchModule\Container\EndpointsInterface; |
|
7
|
|
|
use Zend\ServiceManager\Exception\ServiceNotCreatedException; |
|
8
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Pedro Alves <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class EndpointFactory extends AbstractFactory |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* {@inheritDoc} |
|
18
|
|
|
*/ |
|
19
|
6 |
|
protected function create(ServiceLocatorInterface $serviceLocator, $config) |
|
20
|
|
|
{ |
|
21
|
6 |
|
$serializer = $this->getServiceOrClassObject($serviceLocator, $config['serializer']); |
|
22
|
|
|
|
|
23
|
6 |
|
if (!$serializer instanceof SerializerInterface) { |
|
24
|
1 |
|
throw $this->getInvalidTypeException('serializer', SerializerInterface::class, $serializer); |
|
25
|
|
|
} |
|
26
|
5 |
|
$transport = $serviceLocator->get($config['transport']); |
|
27
|
5 |
|
$container = $config['container']; |
|
28
|
5 |
|
$container = new $container($transport, $serializer); |
|
29
|
|
|
|
|
30
|
5 |
|
if (!$container instanceof EndpointsInterface) { |
|
31
|
1 |
|
throw $this->getInvalidTypeException('container', EndpointsInterface::class, $container); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
4 |
|
return $container; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritDoc} |
|
39
|
|
|
*/ |
|
40
|
6 |
|
public function getServiceType() |
|
41
|
|
|
{ |
|
42
|
6 |
|
return 'endpoint'; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $name |
|
47
|
|
|
* @param string $className |
|
48
|
|
|
* @param mixed $var |
|
49
|
|
|
* @return ServiceNotCreatedException |
|
50
|
|
|
*/ |
|
51
|
2 |
|
private function getInvalidTypeException($name, $className, $var) |
|
52
|
|
|
{ |
|
53
|
2 |
|
return new ServiceNotCreatedException(sprintf( |
|
54
|
2 |
|
"The %s must be instance of %s, %s given", |
|
55
|
2 |
|
$name, |
|
56
|
2 |
|
$className, |
|
57
|
2 |
|
is_object($var) ? get_class($var) : gettype($var) |
|
58
|
2 |
|
)); |
|
59
|
|
|
} |
|
60
|
|
|
} |