Passed
Pull Request — master (#87)
by
unknown
05:19
created

Index::getConfig()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 15
ccs 8
cts 9
cp 0.8889
crap 4.0218
rs 10
1
<?php
2
3
namespace Matchish\ScoutElasticSearch\ElasticSearch;
4
5
use Matchish\ScoutElasticSearch\Searchable\ImportSource;
6
7
/**
8
 * @internal
9
 */
10
final class Index
11
{
12
    /**
13
     * @var array
14
     */
15
    private $aliases = [];
16
17
    /**
18
     * @var string
19
     */
20
    private $name;
21
    /**
22
     * @var array|null
23
     */
24
    private $settings;
25
    /**
26
     * @var array|null
27
     */
28
    private $mappings;
29
30
    /**
31
     * Index constructor.
32
     * @param string $name
33
     * @param array $settings
34
     * @param array $mappings
35
     */
36 20
    public function __construct(string $name, array $settings = null, array $mappings = null)
37
    {
38 20
        $this->name = $name;
39 20
        $this->settings = $settings;
40 20
        $this->mappings = $mappings;
41 20
    }
42
43
    /**
44
     * @return array
45
     */
46 15
    public function aliases(): array
47
    {
48 15
        return $this->aliases;
49
    }
50
51
    /**
52
     * @return string
53
     */
54 17
    public function name(): string
55
    {
56 17
        return $this->name;
57
    }
58
59
    /**
60
     * @param Alias $alias
61
     */
62 14
    public function addAlias(Alias $alias): void
63
    {
64 14
        $this->aliases[$alias->name()] = $alias->config() ?: new \stdClass();
65 14
    }
66
67
    /**
68
     * @return array
69
     */
70 15
    public function config(): array
71
    {
72 15
        $config = [];
73 15
        if (! empty($this->settings)) {
74 15
            $config['settings'] = $this->settings;
75
        }
76 15
        if (! empty($this->mappings)) {
77 15
            $config['mappings'] = $this->mappings;
78
        }
79 15
        if (! empty($this->aliases())) {
80 14
            $config['aliases'] = $this->aliases();
81
        }
82
83 15
        return $config;
84
    }
85
86 18
    public static function fromSource(ImportSource $source): Index
87
    {
88 18
        $name = $source->searchableAs().'_'.time();
89
90
        $defaultSettings = [
91 18
            'number_of_shards' => 1,
92
            'number_of_replicas' => 0,
93
        ];
94
95 18
        $settings = self::getConfig($source, 'elasticsearch.indices.settings', $defaultSettings);
96 18
        $mappings = self::getConfig($source, 'elasticsearch.indices.mappings', config('elasticsearch.indices.mappings.default'));
97
98 18
        return new static($name, $settings, $mappings);
99
    }
100
101 18
    private static function getConfig(ImportSource $source, $baseKey, $default = null): ?array
102
    {
103 18
        if($config = config($baseKey.$source->searchableAs(), null)){
104
            return $config;
105
        }
106
107 18
        foreach(config($baseKey) as $key => $mapping){
108 18
            $keyPattern = str_replace('*', '(\w{1,})', $key);
109 18
            preg_match("/{$keyPattern}/", $source->searchableAs(), $matches);
110 18
            if($matches){
111 18
                return $mapping;
112
            }
113
        }
114
115 18
        return $default ?? null;
116
    }
117
}
118