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

PagerProviderRegistry::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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