Completed
Push — master ( c7595f...4b08b2 )
by Karel
05:07
created

PagerProviderRegistry::getAllProviders()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3
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\ContainerAwareInterface;
15
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
16
17
/**
18
 * References persistence providers for each index and type.
19
 */
20
class PagerProviderRegistry implements ContainerAwareInterface
21
{
22
    use ContainerAwareTrait;
23
24
    /** @var array */
25
    private $providers = [];
26
27
    /**
28
     * @param array $providers
29
     */
30 5
    public function __construct(array $providers)
31
    {
32 5
        $this->providers = $providers;
33 5
    }
34
35
    /**
36
     * Gets all registered providers.
37
     *
38
     * Providers will be indexed by "index/type" strings in the returned array.
39
     *
40
     * @return PagerProviderInterface[]
41
     */
42 1
    public function getAllProviders()
43
    {
44 1
        $providers = [];
45
46 1
        foreach ($this->providers as $index => $indexProviders) {
47 1
            foreach ($indexProviders as $type => $providerId) {
48 1
                $providers[sprintf('%s/%s', $index, $type)] = $this->container->get($providerId);
49
            }
50
        }
51
52 1
        return $providers;
53
    }
54
55
    /**
56
     * Gets all providers for an index.
57
     *
58
     * Providers will be indexed by "type" strings in the returned array.
59
     *
60
     * @param string $index
61
     *
62
     * @return PagerProviderInterface[]|
63
     *
64
     * @throws \InvalidArgumentException if no providers were registered for the index
65
     */
66 2
    public function getIndexProviders($index)
67
    {
68 2
        if (!isset($this->providers[$index])) {
69 1
            throw new \InvalidArgumentException(sprintf('No providers were registered for index "%s".', $index));
70
        }
71
72 1
        $providers = [];
73 1
        foreach ($this->providers[$index] as $type => $providerId) {
74 1
            $providers[$type] = $this->getProvider($index, $type);
75
        }
76
77 1
        return $providers;
78
    }
79
80
    /**
81
     * Gets the provider for an index and type.
82
     *
83
     * @param string $index
84
     * @param string $type
85
     *
86
     * @return PagerProviderInterface
87
     *
88
     * @throws \InvalidArgumentException if no provider was registered for the index and type
89
     */
90 3 View Code Duplication
    public function getProvider($index, $type)
91
    {
92 3
        if (!isset($this->providers[$index][$type])) {
93 1
            throw new \InvalidArgumentException(sprintf('No provider was registered for index "%s" and type "%s".', $index, $type));
94
        }
95
96 2
        return $this->container->get($this->providers[$index][$type]);
97
    }
98
}
99