Passed
Push — master ( 1c3fc1...f51d93 )
by Mike
02:44
created

IndexBuilder::createTypes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Xervice\Elasticsearch\Business\Model\Index;
5
6
7
use DataProvider\IndexDataProvider;
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
        $index->create(
37 3
            $indexDataProvider->getArguments(),
38 3
            $indexDataProvider->getDelete()
39
        );
40
41 3
        $this->createTypes($index, $indexDataProvider->getTypes());
42 3
    }
43
44
    /**
45
     * @param \Elastica\Index $index
46
     * @param \DataProvider\TypeDataProvider[] $types
47
     */
48 3
    protected function createTypes(Index $index, array $types): void
49
    {
50 3
        foreach ($types as $type) {
51 2
            $esType = $index->getType($type->getName());
52
53 2
            $mapping = new Mapping();
54 2
            $mapping->setType($esType);
55
56 2
            $mapping->setProperties($type->getMapping());
57
58 2
            $mapping->send();
59
        }
60
    }
61
}