Xhgui_Controller_Custom   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 61
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 4 1
A help() 0 13 2
A query() 0 28 4
1
<?php
2
3
use Slim\Slim;
4
5
class Xhgui_Controller_Custom extends Xhgui_Controller
6
{
7
    /**
8
     * @var Xhgui_Searcher_Interface
9
     */
10
    protected $searcher;
11
12
    public function __construct(Slim $app, Xhgui_Searcher_Interface $searcher)
13
    {
14
        parent::__construct($app);
15
        $this->searcher = $searcher;
16
    }
17
18
    public function get()
19
    {
20
        $this->_template = 'custom/create.twig';
21
    }
22
23
    public function help()
24
    {
25
        $request = $this->app->request();
26
        if ($request->get('id')) {
27
            $res = $this->searcher->get($request->get('id'));
28
        } else {
29
            $res = $this->searcher->latest();
30
        }
31
        $this->_template = 'custom/help.twig';
32
        $this->set(array(
33
            'data' => print_r($res->toArray(), 1)
34
        ));
35
    }
36
37
    public function query()
38
    {
39
        $request = $this->app->request();
40
        $response = $this->app->response();
41
        $response['Content-Type'] = 'application/json';
42
43
        $query = json_decode($request->post('query'), true);
44
        $error = array();
45
        if (null === $query) {
46
            $error['query'] = json_last_error();
47
        }
48
49
        $retrieve = json_decode($request->post('retrieve'), true);
50
        if (null === $retrieve) {
51
            $error['retrieve'] = json_last_error();
52
        }
53
54
        if (count($error) > 0) {
55
            $json = json_encode(array('error' => $error));
56
            return $response->body($json);
0 ignored issues
show
Bug introduced by
The method body cannot be called on $response (of type array<string,string,{"Content-Type":"string"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
57
        }
58
59
        $perPage = $this->app->config('page.limit');
60
61
        $res = $this->searcher->query($query, $perPage, $retrieve);
62
63
        return $response->body(json_encode($res));
0 ignored issues
show
Bug introduced by
The method body cannot be called on $response (of type array<string,string,{"Content-Type":"string"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
64
    }
65
}
66