Completed
Pull Request — master (#1340)
by Maksim
06:56
created

PagerPersisterRegistry::getPagerPersister()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
ccs 12
cts 12
cp 1
rs 9.3142
cc 3
eloc 12
nc 3
nop 1
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\Persister;
13
14
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
15
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
16
17
final class PagerPersisterRegistry implements ContainerAwareInterface
18
{
19
    use ContainerAwareTrait;
20
21
    /** 
22
     * @var string[]
23
     */
24
    private $nameToServiceIdMap = [];
25
26
    /**
27
     * @param string[] $nameToServiceIdMap
28
     */
29 5
    public function __construct(array $nameToServiceIdMap)
30
    {
31 5
        $this->nameToServiceIdMap = $nameToServiceIdMap;
32 5
    }
33
34
    /**
35
     * @param string $name
36
     *
37
     * @throws \InvalidArgumentException if no pager persister was registered for the given name
38
     *
39
     * @return PagerPersisterInterface
40
     */
41 4
    public function getPagerPersister($name)
42
    {
43 4
        if (!isset($this->nameToServiceIdMap[$name])) {
44 1
            throw new \InvalidArgumentException(sprintf('No pager persister was registered for the give name "%s".', $name));
45
        }
46
47 3
        $serviceId = $this->nameToServiceIdMap[$name];
48
49 3
        $pagerPersister = $this->container->get($serviceId);
50
51 2
        if (!$pagerPersister instanceof PagerPersisterInterface) {
52 1
            throw new \LogicException(sprintf(
53 1
                'The pager provider service "%s" must implement "%s" interface but it is an instance of "%s" class.',
54 1
                $serviceId,
55 1
                PagerPersisterInterface::class,
56 1
                get_class($pagerPersister)
57
            ));
58
        }
59
60 1
        return $pagerPersister;
61
    }
62
}
63