Completed
Pull Request — master (#917)
by Dmitry
08:52
created

ContainerSource::getTypes()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12
Metric Value
dl 17
loc 17
ccs 0
cts 14
cp 0
rs 9.4285
cc 3
eloc 9
nc 2
nop 1
crap 12
1
<?php
2
3
/**
4
 * This file is part of the FOSElasticaBundle project.
5
 *
6
 * (c) Tim Nagel <[email protected]>
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\Configuration\Source;
13
14
use FOS\ElasticaBundle\Configuration\IndexConfig;
15
use FOS\ElasticaBundle\Configuration\TypeConfig;
16
17
/**
18
 * Returns index and type configuration from the container.
19
 */
20 View Code Duplication
class ContainerSource implements SourceInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
{
22
    /**
23
     * The internal container representation of information.
24
     *
25
     * @var array
26
     */
27
    private $configArray;
28
29
    public function __construct(array $configArray)
30
    {
31
        $this->configArray = $configArray;
32
    }
33
34
    /**
35
     * Should return all configuration available from the data source.
36
     *
37
     * @return IndexConfig[]
38
     */
39
    public function getConfiguration()
40
    {
41
        $indexes = array();
42
        foreach ($this->configArray as $config) {
43
            $types = $this->getTypes($config);
44
            $index = new IndexConfig($config['name'], $types, array(
45
                'elasticSearchName' => $config['elasticsearch_name'],
46
                'settings' => $config['settings'],
47
                'useAlias' => $config['use_alias'],
48
            ));
49
50
            $indexes[$config['name']] = $index;
51
        }
52
53
        return $indexes;
54
    }
55
56
    /**
57
     * Builds TypeConfig objects for each type.
58
     *
59
     * @param array $config
60
     *
61
     * @return array
62
     */
63
    protected function getTypes($config)
64
    {
65
        $types = array();
66
67
        if (isset($config['types'])) {
68
            foreach ($config['types'] as $typeConfig) {
69
                $types[$typeConfig['name']] = new TypeConfig(
70
                    $typeConfig['name'],
71
                    $typeConfig['mapping'],
72
                    $typeConfig['config']
73
                );
74
                // TODO: handle prototypes..
75
            }
76
        }
77
78
        return $types;
79
    }
80
}
81