Completed
Push — api/develop ( e4271f...4a9872 )
by Bertrand
11:08
created

Elastic   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 27.45%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 13
c 1
b 0
f 1
lcom 1
cbo 5
dl 0
loc 187
ccs 14
cts 51
cp 0.2745
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A delete() 0 4 1
A indexMany() 0 18 2
A deleteIndex() 0 10 2
A indexExists() 0 4 1
B searchEmployee() 0 37 1
A search() 0 4 1
A getClient() 0 4 1
A indexAllEmployees() 0 15 2
A index() 0 4 1
1
<?php
2
3
namespace HRis\Api\ThirdParty;
4
5
use Elasticsearch\Client;
6
use HRis\Api\Eloquent\Employee;
7
8
class Elastic
9
{
10
    /**
11
     * @var Client
12
     */
13
    protected $client;
14
15
    /**
16
     * Elastic constructor.
17
     * @param Client $client
18
     */
19 2
    public function __construct(Client $client)
20
    {
21 2
        $this->client = $client;
22 2
    }
23
24
    /**
25
     * Delete a single item.
26
     *
27
     * @param  array $parameters
28
     *
29
     * @return array
30
     */
31
    public function delete(array $parameters)
32
    {
33
        return $this->client->delete($parameters);
34
    }
35
36
    /**
37
     * Index multiple items.
38
     *
39
     * This method normalises the 'bulk' method of the Elastic Search
40
     * Client to have a signature more similar to 'index'.
41
     *
42
     * @param  array $collection [[index, type, id, body], [index, type, id, body]...]
43
     *
44
     * @return array
45
     */
46
    public function indexMany(array $collection)
47
    {
48
        $parameters = [];
49
50
        foreach ($collection as $item) {
51
            $parameters['body'][] = [
52
                "index" => [
53
                    '_id'    => $item['id'],
54
                    '_index' => $item['index'],
55
                    '_type'  => $item['type'],
56
                ]
57
            ];
58
59
            $parameters['body'][] = $item['body'];
60
        }
61
62
        return $this->client->bulk($parameters);
63
    }
64
65
    /**
66
     * Delete Index.
67
     *
68
     * This suppresses any exceptions thrown by trying
69
     * to delete a non-existent index by first
70
     * checking if it exists, then deleting.
71
     *
72
     * @param  string $name
73
     *
74
     * @return bool
75
     */
76
    public function deleteIndex($name)
77
    {
78
        if (!$this->indexExists($name)) {
79
            return true;
80
        }
81
82
        return $this->client->indices()->delete([
83
            'index' => $name
84
        ]);
85
    }
86
87
    /**
88
     * @param $name
89
     *
90
     * @return bool
91
     */
92
    public function indexExists($name)
93
    {
94
        return $this->client->indices()->exists(['index' => $name]);
95
    }
96
97
    /**
98
     * @param $request
99
     *
100
     * @return array
101
     */
102
    public function searchEmployee($request)
103
    {
104
//        $query = [
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
105
//            'multi_match' => [
106
//                [
107
//                    'query'  => $request['query'],
108
//                    'fields' => [
109
//                        'employee_id^10', 'face_id', 'first_name^10', 'middle_name', 'last_name^9', 'suffix_name',
110
//                        'avatar', 'gender', 'address_1', 'address_2', 'address_postal_code', 'home_phone',
111
//                        'mobile_phone', 'work_email', 'other_email', 'social_security', 'tax_identification',
112
//                        'philhealth', 'hdmf_pagibig', 'mid_rtn', 'birth_date', 'remarks', 'joined_date',
113
//                        'probation_end_date', 'permanency_date', 'resign_date', 'city.name', 'country.name',
114
//                        'marital_status.name', 'nationality.name', 'province.name', 'user.email^8'
115
//                    ],
116
//                ],
117
//            ],
118
//        ];
119
120
        $query = [
121
            'query_string' => [
122
                'query'                => $request['query'].' OR *'.$request['query'].'*',
123
                'use_dis_max'          => true,
124
                'fuzzy_max_expansions' => 50,
125
                'fuzziness'            => 'AUTO',
126
            ]
127
        ];
128
129
        $parameters = [
130
            'index' => 'hris',
131
            'type'  => 'employee',
132
            'body'  => [
133
                'query' => $query
134
            ]
135
        ];
136
137
        return $this->search($parameters);
138
    }
139
140
    /**
141
     * @param array $parameters
142
     *
143
     * @return array
144
     */
145
    public function search(array $parameters)
146
    {
147
        return $this->client->search($parameters);
148
    }
149
150
    /**
151
     * @return Client
152
     */
153
    public function getClient()
154
    {
155
        return $this->client;
156
    }
157
158
    /**
159
     * Index all employees.
160
     *
161
     * @param Employee $employee
162
     *
163
     * @return array
164
     *
165
     * @author Bertrand Kintanar <[email protected]>
166
     */
167 2
    public function indexAllEmployees(Employee $employee)
168
    {
169 2
        $employees = $employee->with($employee->includes())->get();
170
171 2
        foreach ($employees as $employee) {
172 2
            $this->index([
173 2
                'index' => 'hris',
174 2
                'type'  => 'employee',
175 2
                'id'    => $employee->id,
176 2
                'body'  => $employee->toArray(),
177 2
            ]);
178
        }
179
180
        return $employees->count();
181
    }
182
183
    /**
184
     * Index a single item.
185
     *
186
     * @param  array $parameters [index, type, id, body]
187
     *
188
     * @return array
189
     */
190 2
    public function index(array $parameters)
191
    {
192 2
        return $this->client->index($parameters);
193
    }
194
}
195