IndexBuilder   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 49
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A createIndex() 0 12 3
A createTypes() 0 11 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Xervice\Elasticsearch\Business\Model\Index;
5
6
7
use DataProvider\IndexDataProvider;
0 ignored issues
show
Bug introduced by
The type DataProvider\IndexDataProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Elastica\Client;
9
use Elastica\Index;
10
use Elastica\Type\Mapping;
11
12
class IndexBuilder implements IndexBuilderInterface
13
{
14
    /**
15
     * @var \Elastica\Client
16
     */
17
    private $client;
18
19
    /**
20
     * IndexBuilder constructor.
21
     *
22
     * @param \Elastica\Client $client
23
     */
24 4
    public function __construct(Client $client)
25
    {
26 4
        $this->client = $client;
27 4
    }
28
29
    /**
30
     * @param \DataProvider\IndexDataProvider $indexDataProvider
31
     */
32 3
    public function createIndex(IndexDataProvider $indexDataProvider): void
33
    {
34 3
        $index = $this->client->getIndex($indexDataProvider->getName());
35
36 3
        if (!$index->exists() || $indexDataProvider->getDelete()) {
37 3
            $index->create(
38 3
                $indexDataProvider->getArguments(),
39 3
                $indexDataProvider->getDelete()
40
            );
41
        }
42
43 3
        $this->createTypes($index, $indexDataProvider->getTypes());
44 3
    }
45
46
    /**
47
     * @param \Elastica\Index $index
48
     * @param \DataProvider\TypeDataProvider[] $types
49
     */
50 3
    protected function createTypes(Index $index, array $types): void
51
    {
52 3
        foreach ($types as $type) {
53 2
            $esType = $index->getType($type->getName());
54
55 2
            $mapping = new Mapping();
56 2
            $mapping->setType($esType);
57
58 2
            $mapping->setProperties($type->getMapping());
59
60 2
            $mapping->send();
61
        }
62
    }
63
}