Completed
Pull Request — master (#1569)
by
unknown
14:06
created

PagerProviderRegistry   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 38
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getProviders() 0 6 1
A getProvider() 0 8 2
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\ElasticaBundle\Provider;
13
14
use Symfony\Component\DependencyInjection\ServiceLocator;
15
16
/**
17
 * References persistence providers for each index.
18
 */
19
class PagerProviderRegistry
20
{
21
    /** @var ServiceLocator */
22
    private $providers = [];
23
24
    public function __construct(ServiceLocator $providers)
25
    {
26
        $this->providers = $providers;
27
    }
28
29
    /**
30 9
     * Gets all registered providers.
31
     *
32 9
     * Providers will be indexed by "index" strings in the returned array.
33 9
     *
34
     * @return PagerProviderInterface[]
35
     */
36
    public function getProviders()
37
    {
38
        return \array_reduce(\array_keys($this->providers->getProvidedServices()), function ($carry, $index) {
39
            return $carry + [$index => $this->providers->get($index)];
40
        }, []);
41
    }
42 1
43
    /**
44 1
     * Gets the provider for an index.
45
     *
46 1
     * @throws \InvalidArgumentException if no provider was registered for the index and type
47 1
     */
48 1
    public function getProvider(string $index): ?PagerProviderInterface
49
    {
50
        if (!$this->providers->has($index)) {
51
            throw new \InvalidArgumentException(sprintf('No provider was registered for index "%s".', $index));
52 1
        }
53
54
        return $this->providers->get($index);
55
    }
56
}
57