PagerProviderRegistry::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <https://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 7
    public function __construct(ServiceLocator $providers)
25
    {
26 7
        $this->providers = $providers;
27 7
    }
28
29
    /**
30
     * Gets all registered providers.
31
     *
32
     * Providers will be indexed by "index" strings in the returned array.
33
     *
34
     * @return PagerProviderInterface[]
35
     */
36 1
    public function getProviders()
37
    {
38 1
        return \array_reduce(\array_keys($this->providers->getProvidedServices()), function ($carry, $index) {
39 1
            return $carry + [$index => $this->providers->get($index)];
40 1
        }, []);
41
    }
42
43
    /**
44
     * Gets the provider for an index.
45
     *
46
     * @throws \InvalidArgumentException if no provider was registered for the index and type
47
     */
48 2
    public function getProvider(string $index): PagerProviderInterface
49
    {
50 2
        if (!$this->providers->has($index)) {
51 1
            throw new \InvalidArgumentException(\sprintf('No provider was registered for index "%s".', $index));
52
        }
53
54 1
        return $this->providers->get($index);
55
    }
56
}
57