Completed
Push — master ( cd82a5...640bc7 )
by Mehmet
02:09
created

doc_delete.php ➔ es_doc_delete()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 3 Features 0
Metric Value
cc 2
eloc 18
c 8
b 3
f 0
nc 2
nop 2
dl 0
loc 27
rs 8.8571
1
<?php
2
3
function es_doc_delete($request, $args)
4
{
5
    global $esConn;
6
    $from = $request->getParam('from', 0);
7
    $sort = $request->getParam('sort', '');
8
    $query = $request->getParam('query', '');
9
    $params = [
10
        'index'=>$args['index'],
11
        'type'=>$args['type'],
12
        'id'=>$args['id'],
13
    ];
14
    try {
15
        $result = $esConn->delete($params);
16
    } catch (Exception $e) {
17
        /*
18
        If you try to delete a document with an _id that does not exist, $ES->delete throws an Exception.
19
        B/c of default behaviour of reformo/rslim, it bypasses all Exceptions and it returns Exception message.
20
        In this case it returns this message as a plain text json that elasticsearch-php returns.
21
        */
22
        $result = json_decode($e->getMessage(), true);
23
    }
24
    $found = (int) $result['found'];
25
    sleep(1);
26
    return ['redirect'=>BASE_HREF . '/' . $args['index'] . '/' . $args['type']
27
        . '?from=' . $from . "&sort=" . $sort . "&query=" . $query
28
        . '&res=success&req=delete&f=' . $found];
29
}
30