Completed
Pull Request — master (#1326)
by
unknown
03:42
created

ContainerSource::getTypes()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 2
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
/**
13
 * This file is part of the FOSElasticaBundle project.
14
 *
15
 * (c) Tim Nagel <[email protected]>
16
 *
17
 * For the full copyright and license information, please view the LICENSE
18
 * file that was distributed with this source code.
19
 */
20
21
namespace FOS\ElasticaBundle\Configuration\Source;
22
23
use FOS\ElasticaBundle\Configuration\IndexConfig;
24
use FOS\ElasticaBundle\Configuration\TypeConfig;
25
26
/**
27
 * Returns index and type configuration from the container.
28
 */
29
class ContainerSource implements SourceInterface
30
{
31
    /**
32
     * The internal container representation of information.
33
     *
34
     * @var array
35
     */
36
    private $configArray;
37
38
    /**
39
     * @param array $configArray
40
     */
41 6
    public function __construct(array $configArray)
42
    {
43 6
        $this->configArray = $configArray;
44 6
    }
45
46
    /**
47
     * Should return all configuration available from the data source.
48
     *
49
     * @return IndexConfig[]
50
     */
51 6
    public function getConfiguration()
52
    {
53 6
        $indexes = [];
54 6
        foreach ($this->configArray as $config) {
55 6
            $types = $this->getTypes($config);
56 6
            $index = new IndexConfig($config['name'], $types, [
57 6
                'elasticSearchName' => $config['elasticsearch_name'],
58 6
                'settings' => $config['settings'],
59 6
                'useAlias' => $config['use_alias'],
60
            ]);
61
62 6
            $indexes[$config['name']] = $index;
63
        }
64
65 6
        return $indexes;
66
    }
67
68
    /**
69
     * Builds TypeConfig objects for each type.
70
     *
71
     * @param array $config
72
     *
73
     * @return array
74
     */
75 6
    protected function getTypes($config)
76
    {
77 6
        $types = [];
78
79 6
        if (isset($config['types'])) {
80 6
            foreach ($config['types'] as $typeConfig) {
81 6
                $types[$typeConfig['name']] = new TypeConfig(
82 6
                    $typeConfig['name'],
83 6
                    $typeConfig['mapping'],
84 6
                    $typeConfig['config']
85
                );
86
                // TODO: handle prototypes..
87
            }
88
        }
89
90 6
        return $types;
91
    }
92
}
93