Passed
Push — master ( caf566...d31b80 )
by Mina
03:05
created

ElasticClient::search()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: mina
5
 * Date: 3/17/17
6
 * Time: 2:20 AM
7
 */
8
9
namespace MAbadir\ElasticLaravel;
10
use Elasticsearch\ClientBuilder as Elastic;
11
12
class ElasticClient
13
{
14
    protected $index, $params, $client;
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
15
16
    /**
17
     * Connection constructor.
18
     */
19
    public function __construct()
20
    {
21
        $this->params = [
22
            'index' => config('elastic.index')
23
        ];
24
25
        $hosts = [
26
            [
27
                'host' => config('elastic.host'),
28
                'port' => config('elastic.port'),
29
//            'scheme' => 'https',
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
30
//            'user' => 'username',
31
//            'pass' => 'password!#$?*abc'
32
            ]
33
        ];
34
35
        $this->client = Elastic::create()           // Instantiate a new ClientBuilder
36
                                ->setHosts($hosts)      // Set the hosts
37
                                ->build();
38
    }
39
40
    /**
41
     * Merges Parameters received by client with index parameter
42
     *
43
     * @param $params
44
     *
45
     * @return array
46
     */
47
    protected function mergeParams($params)
48
    {
49
        return $this->params + $params;
50
    }
51
52
    /**
53
     * Returns the Elastic Search Client instance
54
     * @return \Elasticsearch\Client
55
     */
56
    public function getClient()
57
    {
58
        return $this->client;
59
    }
60
61
    /**
62
     * Creates the index with the given $params settings
63
     *
64
     * @param $params
65
     *
66
     * @return array
67
     */
68
    public function createIndex($params = [])
69
    {
70
        return $this->client->indices()->create($this->mergeParams($params));
71
    }
72
73
    /**
74
     * Drops the index with the given $params settings
75
     *
76
     * @param $params
77
     *
78
     * @return array
79
     */
80
    public function dropIndex($params = [])
81
    {
82
        return $this->client->indices()->delete($this->mergeParams($params));
83
    }
84
85
    /**
86
     * Indexes a document of the given parameters
87
     *
88
     * @param $params
89
     *
90
     * @return array
91
     */
92
    public function index($params)
93
    {
94
        return $this->client->index($this->mergeParams($params));
95
    }
96
97
    /**
98
     * Retrieves a document of the given parameters
99
     *
100
     * @param $params
101
     *
102
     * @return array
103
     */
104
    public function get($params)
105
    {
106
        return $this->client->get($this->mergeParams($params));
107
    }
108
109
    /**
110
     * Updates a document of the given parameters
111
     *
112
     * @param $params
113
     *
114
     * @return array
115
     */
116
    public function update($params)
117
    {
118
        return $this->client->update($this->mergeParams($params));
119
    }
120
121
    /**
122
     * Deletes a document of the given parameters
123
     *
124
     * @param $params
125
     *
126
     * @return array
127
     */
128
    public function delete($params)
129
    {
130
        return $this->client->delete($this->mergeParams($params));
131
    }
132
133
    /**
134
     * Searches the index with the given params
135
     *
136
     * @param $params
137
     *
138
     * @return array
139
     */
140
    public function search($params)
141
    {
142
        return $this->client->search($this->mergeParams($params));
143
    }
144
}