Completed
Push — master ( 212f0a...214901 )
by Lukas Kahwe
23s queued 10s
created

src/Configuration/Source/ContainerSource.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\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
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
    /**
30
     * @param array $configArray
31
     */
32 10
    public function __construct(array $configArray)
33
    {
34 10
        $this->configArray = $configArray;
35 10
    }
36
37
    /**
38
     * Should return all configuration available from the data source.
39
     *
40
     * @return IndexConfig[]
41
     */
42 10
    public function getConfiguration()
43
    {
44 10
        $indexes = [];
45 10
        foreach ($this->configArray as $config) {
46 10
            $types = $this->getTypes($config);
47 10
            $index = new IndexConfig($config['name'], $types, [
48 10
                'elasticSearchName' => $config['elasticsearch_name'],
49 10
                'settings' => $config['settings'],
50 10
                'useAlias' => $config['use_alias'],
51
            ]);
52
53 10
            $indexes[$config['name']] = $index;
54
        }
55
56 10
        return $indexes;
57
    }
58
59
    /**
60
     * Builds TypeConfig objects for each type.
61
     *
62
     * @param array $config
63
     *
64
     * @return array
65
     */
66 10
    protected function getTypes($config)
67
    {
68 10
        $types = [];
69
70 10
        if (isset($config['types'])) {
71 10
            foreach ($config['types'] as $typeConfig) {
72 10
                $types[$typeConfig['name']] = new TypeConfig(
73 10
                    $typeConfig['name'],
74 10
                    $typeConfig['mapping'],
75 10
                    $typeConfig['config']
76
                );
77
                // TODO: handle prototypes..
78
            }
79
        }
80
81 10
        return $types;
82
    }
83
}
84