Completed
Push — master ( 4e4cd8...063296 )
by Maksim
16s
created

PagerProviderRegistry::getAllProviders()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
crap 12
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
    public function __construct(array $providers)
31
    {
32
        $this->providers = $providers;
33
    }
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 View Code Duplication
    public function getAllProviders()
43
    {
44
        $providers = [];
45
46
        foreach ($this->providers as $index => $indexProviders) {
47
            foreach ($indexProviders as $type => $providerId) {
48
                $providers[sprintf('%s/%s', $index, $type)] = $this->container->get($providerId);
49
            }
50
        }
51
52
        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 View Code Duplication
    public function getIndexProviders($index)
67
    {
68
        if (!isset($this->providers[$index])) {
69
            throw new \InvalidArgumentException(sprintf('No providers were registered for index "%s".', $index));
70
        }
71
72
        $providers = [];
73
        foreach ($this->providers[$index] as $type => $providerId) {
74
            $providers[$type] = $this->getProvider($index, $type);
75
        }
76
77
        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 View Code Duplication
    public function getProvider($index, $type)
91
    {
92
        if (!isset($this->providers[$index][$type])) {
93
            throw new \InvalidArgumentException(sprintf('No provider was registered for index "%s" and type "%s".', $index, $type));
94
        }
95
96
        return $this->container->get($this->providers[$index][$type]);
97
    }
98
}
99