Completed
Pull Request — master (#331)
by Elan
01:21
created

CustomController::query()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 9.456
c 0
b 0
f 0
cc 4
nc 8
nop 0
1
<?php
2
3
namespace XHGui\Controller;
4
5
use Slim\Slim;
6
use XHGui\Searcher\SearcherInterface;
7
use XHGui\AbstractController;
8
9
class CustomController extends AbstractController
10
{
11
    /**
12
     * @var SearcherInterface
13
     */
14
    protected $searcher;
15
16
    public function __construct(Slim $app, SearcherInterface $searcher)
17
    {
18
        parent::__construct($app);
19
        $this->searcher = $searcher;
20
    }
21
22
    public function get()
23
    {
24
        $this->_template = 'custom/create.twig';
25
    }
26
27
    public function help()
28
    {
29
        $request = $this->app->request();
30
        if ($request->get('id')) {
31
            $res = $this->searcher->get($request->get('id'));
32
        } else {
33
            $res = $this->searcher->latest();
34
        }
35
        $this->_template = 'custom/help.twig';
36
        $this->set([
37
            'data' => print_r($res->toArray(), 1),
38
        ]);
39
    }
40
41
    public function query()
42
    {
43
        $request = $this->app->request();
44
        $response = $this->app->response();
45
        $response['Content-Type'] = 'application/json';
46
47
        $query = json_decode($request->post('query'), true);
48
        $error = [];
49
        if (null === $query) {
50
            $error['query'] = json_last_error();
51
        }
52
53
        $retrieve = json_decode($request->post('retrieve'), true);
54
        if (null === $retrieve) {
55
            $error['retrieve'] = json_last_error();
56
        }
57
58
        if (count($error) > 0) {
59
            $json = json_encode(['error' => $error]);
60
61
            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...
62
        }
63
64
        $perPage = $this->app->config('page.limit');
65
66
        $res = $this->searcher->query($query, $perPage, $retrieve);
67
68
        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...
69
    }
70
}
71