Completed
Push — master ( 257818...6d6774 )
by Jeroen
06:17 queued 13s
created

SearchBundle/Provider/ElasticaProvider.php (2 issues)

calls to non-existent methods.

Bug Major

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\SearchBundle\Provider;
4
5
use Elastica\Client;
6
use Elastica\Document;
7
use Elastica\Index;
8
9
class ElasticaProvider implements SearchProviderInterface
10
{
11
    /** @var Client The Elastica client */
12
    private $client;
13
14
    /** @var array An array of Elastica search nodes (each item in the array needs a host and port) */
15
    private $nodes = array();
16
17
    /**
18
     * @return Client
19
     */
20
    public function getClient()
21
    {
22
        if (!$this->client instanceof Client) {
23
            $this->client = new Client(
24
                array('connections' => $this->nodes,
25
                )
26
            );
27
28
            //NEXT_MAJOR: remove checks and update ruflin/elastica dependency constraints
29
            if (!class_exists(\Elastica\Mapping::class)) {
30
                @trigger_error('Using a version of ruflin/elastica below v7.0 is deprecated since KunstmaanSearchBundle 5.6 and support for older versions will be removed in KunstmaanSearchBundle 6.0. Upgrade to ruflin/elastica and elasticsearch v7 instead.', E_USER_DEPRECATED);
31
            }
32
        }
33
34
        return $this->client;
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    public function getName()
41
    {
42
        return 'Elastica';
43
    }
44
45
    /**
46
     * @param string $indexName
47
     *
48
     * @return \Elastica\Index
49
     */
50
    public function createIndex($indexName)
51
    {
52
        return new Index($this->getClient(), $indexName);
53
    }
54
55
    /**
56
     * @param string $indexName
57
     *
58
     * @return \Elastica\Index
59
     */
60
    public function getIndex($indexName)
61
    {
62
        return $this->getClient()->getIndex($indexName);
63
    }
64
65
    /**
66
     * @param string $uid
67
     * @param array  $document
68
     * @param string $indexName
69
     * @param string $indexType
70
     *
71
     * @return \Elastica\Document
72
     */
73
    public function createDocument($uid, $document, $indexName = '', $indexType = '')
74
    {
75
        if (method_exists(Document::class, 'setType')) {
76
            return new Document($uid, $document, $indexType, $indexName);
77
        }
78
79
        return new Document($uid, $document, $indexName);
80
    }
81
82
    /**
83
     * @param string $indexName
84
     * @param string $indexType
85
     * @param array  $document
86
     * @param string $uid
87
     *
88
     * @return \Elastica\Response
89
     */
90 View Code Duplication
    public function addDocument($indexName, $indexType, $document, $uid)
91
    {
92
        $doc = $this->createDocument($uid, $document);
93
        $index = $this->getClient()->getIndex($indexName);
94
        if (method_exists($index, 'getType')) {
95
            return $index->getType($indexType)->addDocument($doc);
0 ignored issues
show
The method getType() does not seem to exist on object<Elastica\Index>.

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...
96
        }
97
98
        return $index->addDocument($doc);
99
    }
100
101
    /**
102
     * @param array  $docs
103
     * @param string $indexName
104
     * @param string $indexType
105
     *
106
     * @return \Elastica\Bulk\ResponseSet
107
     */
108
    public function addDocuments($docs, $indexName = '', $indexType = '')
109
    {
110
        // Ignore indexName & indexType for Elastica, they have already been set in the document...
111
        return $this->getClient()->addDocuments($docs);
112
    }
113
114
    /**
115
     * @param string $indexName
116
     * @param string $indexType
117
     * @param string $uid
118
     *
119
     * @return \Elastica\Bulk\ResponseSet
120
     */
121
    public function deleteDocument($indexName, $indexType, $uid)
122
    {
123
        $ids = array($uid);
124
125
        return $this->deleteDocuments($indexName, $indexType, $ids);
126
    }
127
128
    /**
129
     * @param string $indexName
130
     * @param string $indexType
131
     * @param array  $ids
132
     *
133
     * @return \Elastica\Bulk\ResponseSet
134
     */
135 View Code Duplication
    public function deleteDocuments($indexName, $indexType, array $ids)
136
    {
137
        $index = $this->getIndex($indexName);
138
139
        if (method_exists($index, 'getType')) {
140
            $type = $index->getType($indexType);
0 ignored issues
show
The method getType() does not seem to exist on object<Elastica\Index>.

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...
141
142
            return $this->getClient()->deleteIds($ids, $index, $type);
143
        }
144
145
        return $this->getClient()->deleteIds($ids, $index);
146
    }
147
148
    /**
149
     * @param string $indexName
150
     *
151
     * @return \Elastica\Response|null
152
     */
153
    public function deleteIndex($indexName)
154
    {
155
        $index = $this->getIndex($indexName);
156
        if ($index->exists()) {
157
            return $index->delete();
158
        }
159
160
        return null;
161
    }
162
163
    /**
164
     * @param string      $host
165
     * @param int         $port
166
     * @param string|null $username
167
     * @param string|null $password
168
     */
169
    public function addNode($host, $port, $username = null, $password = null)
170
    {
171
        foreach ($this->nodes as $node) {
172
            if ($node['host'] === $host && $node['port'] === $port) {
173
                return;
174
            }
175
        }
176
177
        $authHeader = null;
178
        if (null !== $username && $password !== null) {
179
            $authHeader = array('Authorization' => 'Basic ' . base64_encode($username . ':' . $password));
180
        }
181
182
        $this->nodes[] = array('host' => $host, 'port' => $port, 'headers' => $authHeader);
183
    }
184
185
    /**
186
     * @param array $nodes
187
     */
188
    public function setNodes($nodes)
189
    {
190
        $this->nodes = $nodes;
191
    }
192
193
    /**
194
     * @return array
195
     */
196
    public function getNodes()
197
    {
198
        return $this->nodes;
199
    }
200
}
201