ElasticsearchServiceProvider   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 42
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A register() 0 12 2
A boot() 0 3 1
1
<?php
2
3
namespace xmarcos\Silex;
4
5
use Silex\Application;
6
use Elasticsearch\Client;
7
use InvalidArgumentException;
8
use Silex\ServiceProviderInterface;
9
10
class ElasticsearchServiceProvider implements ServiceProviderInterface
11
{
12
    protected $prefix;
13
14
    /**
15
     * @param string $prefix Prefix name used to register the service in Silex.
16
     */
17
    public function __construct($prefix = 'elasticsearch')
18
    {
19
        if (empty($prefix) || false === is_string($prefix)) {
20
            throw new InvalidArgumentException(
21
                sprintf('$prefix must be a non-empty string, "%s" given', gettype($prefix))
22
            );
23
        }
24
25
        $this->prefix = $prefix;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function register(Application $app)
32
    {
33
        $prefix           = $this->prefix;
34
        $params_key       = sprintf('%s.params', $prefix);
35
        $app[$params_key] = isset($app[$params_key]) ? $app[$params_key] : [];
36
37
        $app[$prefix] = $app->share(
38
            function (Application $app) use ($params_key) {
39
                return new Client($app[$params_key]);
40
            }
41
        );
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     * @codeCoverageIgnore
47
     */
48
    public function boot(Application $app)
49
    {
50
    }
51
}
52