Elastic   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 25.92%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 188
ccs 14
cts 54
cp 0.2592
rs 10
c 0
b 0
f 0

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

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
190
191 2
        foreach ($employees as $employee) {
192 2
            $this->index([
193 2
                'index' => 'hris',
194 2
                'type'  => 'employee',
195 2
                'id'    => $employee->id,
196 2
                'body'  => $employee->toArray(),
197 2
            ]);
198
        }
199
200
        return $employees->count();
201
    }
202
203
    /**
204
     * Index a single item.
205
     *
206
     * @param array $parameters [index, type, id, body]
207
     *
208
     * @return array
209
     */
210 2
    public function index(array $parameters)
211
    {
212 2
        return $this->client->index($parameters);
213
    }
214
}
215