Passed
Push — feature/initial-implementation ( fae671...591f29 )
by Fike
02:37
created

IndexClient   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 67
c 0
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 5 2
A __construct() 0 3 1
A exists() 0 3 1
A setIndexing() 0 4 1
A create() 0 15 4
A getMapping() 0 7 1
A setMapping() 0 6 1
1
<?php
2
3
namespace AmaTeam\ElasticSearch\Client;
4
5
use AmaTeam\ElasticSearch\API\Client\IndexClientInterface;
6
use AmaTeam\ElasticSearch\API\IndexingInterface;
7
use AmaTeam\ElasticSearch\API\MappingInterface;
8
use AmaTeam\ElasticSearch\Mapping\Operations as Mappings;
9
use AmaTeam\ElasticSearch\Mapping\Type\RootType;
10
use Elasticsearch\Client;
11
use Elasticsearch\Common\Exceptions\Missing404Exception;
12
13
class IndexClient implements IndexClientInterface
14
{
15
    /**
16
     * @var Client
17
     */
18
    private $client;
19
20
    /**
21
     * @param Client $client
22
     */
23
    public function __construct(Client $client)
24
    {
25
        $this->client = $client;
26
    }
27
28
    public function create(string $index, IndexingInterface $indexing = null, array $mappings = []): void
29
    {
30
        $payload = ['settings' => $indexing->getOptions(), 'mappings' => []];
0 ignored issues
show
Bug introduced by
The method getOptions() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
        $payload = ['settings' => $indexing->/** @scrutinizer ignore-call */ getOptions(), 'mappings' => []];

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
31
        foreach ($mappings as $type => $mapping) {
32
            $normalized = Mappings::toStdObject($mapping);
33
            unset($normalized->type);
34
            $payload['mappings'][$type] = $normalized;
35
        }
36
        foreach ($payload as $key => $value) {
37
            if (empty($value)) {
38
                unset($payload[$key]);
39
            }
40
        }
41
        $parameters = ['index' => $index, 'body' => $payload];
42
        $this->client->indices()->create($parameters);
43
    }
44
45
    public function exists(string $index): bool
46
    {
47
        return $this->client->indices()->exists(['index' => $index]);
48
    }
49
50
    public function delete(string $index): void
51
    {
52
        try {
53
            $this->client->delete(['index' => $index]);
54
        } catch (Missing404Exception $e) {
55
            // noop
56
        }
57
    }
58
59
    public function setMapping(string $index, string $type, MappingInterface $mapping): void
60
    {
61
        $payload = Mappings::toStdObject($mapping);
62
        unset($payload->type);
63
        $parameters = ['index' => $index, 'type' => $type, 'body' => $payload];
64
        $this->client->indices()->putMapping($parameters);
65
    }
66
67
    public function getMapping(string $index, string $type): ?MappingInterface
68
    {
69
        $data = $this->client->indices()->getMapping(['index' => $index, 'type' => $type]);
70
        $normalized = $data[$index]['mappings'][$type];
71
        $mapping = Mappings::fromArray($normalized);
72
        $mapping->setType(RootType::ID);
73
        return $mapping;
74
    }
75
76
    public function setIndexing(string $index, IndexingInterface $indexing): void
77
    {
78
        $parameters = ['index' => $index, 'body' => $indexing->getOptions()];
79
        $this->client->indices()->putSettings($parameters);
80
    }
81
}
82