Completed
Pull Request — master (#1240)
by Maksim
03:38
created

PersisterRegistry   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 24.24 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addPersister() 0 8 2
A getPersister() 8 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
class PersisterRegistry implements ContainerAwareInterface
18
{
19
    use ContainerAwareTrait;
20
21
    /** @var array */
22
    private $persisters = [];
23
24
    /**
25
     * @param string $index
26
     * @param string $type
27
     * @param string $persisterId
28
     */
29
    public function addPersister($index, $type, $persisterId)
30
    {
31
        if (!isset($this->persisters[$index])) {
32
            $this->persisters[$index] = [];
33
        }
34
35
        $this->persisters[$index][$type] = $persisterId;
36
    }
37
38
    /**
39
     * @return ObjectPersisterInterface
40
     */
41 View Code Duplication
    public function getPersister($index, $type)
42
    {
43
        if (!isset($this->persisters[$index][$type])) {
44
            throw new \InvalidArgumentException(sprintf('No persister was registered for index "%s" and type "%s".', $index, $type));
45
        }
46
47
        return $this->container->get($this->persisters[$index][$type]);
48
    }
49
}
50