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

PagerPersisterRegistry   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 46
ccs 15
cts 15
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getPagerPersister() 0 21 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