Passed
Push — master ( 820a38...a5ee5d )
by Mihail
05:04
created

Search   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 10
wmc 3
lcom 1
cbo 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A actionIndex() 0 22 3
1
<?php
2
3
namespace Apps\Controller\Api;
4
5
6
use Apps\Model\Front\Search\EntitySearchMain;
7
use Extend\Core\Arch\ApiController;
8
use Ffcms\Core\Exception\JsonException;
9
use Ffcms\Core\Helper\Type\Str;
10
11
/**
12
 * Class Search. Make search with json response by standard model
13
 * @package Apps\Controller\Api
14
 */
15
class Search extends ApiController
16
{
17
    /**
18
     * Print json response for search query based on standard model
19
     * @return string
20
     * @throws JsonException
21
     */
22
    public function actionIndex()
23
    {
24
        $this->setJsonHeader();
25
        // get search query as string from request
26
        $query = $this->request->query->get('query', null);
27
        if (Str::likeEmpty($query) || Str::length($query) < 2) {
28
            throw new JsonException('Short query');
29
        }
30
31
        // initialize basic search model
32
        $model = new EntitySearchMain($query, ['itemPerApp' => 3]);
33
        $model->make();
34
35
        // build response by relevance as array
36
        $response = $model->getRelevanceSortedResult();
37
38
        return json_encode([
39
            'status' => 1,
40
            'count' => count($response),
41
            'data' => $response
42
        ]);
43
    }
44
}