Completed
Push — master ( d9c59b...8e2d47 )
by Nicolas
03:16
created

Reindex::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
namespace Elastica;
3
4
use Elastica\Query\AbstractQuery;
5
6
class Reindex
7
{
8
    const VERSION_TYPE = 'version_type';
9
    const VERSION_TYPE_INTERNAL = 'internal';
10
    const VERSION_TYPE_EXTERNAL = 'external';
11
    const OPERATION_TYPE = 'op_type';
12
    const OPERATION_TYPE_CREATE = 'create';
13
    const CONFLICTS = 'conflicts';
14
    const CONFLICTS_PROCEED = 'proceed';
15
    const TYPE = 'type';
16
    const SIZE = 'size';
17
    const QUERY = 'query';
18
19
    /**
20
     * @var Index
21
     */
22
    protected $_oldIndex;
23
24
    /**
25
     * @var Index
26
     */
27
    protected $_newIndex;
28
29
    /**
30
     * @var array
31
     */
32
    protected $_options;
33
34
    public function __construct(Index $oldIndex, Index $newIndex, array $options = [])
35
    {
36
        $this->_oldIndex = $oldIndex;
37
        $this->_newIndex = $newIndex;
38
        $this->_options = $options;
39
    }
40
41
    public function run()
42
    {
43
        $body = $this->_getBody($this->_oldIndex, $this->_newIndex, $this->_options);
44
45
        $reindexEndpoint = new \Elasticsearch\Endpoints\Reindex();
46
        $reindexEndpoint->setBody($body);
47
48
        $this->_oldIndex->getClient()->requestEndpoint($reindexEndpoint);
49
        $this->_newIndex->refresh();
50
51
        return $this->_newIndex;
52
    }
53
54
    protected function _getBody($oldIndex, $newIndex, $options)
55
    {
56
        $body = array_merge([
57
            'source' => $this->_getSourcePartBody($oldIndex, $options),
58
            'dest' => $this->_getDestPartBody($newIndex, $options)
59
        ], $this->_resolveBodyOptions($options));
60
61
        return $body;
62
    }
63
64
    protected function _getSourcePartBody(Index $index, array $options)
65
    {
66
        $sourceBody = array_merge([
67
            'index' => $index->getName(),
68
        ], $this->_resolveSourceOptions($options));
69
70
        $sourceBody = $this->_setSourceQuery($sourceBody);
71
        $sourceBody = $this->_setSourceType($sourceBody);
72
73
        return $sourceBody;
74
    }
75
76
    protected function _getDestPartBody(Index $index, array $options)
77
    {
78
        return array_merge([
79
            'index' => $index->getName(),
80
        ], $this->_resolveDestOptions($options));
81
    }
82
83
    private function _resolveSourceOptions(array $options)
84
    {
85
        return array_intersect_key($options, [
86
            self::TYPE => null,
87
            self::QUERY => null,
88
        ]);
89
    }
90
91
    private function _resolveDestOptions(array $options)
92
    {
93
        return array_intersect_key($options, [
94
            self::VERSION_TYPE => null,
95
            self::OPERATION_TYPE => null,
96
        ]);
97
    }
98
99
    private function _resolveBodyOptions(array $options)
100
    {
101
        return array_intersect_key($options, [
102
            self::SIZE => null,
103
            self::CONFLICTS => null,
104
        ]);
105
    }
106
107
    /**
108
     * @param array $sourceBody
109
     *
110
     * @return array
111
     */
112
    private function _setSourceQuery(array $sourceBody)
113
    {
114
        if (isset($sourceBody[self::QUERY]) && $sourceBody[self::QUERY] instanceof AbstractQuery) {
115
            $sourceBody[self::QUERY] = $sourceBody[self::QUERY]->toArray();
116
        }
117
        return $sourceBody;
118
    }
119
120
    /**
121
     * @param array $sourceBody
122
     *
123
     * @return array
124
     */
125
    private function _setSourceType(array $sourceBody)
126
    {
127
        if (isset($sourceBody[self::TYPE]) && !is_array($sourceBody[self::TYPE])) {
128
            $sourceBody[self::TYPE] = [$sourceBody[self::TYPE]];
129
        }
130
        if (isset($sourceBody[self::TYPE])) {
131
            foreach ($sourceBody[self::TYPE] as $key => $type) {
132
                if ($type instanceof Type) {
133
                    $sourceBody[self::TYPE][$key] = $type->getName();
134
                }
135
           }
136
        }
137
        return $sourceBody;
138
    }
139
}
140