ElasticApi::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Pgs\ElasticOM\ElasticApi;
4
5
use Elasticsearch\Client;
6
use Pgs\ElasticOM\Annotation\Visitor\AnnotationVisitorFactory;
7
use Pgs\ElasticOM\Annotation\Visitor\ClassMetadataVisiteeFactory;
8
9
class ElasticApi
10
{
11
    /** @var Client */
12
    protected $client;
13
14 4
    public function __construct(Client $client)
15
    {
16 4
        $this->client = $client;
17 4
    }
18
19 1
    public function createIndex(string $index)
20
    {
21 1
        return $this->client->indices()->create([
22 1
            'index' => $index,
23
        ]);
24
    }
25
26 1
    public function reindex(string $source, string $destination): array
27
    {
28 1
        return $this->client->reindex([
29 1
            'refresh' => true,
30
            'body' => [
31
                'source' => [
32 1
                    'index' => $source,
33
                ],
34
                'dest' => [
35 1
                    'index' => $destination,
36
                ],
37
            ],
38
        ]);
39
    }
40
41 1
    public function removeIndex(string $index)
42
    {
43 1
        return $this->client->indices()->delete([
44 1
            'index' => $index,
45
        ]);
46
    }
47
48 1
    public function createType(string $index, string $entityClass)
49
    {
50 1
        $classMetadata = ClassMetadataVisiteeFactory::create($entityClass);
51 1
        $properties = $classMetadata->accept(AnnotationVisitorFactory::getConfigVisitor());
52
53
        $indexConfig = [
54 1
            'index' => $index,
55 1
            'type' => $entityClass,
56
            'body' => [
57
                $entityClass => [
58 1
                    'properties' => $properties,
59
                ],
60
            ],
61
        ];
62
63 1
        return $this->client->indices()->putMapping($indexConfig);
64
    }
65
}
66