Completed
Pull Request — master (#1168)
by Alessandro
06:13
created

ProviderRegistry::setContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace FOS\ElasticaBundle\Provider;
4
5
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
6
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
7
use Symfony\Component\DependencyInjection\ContainerInterface;
8
9
/**
10
 * References persistence providers for each index and type.
11
 */
12
class ProviderRegistry implements ContainerAwareInterface
13
{
14
    use ContainerAwareTrait;
15
    
16
    /** @var array */
17
    private $providers = array();
18
19
    /**
20
     * Registers a provider for the specified index and type.
21
     *
22
     * @param string $index
23
     * @param string $type
24
     * @param string $providerId
25
     */
26 5
    public function addProvider($index, $type, $providerId)
27
    {
28 5
        if (!isset($this->providers[$index])) {
29 5
            $this->providers[$index] = array();
30
        }
31
32 5
        $this->providers[$index][$type] = $providerId;
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 array of ProviderInterface instances
41
     */
42 1
    public function getAllProviders()
43
    {
44 1
        $providers = array();
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 ProviderInterface[]
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 = array();
73
74 1
        foreach ($this->providers[$index] as $type => $providerId) {
75 1
            $providers[$type] = $this->container->get($providerId);
76
        }
77
78 1
        return $providers;
79
    }
80
81
    /**
82
     * Gets the provider for an index and type.
83
     *
84
     * @param string $index
85
     * @param string $type
86
     *
87
     * @return ProviderInterface
88
     *
89
     * @throws \InvalidArgumentException if no provider was registered for the index and type
90
     */
91 2
    public function getProvider($index, $type)
92
    {
93 2
        if (!isset($this->providers[$index][$type])) {
94 1
            throw new \InvalidArgumentException(sprintf('No provider was registered for index "%s" and type "%s".', $index, $type));
95
        }
96
97 1
        return $this->container->get($this->providers[$index][$type]);
98
    }
99
}
100